Forum Discussion
Sort by another column error
- 9 years ago
This is one of those time where I don't think the error message is entirely helpful, and I'm not even sure why it's an issue. What it really means is that you can't sort by a column that depends on the column you want sorted. You can see the same problem here in Power Query and Power Pivot.
As a workaround, can you recreate the Sort Order in the Query Editor? If you go to Edit Queries, you can use the Add Custom Column feature in the Add Column tab in the ribbon. Use this formula:
=if [Category]="A" then 1 else if [Category]="C" then 2 else if [Category]="B" then 3 else 0
You have to end with an "else" which is the only reason I end with else 0. If you know you'll only have A, B, and C values in Category, it won't hurt anything, but neither will:
if [Category]="A" then 1 else if [Category]="C" then 2 else 3
That will create the Sort Order column. If you prefer a UI to writing the formula by hand, you can alternatively use the Conditional Column under Add Column:
Once you Close and Apply, you can then go to the Data pane and sort your Category column by Sort Order.
You can only sort your data on one coulm. In your case, your sort order has already been sorted by category column and
it won't let you sort your data based on the other coulmn.
I am sorry, I don't understand.
The original data only contains the [Category] column.
I want to custom sort the [Category] column, so I created the [Sort Order] column.
The [Sort Order] column is sorted by itself.
I want to sort the [Category] column by the [Sort Order] column.
?
- KGrice9 years agoMemorable Member
This is one of those time where I don't think the error message is entirely helpful, and I'm not even sure why it's an issue. What it really means is that you can't sort by a column that depends on the column you want sorted. You can see the same problem here in Power Query and Power Pivot.
As a workaround, can you recreate the Sort Order in the Query Editor? If you go to Edit Queries, you can use the Add Custom Column feature in the Add Column tab in the ribbon. Use this formula:
=if [Category]="A" then 1 else if [Category]="C" then 2 else if [Category]="B" then 3 else 0
You have to end with an "else" which is the only reason I end with else 0. If you know you'll only have A, B, and C values in Category, it won't hurt anything, but neither will:
if [Category]="A" then 1 else if [Category]="C" then 2 else 3
That will create the Sort Order column. If you prefer a UI to writing the formula by hand, you can alternatively use the Conditional Column under Add Column:
Once you Close and Apply, you can then go to the Data pane and sort your Category column by Sort Order.
- Sokon8 years agoAdvocate V
Works great, thanks! Would be great if there was a solution using only DAX, though.
- Anonymous8 years agoNot applicable
Just want to add to this thread that the Power Query/M alternative only works if the column you need to sort is available via Edit Query.
In my case, I have a ranges column created in a table which was itself created in Report View.
The reason for this is that I am avoiding Edit Query "Group By" functionality as a way to create aggregate datasets because unless you know M pretty well you cannot create all the aggregates you need via the Group By dialogue (e.g., there is no DISTINCTCOUNT option).
So in Report View I have created a table and in that table a column with values, generated using nested IFs (very easy to do in DAX), like:
"0-19", "20-39", etc. through to "100+"
Without sorting, 100+ appears first in visuals. Even if you make it 100-150, the same happens as the values are treated as text. When you try to create a sort column referencing the range column, you get the error reported here.
(I also want to avoid ugly workarounds like "1. 0-19", "2. 20-39", etc.)
So I have to concur that a DAX-based solution which can be implemented via Report View would be far better than being forced to work with M, which is less efficient and intuitive than DAX for the non-specialist.
Thanks!
- bullius9 years agoHelper V
Thanks! That works perfectly.
- vanessa8 years agoPost Patron
I am facing a similar issue on a summarized table. Hence I can't add a column in edit queries mode.
Is there a workaround for this?
- Anonymous8 years agoNot applicable
Could you specify what you're trying to do?