·

Power BI RANKX: Rank by Any Measure with Field Parameters

·

Your manager wants a “Top customers” table. Then she asks: “Can I see the top customers by margin? And by units? And by growth?”

Four tables? Four rank measures? No.

Short answer: build one rank measure with RANKX (or RANK) over ALLSELECTED, and feed it the measure the user picks in a field parameter slicer. A small SWITCH translates the slicer selection into a value. One table, one slicer, one rank.

Let’s build it on my demo model, step by step.

1. A basic RANKX measure that actually works

Start simple: rank customers by Sales Amount.

Customer Rank =
IF (
ISINSCOPE ( Customers[Customer] )
&& NOT ISBLANK ( [Sales Amount] ),
RANKX (
ALLSELECTED ( Customers[Customer] ),
[Sales Amount],
,
DESC,
DENSE
)
)

Three small details make the difference:

  • ALLSELECTED ( Customers[Customer] ) ranks against all customers visible in the visual, so it still respects your year or region slicers.
  • ISINSCOPE hides the rank on the total row. A total ranked “1” confuses everyone.
  • NOT ISBLANK ( [Sales Amount] ) matters more than it looks. Without it, the rank returns a number for customers with no sales, and Power BI shows them in the table. In my demo, filtered to 2025, that’s 150 rows instead of the 148 customers who actually bought something.

Result for 2025: North Finance is #1 with $584,271, Green Motors #2, Rapid Systems #3.

Bonus: put Customers[Segment] in the table too and the rank restarts in every segment. Same measure, no changes. The row filter on Segment stays, and ALLSELECTED only clears the customer.

2. Create a field parameter for the measures

Now let the user choose the measure.

  1. Modeling > New parameter > Fields.
  2. Name it Rank By.
  3. Add Sales Amount, Margin, Units Sold and Sales YoY % from _Measures.
  4. Keep Add slicer to this page checked.

Power BI creates a small table. Behind it is this DAX:

Rank By =
{
( "Sales Amount", NAMEOF ( '_Measures'[Sales Amount] ), 0 ),
( "Margin", NAMEOF ( '_Measures'[Margin] ), 1 ),
( "Units Sold", NAMEOF ( '_Measures'[Units Sold] ), 2 ),
( "Sales YoY %", NAMEOF ( '_Measures'[Sales YoY %] ), 3 )
}

The third column (I call it Rank By Order) is the one we need. Remember the numbers: 0, 1, 2, 3.

3. Turn the slicer selection into a value

Here is the catch most people hit on the forum. A field parameter changes what a visual shows. A measure can’t just “read” the selected measure and calculate with it.

What a measure can read is the order number of the selected row. So we map it back with SWITCH:

Rank By Value =
SWITCH (
SELECTEDVALUE ( 'Rank By'[Rank By Order] ),
0, [Sales Amount],
1, [Margin],
2, [Units Sold],
3, [Sales YoY %],
[Sales Amount]
)

The last line is the default. If nothing (or more than one item) is selected, we rank by sales. Set the slicer to single select, it makes life easier.

4. Rank by the selected measure

Now the rank measure uses Rank By Value instead of a fixed measure:

Customer Rank (Dynamic) =
VAR _Value = [Rank By Value]
RETURN
IF (
ISINSCOPE ( Customers[Customer] )
&& NOT ISBLANK ( _Value ),
RANKX (
FILTER (
ALLSELECTED ( Customers[Customer] ),
NOT ISBLANK ( [Rank By Value] )
),
[Rank By Value],
_Value,
DESC,
DENSE
)
)

Why the FILTER with NOT ISBLANK? Because of Sales YoY %.

Customers without sales last year have a blank YoY. RANKX treats blank like zero. So all those customers land in the middle of the ranking, and every customer with negative growth gets pushed down.

I checked this on the demo model for 2025. Clear Systems has -0.3% growth. The correct rank is 66. Without the filter it’s 112, because 46 customers with blank YoY sit in front of it.

Build the visual:

  • Table with Customers[Customer], the Rank By field parameter (so the value column switches too) and Customer Rank (Dynamic).
  • Sort by the rank.
  • For a Top 10: in the Filters pane, add Customer Rank (Dynamic) to the visual filter, is less than or equal to 10.

Click “Margin” in the slicer and the table re-ranks. Rapid Systems moves from #3 by sales to #2 by margin. Green Motors drops from #2 to #5.

5. A “Top 1” card

Same idea, one more measure. This returns the name of the number one customer for the selected measure:

Top Customer =
VAR _Top =
TOPN (
1,
FILTER (
ALLSELECTED ( Customers[Customer] ),
NOT ISBLANK ( [Rank By Value] )
),
[Rank By Value], DESC
)
RETURN
CONCATENATEX ( _Top, Customers[Customer], ", " )

TOPN returns all ties, so CONCATENATEX lists them together instead of picking one at random.

In 2025, North Finance wins on sales, margin and units. Switch to “Sales YoY %” and the card shows Atlas Systems. Why? It went from $486 to $38,460. That’s +7,814%. Ranking by growth loves small numbers, so pair it with a minimum sales filter if that matters to your business.

I used the new Card visual for this. If you haven’t met it yet: Where Is the Multi-row Card? How to Use the New Card Visual in Power BI.

6. The same thing with RANK

Since 2023 DAX also has the RANK window function. Same result, a bit more readable:

Customer Rank (RANK) =
IF (
ISINSCOPE ( Customers[Customer] )
&& NOT ISBLANK ( [Rank By Value] ),
RANK (
DENSE,
FILTER (
ALLSELECTED ( Customers[Customer] ),
NOT ISBLANK ( [Rank By Value] )
),
ORDERBY ( [Rank By Value], DESC )
)
)

On the demo model both versions return exactly the same ranks for all four measures. Use the one your team reads more easily.

Want to check it yourself? Run this in DAX query view:

EVALUATE
SUMMARIZECOLUMNS (
Customers[Customer],
TREATAS ( { 2025 }, 'Date'[Year] ),
TREATAS ( { 1 }, 'Rank By'[Rank By Order] ),
"Value", [Rank By Value],
"RANKX", [Customer Rank (Dynamic)],
"RANK", [Customer Rank (RANK)]
)
ORDER BY [RANKX]

Common mistakes

  • Every row shows rank 1. The table inside RANKX is only the current customer. Use ALLSELECTED ( Customers[Customer] ) (or ALL), not VALUES.
  • Customers without sales appear in the table. The rank measure returns a number for them. Return blank when the value is blank.
  • Trying to reference the field parameter as a measure. A measure can’t calculate “the selected field”. Use SELECTEDVALUE on the order column and a SWITCH.
  • Ranking a percentage with blanks. Blanks rank like zero and break the order. Filter them out before ranking.

FAQ

How do I rank a measure in Power BI?
Use RANKX ( ALLSELECTED ( Table[Column] ), [Measure], , DESC, DENSE ), wrapped in ISINSCOPE so the total stays blank.

Can RANKX use a field parameter?
Not directly. Map the selected item to a measure with SWITCH ( SELECTEDVALUE ( 'Parameter'[Order] ), ... ) and rank that measure.

What is the difference between RANKX and RANK in DAX?
RANKX evaluates an expression over a table. RANK is a window function with ORDERBY and handles ties and blanks with clear options. For this case they return the same result.

How do I show only the Top 10 by rank?
Add the rank measure to the visual-level filter with “is less than or equal to 10”. Or use the Top N filter type on the customer field, by the Rank By Value measure.

Wrap up

One field parameter, one SWITCH, one rank. Your users get “top customers by anything” without four copies of the same table.

Ranking pairs nicely with visual tricks too. Check bar charts with markers and conditional formatting for a way to highlight the top performers.

Ranking questions are all over the Fabric Community forum. This one has 13K+ views: DAX Measure for Ranking the valid rows.

Nemanja Andić, Power BI consultant and trainer

Nemanja Andić

Power BI consultant and trainer, Microsoft certified, Fabric Community Super User. I write about the problems I solve in real projects.

About me · LinkedIn