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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
thmonte
Helper IV
Helper IV

Multi level custom sort

I have a database that looks like the following:

 

 

NameRankPromotion DatePromotion Number
Person AI1/1/20171
Person BI1/1/20161
Person CII1/10/20151
Person DII1/10/20152
Person EII10/10/20011
Person FIII10/1/20155
Person GIII10/1/20145

 

I am creating a table visual in my report and I would like to have a multi-level sort on this visual and I can't seem to figure it out.  I also want a dynamic number in the visual for each row that is pretty much a "global" rank based on the filter.  My report will never be ran with multiple "Rank" selected but it could have multiple Promotion Dates.  If I were to do the sort in Excel this is what it would look like.

 

11-2-2017 8-35-45 AM.jpg

 

So after would the full table would like similar to below.  Now if I were to filter out just Promotion Date = 1/10/2015, Person C would turn into Visual Number 1 and and Person D would have Visual Number 2

 

NameRankPromotion DatePromotion NumberVisual Number
Person BI1/1/201611
Person AI1/1/201712
Person EII10/10/200111
Person CII1/10/201512
Person DII1/10/201523
Person GIII10/1/201451
Person FIII10/1/201552

 

I've tried RANKX but I don't believe its doing the sorts properly.  Following this thread: http://community.powerbi.com/t5/Desktop/How-to-Rank-a-list-based-on-2-values-double-rankX/m-p/44076#...

 

Any help would be appreciated!  Thanks!

1 ACCEPTED SOLUTION
Eric_Zhang
Microsoft Employee
Microsoft Employee


@thmonte wrote:

I have a database that looks like the following:

 

 

Name Rank Promotion Date Promotion Number
Person A I 1/1/2017 1
Person B I 1/1/2016 1
Person C II 1/10/2015 1
Person D II 1/10/2015 2
Person E II 10/10/2001 1
Person F III 10/1/2015 5
Person G III 10/1/2014 5

 

I am creating a table visual in my report and I would like to have a multi-level sort on this visual and I can't seem to figure it out.  I also want a dynamic number in the visual for each row that is pretty much a "global" rank based on the filter.  My report will never be ran with multiple "Rank" selected but it could have multiple Promotion Dates.  If I were to do the sort in Excel this is what it would look like.

 

11-2-2017 8-35-45 AM.jpg

 

So after would the full table would like similar to below.  Now if I were to filter out just Promotion Date = 1/10/2015, Person C would turn into Visual Number 1 and and Person D would have Visual Number 2

 

Name Rank Promotion Date Promotion Number Visual Number
Person B I 1/1/2016 1 1
Person A I 1/1/2017 1 2
Person E II 10/10/2001 1 1
Person C II 1/10/2015 1 2
Person D II 1/10/2015 2 3
Person G III 10/1/2014 5 1
Person F III 10/1/2015 5 2

 

I've tried RANKX but I don't believe its doing the sorts properly.  Following this thread: http://community.powerbi.com/t5/Desktop/How-to-Rank-a-list-based-on-2-values-double-rankX/m-p/44076#...

 

Any help would be appreciated!  Thanks!


@thmonte

The functions RANKX does't support order by multiple columns, as a workaround, you can try to create a calculated column as below and then rank sorting by that column

sortColumn =
LEFT ( CONCATENATE ( yourTable[Rank], "HHHHHHHHH" ), 10 ) & "-"
    & FORMAT ( yourTable[Promotion Date], "YYYY-MM-DD" )
    & "-"
    & RIGHT ( CONCATENATE ( "000", yourTable[Promotion Number] ), 4 )

Capture.PNG

 

Then create a measure as below so that you can sort by that measure to get the expected sorting order.

rankOverAll = RANKX(ALLSELECTED(yourTable),CALCULATE(MAX(yourTable[sortColumn])),,ASC,Dense)

Capture.PNG

 

For the visual number, you can create a measure as 

Visual Number = 
RANKX (
    FILTER(ALLSELECTED(yourTable),yourTable[Rank]=MAX(yourTable[Rank])),
    CALCULATE ( MAX ( yourTable[sortColumn]) ),
    ,
    ASC,
    DENSE
)

See more details in the attached pbix file.

View solution in original post

5 REPLIES 5
Eric_Zhang
Microsoft Employee
Microsoft Employee


@thmonte wrote:

I have a database that looks like the following:

 

 

Name Rank Promotion Date Promotion Number
Person A I 1/1/2017 1
Person B I 1/1/2016 1
Person C II 1/10/2015 1
Person D II 1/10/2015 2
Person E II 10/10/2001 1
Person F III 10/1/2015 5
Person G III 10/1/2014 5

 

I am creating a table visual in my report and I would like to have a multi-level sort on this visual and I can't seem to figure it out.  I also want a dynamic number in the visual for each row that is pretty much a "global" rank based on the filter.  My report will never be ran with multiple "Rank" selected but it could have multiple Promotion Dates.  If I were to do the sort in Excel this is what it would look like.

 

11-2-2017 8-35-45 AM.jpg

 

So after would the full table would like similar to below.  Now if I were to filter out just Promotion Date = 1/10/2015, Person C would turn into Visual Number 1 and and Person D would have Visual Number 2

 

Name Rank Promotion Date Promotion Number Visual Number
Person B I 1/1/2016 1 1
Person A I 1/1/2017 1 2
Person E II 10/10/2001 1 1
Person C II 1/10/2015 1 2
Person D II 1/10/2015 2 3
Person G III 10/1/2014 5 1
Person F III 10/1/2015 5 2

 

I've tried RANKX but I don't believe its doing the sorts properly.  Following this thread: http://community.powerbi.com/t5/Desktop/How-to-Rank-a-list-based-on-2-values-double-rankX/m-p/44076#...

 

Any help would be appreciated!  Thanks!


@thmonte

The functions RANKX does't support order by multiple columns, as a workaround, you can try to create a calculated column as below and then rank sorting by that column

sortColumn =
LEFT ( CONCATENATE ( yourTable[Rank], "HHHHHHHHH" ), 10 ) & "-"
    & FORMAT ( yourTable[Promotion Date], "YYYY-MM-DD" )
    & "-"
    & RIGHT ( CONCATENATE ( "000", yourTable[Promotion Number] ), 4 )

Capture.PNG

 

Then create a measure as below so that you can sort by that measure to get the expected sorting order.

rankOverAll = RANKX(ALLSELECTED(yourTable),CALCULATE(MAX(yourTable[sortColumn])),,ASC,Dense)

Capture.PNG

 

For the visual number, you can create a measure as 

Visual Number = 
RANKX (
    FILTER(ALLSELECTED(yourTable),yourTable[Rank]=MAX(yourTable[Rank])),
    CALCULATE ( MAX ( yourTable[sortColumn]) ),
    ,
    ASC,
    DENSE
)

See more details in the attached pbix file.

What's with the H's in the concatenation?

 

Either way it did work!  Thanks for the help..

 

I just used your first measure in my table visual and it worked dynamically.  Whats the difference between the two measures?


@thmonte wrote:

What's with the H's in the concatenation?

 

Either way it did work!  Thanks for the help..

 

I just used your first measure in my table visual and it worked dynamically.  Whats the difference between the two measures?


I use H's because H is before I in alphabet order, it is for sorting purpose.  The difference of the two measures is, the rankOverAll ranks dynamically in overall ranks(I + II + III..) while Visual Number ranks dynamically in individual ranks(I, II, III...).

Also if I was to put another column in my table called "Location" of these people and then filter the visual multi-selecting locations and it dynamically updated as I selected the locations in the filter would I need to change anything in the DAX?

I didn't think to ever concatenate the fields to one column and use RANKX - Does RANKX just use alphabetical sorting for this?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.