Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

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:

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)

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


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

  • 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.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi bhanu_gautam,

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

      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..