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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
BenEaton
Frequent Visitor

"Sort by column" - from a related table

Hi all,

 

I'm looking to sort a column, but by an order defined in another table.  Specifically, I have military ranks that display alphabetically:

 

Capt

Maj

Sgt

SSgt etc...

 

...But naturally, wish to display them by a defined order which I have placed into a "Reference" table:

 

Reference:

Rank Reference.png

 

 

 

 

 

 

 

 

 

 

Even though there is a relationship between "Rank" in the source table and "Rank" in the reference data, I can't sort the data by the "RankOrder" column.  I can only sort by columns that already exist in the source data.  I do not want to edit the source data directly as I work from lots of different spreadsheets that all share this "Rank" column, so need a method of sorting.

 

I tried a calculated column of this format:

RankOrder1 = LOOKUPVALUE('Reference'[RankOrder],'Reference'[Rank],'Source Table'[Rank]) , which adds the correct number next to each value, but when I try sorting by this it displays the error:

 

"Sort by another column error:  This column can't be sorted by a column that is already sorted, directly or indirectly, by this column."

 

 

Any help would be greatly appreciated.

 

Ben

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @BenEaton,

 

Now that you have created a one to many relationship between  reference table and source table, then you can use this formula to add calculated column: RankOrder1 = RELATED(Reference[RankOrder]). Then, click the "sort by column" button, select RankOrder1. In the visualisations, slicers etc, you will have the correct sorting.

 

If you have any question, please feel free to ask.

 

Best regards,
Yuliana Gu

 

 

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
benny_sal
Regular Visitor

just try PBI after company switch from Tableau (too expensive for them), I just find it more easy way in PBI Nov 2017 version like this:

1. Sort "Rank" in Reference table by "RankOrder", and connected to main table with correct relationship.

2. just use "Rank" from Reference table (not Rank in main table) in your chart/table, it will sorted by RankOrder, it's work and I'm already try it.

Sorry for any misstype, english is not my native, over all PBI is very poor compare to Tableau, but we get what we pay

Benny Sal

v-yulgu-msft
Employee
Employee

Hi @BenEaton,

 

Now that you have created a one to many relationship between  reference table and source table, then you can use this formula to add calculated column: RankOrder1 = RELATED(Reference[RankOrder]). Then, click the "sort by column" button, select RankOrder1. In the visualisations, slicers etc, you will have the correct sorting.

 

If you have any question, please feel free to ask.

 

Best regards,
Yuliana Gu

 

 

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you, I spent a frustrating amount of time trying to figure out if a table from a query could be merged with a table from a DAX calculation. I don't understand why Google couldn't tell me that. Oh that's right, the ensh*ttification of the internet. Anyway, I tried all the other online things - creating a relationship, etc and it won't sort, but that wasn't the question I asked anyway. Thanks for helping. I guess I can mark your answer as correct? I'm not sure that's right, but I'm happy to if it helps you out with points.

Hi @Greg_Deckler and @v-yulgu-msft,

 

Didn't have time to check to post sample data, by the time it was solved!  the RELATED function worked perfectly, many thanks @v-yulgu-msft.

 

 

Greg_Deckler
Super User
Super User

In your visualization, use the Rank column from your reference table and set the Sort By to your RankOrder column. If there is a relationship between your tables based on Rank, then it shouldn't matter that you use the column from your reference table versus your fact table and then you will have the correct sorting.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

I feel like I did this in a prior file and it worked and it doesn't work in my current file. Do the reference tables have to be pre-sorted?

thanks!

Hi @Greg_Deckler,

 

Thanks for the tip, but just tried that to no effect, the visualisations, slicers etc all remain alphabetical!  It's one to many relationship from reference to source data (if that matters).

 

Can you post some sample data or your PBIX file so that I can take a look or recreate your issue?


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.