Forum Discussion
How do I return a calculated table sorted by a specific column?
Hi there, I'm fairly new to DAX and I have a simple question I can't seem to find an answer to. In the image below I am adding a percentage column to a table in Power BI Desktop, What is the syntax for returning the table sorted in descending order by the percentage column? So, the equivilent of an Order By clause. I have searched but found little information on this topic, I would have expected it to be quite intuitive!
Many thanks
Simon
10 Replies
- RémiResolver III
Hi,
You can't sort columns in this data view.
Do a table with [Sector] and [Percentage] then click on the three dots top/right of the visualisations, you can choose your sort.
Otherwise, if you want a specific order for a column (ex with months : jan / feb / ... ), you have to create a sort column with the number of the order (same ex : Sort Column = MONTH([Date]) ) and you can do a sort by column.
- SimonMRegular Visitor
Hi Rémi, thanks for getting back. Let me explain what I am ultimately trying to do, maybe there is another way around it.
Extract max value from percentage column (you helped me this on my last post so that's fine)
Identify which row the maximum value appeared in.
Extract other information from that row where the maximum value appeared.
My 'quick fix' in my head was to just sort the table descending on the percentage column thus having the max value always on row 1. Bear in mind, this table will be calculated inside a measure, I have simplified it in my example.
Am I thinking through this the wrong way? As I said, I'm still learning from my mistakes!
Simon
- ALeefResolver II
The first issue is that you can't sort highest to lowest with the "sort by" button, because it isn't available in PowerBI (hopefully they are working on it).
ORDER BY is part of the query builder, so if your data model works with it, you could sort in your query - although I'm assuming your percentage is a calculate column, so it won't work in that case: https://msdn.microsoft.com/en-us/library/gg492156.aspx
As a potential solve for your issue, you could use TOPN and only return the first row.
https://msdn.microsoft.com/en-us/library/gg492198.aspx
I *think* that will give you the desired result, but the documentation says it doesn't guarantee the sorting of the results -but since you are only returning one row, it should be the right one, assuming your orderby expression is right.
- MarkLafSuper User
Why not use a measure?
Max % Sector = CALCULATE(VALUES(TableSummaries[Sector]),TOPN(1,FILTER(ALL(TableSummaries),TableSummaries[Percentage]=CALCULATE(MAX(TableSummaries[Percentage]),ALL(TableSummaries)))))
Or avoid relying on a calculated column and just go with the following (LASTNONBLANK+TOPN lifted from yet another great SQLBI article):
Perc = SUMX(TableSummaries,SUM(TableSummaries[Counts])/SUM(TableSummaries[Base]))
Max % Sector NoCC = CALCULATE(LASTNONBLANK(TOPN(1,VALUES(TableSummaries[Sector]),[Perc]),1),ALL(TableSummaries))