The sort by column feature of Power BI hides some potentially unwanted complexities. This blog post shows how you can avoid having to sort a column by another column while maintaining a custom sort order.
Here’s a typical example: you sort month names by month numbers. Let’s say you want to remove filters from month name by using
REMOVEFILTERS. To make it work, you’ve got to remove filters from month number too, otherwise the filters will stay.
Some people are very disappointed that Power BI works this way:
Even experienced professionals fall into this trap. For a more detailed description of the problem, see “Side effects of the Sort By Column setting in DAX” by SQLBI.
Building on my previous blog post, where I sorted duplicate values, I realized that we can prepend zero-width spaces to integrate the sort order.
For example, to create a pre-sorted single-letter day name column, we can use the following code in DAX:
Day Name Abbr Sorted = CONCATENATE( REPT(UNICHAR(8203), 'Calendar DAX'[Day of Week]), LEFT('Calendar DAX'[Day Name], 1) )
Here’s the corresponding code in M:
Text.Repeat(Character.FromNumber(8203), [Day of Week]) & Text.Start([Day Name], 1)
The only catch is that to sort it correctly, you’ll have to sort it in descending order:
If you’d like to avoid that, you can make a small tweak to the code:
Day Name Abbr Sorted Asc = CONCATENATE( REPT(UNICHAR(8203), 7 - 'Calendar DAX'[Day of Week]), LEFT('Calendar DAX'[Day Name], 1) )
NB: In this example, 7 is the number of distinct day names, which is known in advance — it’s the maximum Day of Week value.
Now the day names are sorted correctly in ascending order without sorting by another column:
If you’re going to use this feature, you should be aware that the explain the increase/decrease feature won’t work. Otherwise this may be a good solution especially when you’d like to avoid having extra columns in your model.
Sample file: Built-in column sort order in Power BI.pbix (43 KB)
Happy sorting 🙂