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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Julia2023
Helper I
Helper I

Dynamic Top N Ranking with Bucket-Based Filter Context

Hi all, I need advice on implementing a dynamic Top N ranking filter in Power BI.

I have 2 visuals on the same page:

  • A matrix that groups products into different sales and profitability buckets (for example, High Sales & High Profit) - Count Products measure rpovided below.
  • A table that shows all individual products with an Actual Sales measure.

Above the product table, there is a Top N slicer (driven by 2&3 supporting measures) that allows users to select how many top products to display.

The issue is the following:
When I select a value in the Top N slicer (for example, Top 5) and then click a specific bucket in the matrix (e.g. High Sales & High Profit), the table still shows the overall Top 5 products, instead of the Top 5 products within the selected bucket.

What I need is for the Top N logic to be context-aware.
For example, if I select:

  • Top 5 products, and
  • The High Sales & High Profit bucket in the matrix,

then the table should return only the top 5 products within that selected bucket, based on the current filter context — not the global Top 5.

At the moment, the ranking ignores the bucket selection, which makes the result misleading.
I would appreciate any guidance on how to make the Top N ranking respond dynamically to the bucket selection.

Thanks in advance.  

Julia2023_0-1776784109285.png

 

 

 

  1. Count Products = VAR SalesBucket =

    SELECTEDVALUE(Sales[Sales Name])

 

VAR ProfitBucket =

    SELECTEDVALUE(Profitability[Profit Name])

 

VAR

        CALCULATETABLE (

            ADDCOLUMNS (

                ALLSELECTED (Product[Product]),

                "@Sales", [Actual Sales $],

                "@SalesCode", [Sales Code (Products)],

                "@ProfitabilityCode", [Profitability Code]

            )

        )

 

VAR

    FILTER(

        SeriesTable,

        [@SalesCode] = SalesBucket &&

        [@ProfitabilityCode] = ProfitBucket

        && [@Sales] <> 0

    )

 

    COUNTROWS(FilterSeriesTable)

 

RETURN

IF(

    ISBLANK(Result),

    "",

    Result

)

 

 

2. Flag Products = VAR _top = SELECTEDVALUE('Slicer'[Parameter])

 

RETURN

IF([Rank Products] <= _top, 1, 0)

 

3. Rank Products = RANKX (ALL('Product '[Product]), [Actual Sales $])

 



5 REPLIES 5
v-sshirivolu
Community Support
Community Support

Hi @Julia2023 ,

 

The issue occurs because the ranking is being calculated across the entire dataset instead of within each bucket context.

To fix this, you need to explicitly preserve the bucket context while ranking.
You can achieve this by modifying the "RANKX"  measure as follows:

Rank Within Bucket =
VAR CurrentBucket = MAX(Data[Bucket])
RETURN
RANKX(
FILTER(
ALL(Data),
Data[Bucket] = CurrentBucket
),
[Total Value],
,
DESC,
DENSE
)

Then create a Top N flag measure - 

Top N Flag =
VAR N = SELECTEDVALUE('TopN Table'[N], 2)
RETURN
IF([Rank Within Bucket] <= N, 1, 0)

Finally, apply this measure as a visual level filter set to 1. This ensures Top N is evaluated within each bucket instead of globally.

Thank you.

danextian
Super User
Super User

Hi @Julia2023 

It would be easier to provide a working solution if a sample PBIX were shared (with confidential data removed), so there would be something concrete to work with.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Ashish_Mathur
Super User
Super User

Hi,

Pease share the download link of the PBI file.  Show the problem and expected result there clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Juan-Power-bi
Resident Rockstar
Resident Rockstar

Hi!!

The problem is your Rank Products measure uses ALL('Product'[Product]) which ignores all filters including the bucket selection. You need it to rank only within the current filter context.
Change it to use ALLSELECTED instead:
daxRank Products =
RANKX(ALLSELECTED('Product'[Product]), [Actual Sales $])
ALLSELECTED respects filters coming from slicers and cross-filtering from other visuals, so when someone clicks a bucket in the matrix it will rank only the products within that filtered set. The Top N flag then naturally applies to that ranked subset.

Thank you. I tried ALLSELECTED, but it’s still not working...

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.