Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext 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
Hi all, I need advice on implementing a dynamic Top N ranking filter in Power BI.
I have 2 visuals on the same page:
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:
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.
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 $])
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.
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.
Hi,
Pease share the download link of the PBI file. Show the problem and expected result there clearly.
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.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.
| User | Count |
|---|---|
| 48 | |
| 40 | |
| 40 | |
| 20 | |
| 16 |
| User | Count |
|---|---|
| 70 | |
| 67 | |
| 32 | |
| 27 | |
| 26 |