Forum Discussion
Sort by another column error
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!
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.
13 Replies
- AnonymousNot applicable
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.
- floydfrancisNew Member
Anonymous solution of a second calculated column worked great for me - thanks!
- BhaveshPatelSuper User
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.
- BhaveshPatelSuper User
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.
- bulliusHelper V
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.
?
- KGriceMemorable 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.