0
0
Power-biHow-ToBeginner ยท 3 min read

How to Use RANKX in DAX in Power BI: Syntax and Examples

Use the RANKX function in DAX to rank values in a table or expression dynamically. It requires a table and an expression to rank, and optionally supports ties and sorting order. For example, RANKX(ALL('Table'), 'Table'[Sales]) ranks sales across all rows.
๐Ÿ“

Syntax

The RANKX function ranks each row in a table based on an expression's value. It has these parts:

  • Table: The table to rank over.
  • Expression: The value to rank by.
  • [Value]: Optional, the value to rank for the current row.
  • [Order]: Optional, 0 for descending (default), 1 for ascending.
  • [Ties]: Optional, Skip (default) or Dense to handle ties.
DAX
RANKX(<Table>, <Expression>, [Value], [Order], [Ties])
๐Ÿ’ป

Example

This example ranks products by their total sales in descending order. It uses ALL to ignore filters and rank all products.

DAX
Product Rank = RANKX(ALL('Sales'[Product]), CALCULATE(SUM('Sales'[Amount])))
Output
If 'Product A' has sales 1000, 'Product B' 700, and 'Product C' 700, ranks will be: Product A = 1, Product B = 2, Product C = 2 (ties handled by default Skip).
โš ๏ธ

Common Pitfalls

  • Not using ALL or a similar function to remove filters can cause ranks to be calculated only within the current filter context.
  • Forgetting to specify Order leads to unexpected ascending or descending ranks.
  • Ignoring ties can cause gaps in ranking numbers.
DAX
/* Wrong: ranks only within filtered context */
Rank Wrong = RANKX('Sales', SUM('Sales'[Amount]))

/* Right: ranks across all products */
Rank Right = RANKX(ALL('Sales'[Product]), CALCULATE(SUM('Sales'[Amount])))
๐Ÿ“Š

Quick Reference

ParameterDescriptionDefault
TableTable to rank overRequired
ExpressionValue to rank byRequired
ValueValue for current row (optional)Current row value
Order0 = Descending, 1 = Ascending0 (Descending)
Ties'Skip' or 'Dense' ranking for ties'Skip'
โœ…

Key Takeaways

Use RANKX to assign ranks dynamically based on an expression over a table.
Use ALL() inside RANKX to rank across all data ignoring filters.
Specify order to control ascending or descending ranking.
Handle ties with 'Skip' or 'Dense' options to avoid gaps or duplicates.
Always test ranking results to ensure correct filter context.