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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Anonymous
Not applicable

Dynamic Top N Parameter doesn't work when adding a LEGEND to a Stacked Bar Chart

Link to pbix: Dynamic TopN with Legend not working.pbix
In my sample, I have a horizontal stacked bar chart (Sales by City) that interacts correctly with the dynamic Top N parameter:

JustParagon_1-1722935259351.png

I used the following rank measure:

Rank_City =
    RANKX(
        ALLSELECTED('Dim_City'[City]),
        [Sales Amount],
        ,
        DESC,
        DENSE
    )

And a measure applied to the chart as a filter to activate the dynamic Top N:
TopNCity =
    IF([Rank_City] <= 'TopN'[TopN Value], 1, 0)
JustParagon_2-1722935395924.png

All is well until adding a sub-category as a legend breaks the dynamic top N:

JustParagon_4-1722935490134.png


How do I make sure the chart still displays the correct number of cities even with a sub-category applied as a legend? I have attached a link to the sample pbix. Thank you!

2 REPLIES 2
bhanu_gautam
Super User
Super User

@Anonymous , Try updating measure as  

Rank_City =
       RANKX(
           ALLSELECTED('Dim_City'[City], 'Dim_SubCategory'[SubCategory]),
           [Sales Amount],
           ,
           DESC,
           DENSE
       )
 
And 
TopNCity =
       IF(
           [Rank_City] <= 'TopN'[TopN Value],
           1,
           0
       )
 
Ensure that the TopNCity measure is applied as a visual-level filter on your chart, and set it to show only values where TopNCity equals 1.




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Anonymous
Not applicable

Hi @bhanu_gautam,

It didn't work as it says that columns used in ALLSELECTED must be from the same table:

JustParagon_0-1722950530390.png

But thank you for the idea because from there, I tried to create a table that summarizes the City and Sub-Category, established a many-to-many relationship with the fact table. I used these columns in the chart and updated the rank measure as well:

Rank_City =
    RANKX(
        ALLSELECTED('Dim_City_Sub'[City],Dim_City_Sub[Sub-Category]),
        [Sales Amount],
        ,
        DESC,
        DENSE
    )
This seems to fix the issue, but the legend doesn't seem correct as all bars look unnatural + they have similar width..
JustParagon_1-1722951156764.png

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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