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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
MbProg
Helper II
Helper II

Top N by different parameters

Hello,

I have a dataset with very simple data: ProductID, Region, Value, Category. I wanted to rank the Products by their Value. So I did the following:

Create Measures:

1. Total_Budget = SUM('Projects'[Value])

2. Rank = RANKX(ALLSELECTED(Projects);[Total_Budget];;DESC;Dense)

And it works. When I create a table diagram, they are ranked correctly and when I use a slicer and filter the region, then the data is refreshed correctly:

Unbenannt.png

Now I want to remove the ProductID column from the diagram and ranked again by their values but by the region, so that I get the regions ranked by their values (of course it is the same Value Column as before)

But removing the ProductID column results in a different result not correctly sorted and ranked:

Unbenannt.png

How can I accomplish this? What am I doing wrong?

2 REPLIES 2
Praful_Potphode
Super User
Super User

Hi @MbProg ,

 

try below measure:

Dynamic Rank = 
SWITCH(
    TRUE(),
    ISINSCOPE(Projects[ProductID]), 
        RANKX(
            ALLSELECTED(Projects[ProductID]),
            [Total_Budget],
            ,
            DESC,
            Dense
        ),
    ISINSCOPE(Projects[Region]), 
        RANKX(
            ALLSELECTED(Projects[Region]),
            [Total_Budget],
            ,
            DESC,
            Dense
        ),
    BLANK()
)

Praful_Potphode_0-1767450631923.pngPraful_Potphode_1-1767450677282.pngPraful_Potphode_2-1767450705590.png

 

 

 

 

 

Sample PBIX

Please give kudos or mark it as solution once confirmed.

Thanks and Regards,

Praful

vaibhavmahajan
Advocate I
Advocate I

Hi @MbProg,

I hope you are doing well today 🙂❤️

 

There are two solutions you can use to fix the rank issue

 

Solution 1: Rank by Region explicitly

 

Total_Budget :=
SUM ( Projects[Value] )

 

Rank by Region :=
RANKX (
    ALLSELECTED ( Projects[Region] ),
    [Total_Budget],
    ,
    DESC,
    DENSE
)

 

Key takeaway (important):

  • ALLSELECTED(Projects[Region]) → ranking list = Regions
  • Total_Budget → evaluates Value per Region

Ranking now matches the table granularity

  • Works with slicers
  • Works when ProductID is removed
  • Correct sorting & ranking

Sort: By Rank by Region (ascending) in Table / Visual

 

 

Solution 2: Dynamic Ranking

If you want dynamic behavior (rank by Product when ProductID is present, otherwise by Region):

 

Total_Budget :=
SUM ( Projects[Value] )

 

Dynamic Rank :=
IF (
    ISINSCOPE ( Projects[ProductID] ),
    RANKX (
        ALLSELECTED ( Projects[ProductID] ),
        [Total_Budget],
        ,
        DESC,
        DENSE
    ),
    RANKX (
        ALLSELECTED ( Projects[Region] ),
        [Total_Budget],
        ,
        DESC,
        DENSE
    )
)

 

Key takeaway (important):

RANKX must always rank over the same column(s) used in the visual grouping.

If the visual shows:

Products → rank Products

Regions → rank Regions

 

Final Implementation Screenshot:

IMG_20251226_185217.jpg

 

 

 

 

 

 

 

 

If this answer helped, kindly give Kudos and mark it as the Accepted Solution

to help other members find it more quickly.

 

Best regards,

Vaibhav Mahajan

LinkedIn: https://www.linkedin.com/in/vaibhavnmahajan

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.