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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Dynamic Rank Measure

I'm trying to create a measure that re-calculates the rank in the power pivot table everytime the user deselects a value or changes the group by row labels. For example, per table below, there are 3 companies rates per quote id with their respective rank in each quote.

QuoteCompanyRateRank
1A5001
1B6002
1C7003
2A7003
2B5001
2C6002

Is there a measure that can recalulcate the rank of each company's rate per quote id if the user deselects company B so that power pivot table would look like this:

QuoteCompanyRateRank
1A5001
1C7002
2A7002
2C6001

Also, can the same measure also recalculate the rank by averaging the rank of each quote id if the user removed quote id from the row labels for the power pivot table so that power pivot table would look like this:

CompanyRank
A1.5
C1.5

Any help would be appreciated.

2 ACCEPTED SOLUTIONS

@Anonymous 

Thank you for sharing the extended data set.  I didn't really like the -1 either and with the extended data set it doesn't work because there are many companies not represented on a given quote and each of them was returning BLANK which is treated as a 0 in the ranking.  This is fine if you do DESC but because we needed ASC it was counting them so the first company in Quote 1 that got a ranking was getting like 24.

What we needed it to filter the list of companies that are getting ranked to just those with Rate so here we go.

Ranking = 
IF (
    ISFILTERED ( Quotes[Quote] ),
    RANKX ( FILTER ( ALL ( Quotes[Company] ), [Rate Total] ), [Rate Total],, ASC ),
    AVERAGEX (
        VALUES ( Quotes[Quote] ),
        RANKX ( FILTER ( ALL ( Quotes[Company] ), [Rate Total] ), [Rate Total],, ASC )
    )
)

This portion of the measure (FILTER ( ALL ( Quotes[Company] ), [Rate Total]) returns all companies that have a rate in the quote context and if the Quote is not visable we still have quote context because we are iterating over the list of quotes then averaging the Ranking.

View solution in original post

Anonymous
Not applicable

@jdbuchanan71 Thanks a lot. Worked like a charm once I replaced All with AllSelected to make sure that the user selects on CompanyName was also taken into consideration by the measure.

View solution in original post

10 REPLIES 10
jdbuchanan71
Super User
Super User

Hello @Anonymous 

I was able to mirror your results with these two measures.

Rate Total = SUM ( Quotes[Rate] )
Ranking = 
IF (
    ISINSCOPE ( Quotes[Quote] ),
    RANKX ( ALLSELECTED ( Quotes[Company] ), [Rate Total],, ASC ),
    AVERAGEX (
        VALUES ( Quotes[Quote] ),
        ( RANKX ( ALLSELECTED ( Quotes[Company] ), [Rate Total],, ASC ) )
    )
)

RankingDynamic.jpg

My sample file is attached for you to look at.

Anonymous
Not applicable

@jdbuchanan71 This is superb. Thanks a lot. Is there an equivalent solution if this needs to be done using PowerPivot Excel?

@Anonymous ,

I believe the measure should work in Excel as well, it's just DAX code.

Anonymous
Not applicable

@jdbuchanan71 Unfortunately, when I type this out in the new measure window, I get the following error:

 

"Failed to resolve name 'ISINSCOPE'. It is not a valid table, variable, or function name."

Ahh, sorry about that.  Try it with ISFILTERED.

Ranking = 
IF (
    ISFILTERED ( Quotes[Quote] ),
    RANKX ( ALLSELECTED ( Quotes[Company] ), [Rate Total],, ASC ),
    AVERAGEX (
        VALUES ( Quotes[Quote] ),
        ( RANKX ( ALLSELECTED ( Quotes[Company] ), [Rate Total],, ASC ) )
    )
)

 That works for me in Excel

RankExcel.jpg

Anonymous
Not applicable

@jdbuchanan71 Very close to being perfect. I noticed in my data that sometimes there is a possibility that one of the quotes may not have a Company C rate but instead a Company D. For example:

QuoteCompanyRate
1A500
1B600
1C700
2A700
2B500
2D600

Unfortunately, the measure doesn't work. It looks like it is ranking from the distinct list of company names:

QuoteCompanyRate TotalRankingCorrect Rank
1A50021
1B60032
1C70043
2A70043
2B50021
2D60032

@Anonymous 

I think we are getting caught by the "blank" company on a quote where each company doesn't exist being ranked #1 but because we don't want to see that we can just shift the rank down.

Ranking:=IF (
    ISFILTERED ( Quotes[Quote] ),
    RANKX ( ALLSELECTED( Quotes[Company] ), [Rate Total],, ASC )-1,
    AVERAGEX (
        VALUES ( Quotes[Quote] ),
        ( RANKX ( ALLSELECTED( Quotes[Company] ), [Rate Total],, ASC ) )-1
    )
)

This gave me correct results across the sample but give it a test on your side.

Anonymous
Not applicable

@jdbuchanan71 It worked like a charm. However, I feel like the "-1" can not be permanently implemented, due to the possibility of more companies rates being included in my data set. Can you think of a more flexible solution? To assist you, I have pasted a masked sample of my real data set below. Thank you so much for all the help so far.

 

QuoteCompanyRate
1C2815
1F3687
1G2654
1J3166
1K2754
1N1916
1O1755
1P3316
2C1672
2G1833
2M1678
2N2343
2O2191
2P3691
2Q2546
2U2096
2V1774
2W2182
2X2479
3B3522
3C2218
3E8585
3F4150
3G8020
3J3192
3K4507
3L3711
3M2263
3N2647
3O2367
3P1543
3U2074
3V1974
3W1741
4A1533
4B1323
4C870
4D1549
4E5188
4F1396
4G1367
4I1306
4J1210
4L1142
4M783
4R994
4S1520
4T773
4U1460
4X1141
5C2014
5D4474
5G2324
5K1910
5L1876
5N1108
5O1015
5V2039
6C912
6G1226
6M678
6N497
7C777
7F1104
7G1037
7R1234
7S764
7T966
7X1119
8G3459
8K989
8N765
9C6066
9D6047
9H7299
9L6595
9N3543
9O3090
10C2019
10G4549
10L3446
10N3732
10O3228
10P2926
11C1314
11G858
11L1674
11M1403
11N1354
11O1245
11U1464
11V2109
11W1783
12C1884
12V2497
13C2675
13D2044
13G1219
13L2191
13P1502
13U1374
13V1431
13W1393

 

 

@Anonymous 

Thank you for sharing the extended data set.  I didn't really like the -1 either and with the extended data set it doesn't work because there are many companies not represented on a given quote and each of them was returning BLANK which is treated as a 0 in the ranking.  This is fine if you do DESC but because we needed ASC it was counting them so the first company in Quote 1 that got a ranking was getting like 24.

What we needed it to filter the list of companies that are getting ranked to just those with Rate so here we go.

Ranking = 
IF (
    ISFILTERED ( Quotes[Quote] ),
    RANKX ( FILTER ( ALL ( Quotes[Company] ), [Rate Total] ), [Rate Total],, ASC ),
    AVERAGEX (
        VALUES ( Quotes[Quote] ),
        RANKX ( FILTER ( ALL ( Quotes[Company] ), [Rate Total] ), [Rate Total],, ASC )
    )
)

This portion of the measure (FILTER ( ALL ( Quotes[Company] ), [Rate Total]) returns all companies that have a rate in the quote context and if the Quote is not visable we still have quote context because we are iterating over the list of quotes then averaging the Ranking.

Anonymous
Not applicable

@jdbuchanan71 Thanks a lot. Worked like a charm once I replaced All with AllSelected to make sure that the user selects on CompanyName was also taken into consideration by the measure.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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