Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Power Query: sort by custom list

I have a list of time seasons within school years:

"Fall 12-13",
"Winter 12-13",
"Spring 12-13",
"Fall 13-14",
etc.

I want to sort a large number of rows chronologically based on these values. In Excel it is possible to sort by a custom list where I simply input the order that I want the items to be sorted by.

I need that same functionality in Power Query but I have not yet figured out how to do this. I have only been able to set sort order to Order.Ascending or Order.Descending.

Is there a good way to implement sort-by-custom-list in Power Query?

like image 665
zacksg1 Avatar asked Oct 14 '14 15:10

zacksg1


People also ask

Can I sort in Power Query?

You can sort a table in Power Query by one column or multiple columns. For example, take the following table with the columns named Competition, Competitor, and Position. Table with Competition, Competitor, and Position columns.

How do I sort text in Power Query?

Sorting will be easy. Click dropdown of the column and choose Sort Ascending in Query Editor. We also can add an index to keep the new order.


1 Answers

The second argument to Table.Sort can also be a function: either a function that takes two rows and returns an ordering between them (a la strcmp) or a function that takes a single row and returns the value that should be used for comparisons. So one way to sort the values you describe would be to say

= Table.Sort(Table, each List.PositionOf({"Fall 12-13", "Winter 12-13", "Spring 12-13", "Fall 13-14"}, [Column]))
like image 154
Curt Hagenlocher Avatar answered Nov 03 '22 04:11

Curt Hagenlocher