cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
bullius Member
Member

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

Accepted Solutions
KGrice Established Member
Established Member

Re: Sort by another column error

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

10 REPLIES 10
BhaveshPatel Super Contributor
Super Contributor

Re: Sort by another column error

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.
BhaveshPatel Super Contributor
Super Contributor

Re: Sort by another column error

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.
bullius Member
Member

Re: Sort by another column error

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.

 

?

KGrice Established Member
Established Member

Re: Sort by another column error

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

bullius Member
Member

Re: Sort by another column error

Thanks! That works perfectly.

Sokon Member
Member

Re: Sort by another column error

Works great, thanks! Would be great if there was a solution using only DAX, though.

benswift Regular Visitor
Regular Visitor

Re: Sort by another column error

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!

 

vanessa Member
Member

Re: Sort by another column 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?

benswift Regular Visitor
Regular Visitor

Re: Sort by another column error

Could you specify what you're trying to do?

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Users Online
Currently online: 60 members 1,326 guests
Please welcome our newest community members: