Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
Hi
I have categories that I want to sort. The data looks like this:
Category | Sort Order |
A | 1 |
A | 1 |
A | 1 |
B | 3 |
B | 3 |
B | 3 |
C | 2 |
C | 2 |
C | 2 |
To populate the sort order column, I wrote the following formula:
Sort Order =
IF ( [Category] = "A", 1,
IF ( [Category] = "C", 2,
IF ( [Category] = "B", 3)))
Then, I tried to use the "Sort by Column" tool to sort the Category column by the Sort Order column, but the following error message occured:
Anyone know how the reason for this and how to resolve?
Thanks!
Solved! Go to Solution.
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.
Hi @bullius
I know I am a few years late, but a figured out a neat workaround.
Once you created the sort order column, just create another column that is equal to your "Category" column. Use the "Sort by Column" on the new "Category" column instead. No mquery is needed.
@darentengdrake solution of a second calculated column worked great for me - thanks!
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.
?
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.
Unfortunately I won't be able to use Custom column from Edit queries as my Table is created in Power Bi using New Table. I cannot see this in edit Query editor. When I use New column in VI editor to create sort column. Its thrwoing the error.
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?
Could you specify what you're trying to do?
Since the calculated tables do not appear ampong the tables in edit query, i also can not add a conditional column to create a sort order column. I guess that's what she means.
Thanks
Works great, thanks! Would be great if there was a solution using only DAX, though.
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!
Thanks! That works perfectly.
PowerBi has the built in feature of sort by column.
if you sort the sort order by category column, it will solve your problem instead of writing a calculated column.
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
114 | |
94 | |
90 | |
35 | |
35 |
User | Count |
---|---|
154 | |
100 | |
82 | |
63 | |
53 |