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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

sum top values with rankx without duplicates

I've been searching for a solution to my problem but I can't find it :

I want to get the average of the first top 10 total measure.  

 

I created a rank measure first but I get duplicates rank values :

 

rank total = RANKX(ALL(Table[name]),[total],,DESC,Dense)

 

 

I've tried also TOPN function but I get wrong values because of the duplicates values.

 

Here's an a sample of first 10 rows :

nametotalrank totaldesired rank totalDesired output
a96%1190.6%
b95%2290.6%
c94%3390.6%
d92%4490.6%
e91%5590.6%
f91%5690.6%
g90%7790.6%
h87%8890.6%
i85%9990.6%
j85%91090.6%

 

The desired output is :

 

Top 10 = 90.6 %

 

 

Thanks.

2 ACCEPTED SOLUTIONS
smpa01
Super User
Super User

@Anonymous  you can achieve the end goal with following two measueres

Ranking = 
VAR _1 =
    RANKX (
        ALL ( 'Table'[name] ),
        CALCULATE ( MAX ( 'Table'[total] ) )
            + DIVIDE ( CALCULATE ( UNICODE ( MAX ( 'Table'[name] ) ) ), 1000000 ),
        ,
        DESC
    )
RETURN
    IF ( _1 < 11, _1 )

Top10Average = 
VAR _1 =
    CALCULATE (
        AVERAGEX (
            FILTER (
                ADDCOLUMNS (
                    'Table',
                    "X",
                        RANKX (
                            ALL ( 'Table' ),
                            CALCULATE ( MAX ( 'Table'[total] ) )
                                + DIVIDE ( CALCULATE ( UNICODE ( MAX ( 'Table'[name] ) ) ), 1000000 ),
                            ,
                            DESC
                        )
                ),
                [X] < 11
            ),
            [total]
        ),
        ALL ( 'Table'[name] )
    )
RETURN
    IF ( [Ranking] <> BLANK (), _1 )

pbix is attached

 

smpa01_0-1636997974371.png

 

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

I added one more condition to calculate the ranking, that is the alphabet order.

I multiply it by 0.001. And then add to the [Total]. -> This is what I used as an expression inside the Ranking function and TOPN function.

 

Picture3.png

 

Total: =
IF ( HASONEVALUE ( 'Table'[name] ), SUM ( 'Table'[total] ) )
 
Desired rank: =
IF (
HASONEVALUE ( 'Table'[name] ),
RANKX (
ALL ( 'Table' ),
[Total:]
+ 0.001
* CALCULATE (
RANKX ( ALL ( 'Table' ), CALCULATE ( MAX ( 'Table'[name] ) ),, DESC )
),
,
DESC
)
)
 
Desired output: =
VAR top10table =
TOPN (
10,
ALL ( 'Table' ),
[Total:]
+ 0.001
* CALCULATE (
RANKX ( ALL ( 'Table' ), CALCULATE ( MAX ( 'Table'[name] ) ),, DESC )
), DESC
)
RETURN
IF ( HASONEVALUE ( 'Table'[name] ), AVERAGEX ( top10table, [Total:] ) )
 

 



Microsoft MVP



If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.




LinkedInVisit my LinkedIn page




Outlook BookingSchedule a short Teams meeting to discuss your question



View solution in original post

6 REPLIES 6
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

I added one more condition to calculate the ranking, that is the alphabet order.

I multiply it by 0.001. And then add to the [Total]. -> This is what I used as an expression inside the Ranking function and TOPN function.

 

Picture3.png

 

Total: =
IF ( HASONEVALUE ( 'Table'[name] ), SUM ( 'Table'[total] ) )
 
Desired rank: =
IF (
HASONEVALUE ( 'Table'[name] ),
RANKX (
ALL ( 'Table' ),
[Total:]
+ 0.001
* CALCULATE (
RANKX ( ALL ( 'Table' ), CALCULATE ( MAX ( 'Table'[name] ) ),, DESC )
),
,
DESC
)
)
 
Desired output: =
VAR top10table =
TOPN (
10,
ALL ( 'Table' ),
[Total:]
+ 0.001
* CALCULATE (
RANKX ( ALL ( 'Table' ), CALCULATE ( MAX ( 'Table'[name] ) ),, DESC )
), DESC
)
RETURN
IF ( HASONEVALUE ( 'Table'[name] ), AVERAGEX ( top10table, [Total:] ) )
 

 



Microsoft MVP



If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.




LinkedInVisit my LinkedIn page




Outlook BookingSchedule a short Teams meeting to discuss your question



Anonymous
Not applicable

Thanks @Jihwan_Kim your solution worked too

smpa01
Super User
Super User

@Anonymous  you can achieve the end goal with following two measueres

Ranking = 
VAR _1 =
    RANKX (
        ALL ( 'Table'[name] ),
        CALCULATE ( MAX ( 'Table'[total] ) )
            + DIVIDE ( CALCULATE ( UNICODE ( MAX ( 'Table'[name] ) ) ), 1000000 ),
        ,
        DESC
    )
RETURN
    IF ( _1 < 11, _1 )

Top10Average = 
VAR _1 =
    CALCULATE (
        AVERAGEX (
            FILTER (
                ADDCOLUMNS (
                    'Table',
                    "X",
                        RANKX (
                            ALL ( 'Table' ),
                            CALCULATE ( MAX ( 'Table'[total] ) )
                                + DIVIDE ( CALCULATE ( UNICODE ( MAX ( 'Table'[name] ) ) ), 1000000 ),
                            ,
                            DESC
                        )
                ),
                [X] < 11
            ),
            [total]
        ),
        ALL ( 'Table'[name] )
    )
RETURN
    IF ( [Ranking] <> BLANK (), _1 )

pbix is attached

 

smpa01_0-1636997974371.png

 

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Thanks @smpa01 , worked like a charm 🙂

amitchandak
Super User
Super User

@Anonymous ,
Assume this a measure - total, create a new measures

 

total 1 = [total] +rand()

and


calculate([total],TOPN(10,allselected(table[name]),s[total 1],DESC))

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

thanks @amitchandak , I've tested your code but unfortunately didn't work correctly for different examples. You can check @smpa01 and @Jihwan_Kim answers

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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