Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
bullius
Helper V
Helper V

Sort by another column error

Hi

 

I have categories that I want to sort. The data looks like this:

 

CategorySort Order
A1
A1
A1
B3
B3
B3
C2
C2
C2

 

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:

 

Sort Error.png

 

Anyone know how the reason for this and how to resolve?

 

Thanks!

 

 

 

1 ACCEPTED 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:

 

Category.PNG

 

Once you Close and Apply, you can then go to the Data pane and sort your Category column by Sort Order.

View solution in original post

13 REPLIES 13
darentengdrake
Resolver II
Resolver II

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!

BhaveshPatel
Community Champion
Community Champion

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.

Thanks & Regards,
Bhavesh

Love the Self Service BI.
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.

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:

 

Category.PNG

 

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.

@KGrice

 

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?

Anonymous
Not applicable

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.

Anonymous
Not 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!

 

Thanks! That works perfectly.

BhaveshPatel
Community Champion
Community Champion

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.

 

 

Thanks & Regards,
Bhavesh

Love the Self Service BI.
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.