The RANKX function in DAX is used to assign a rank or position to each row within a table or group, based on the value of an expression — for example, ranking products by sales, employees by performance, or customers by revenue.
The basic syntax is:
RANKX(<table>, [, [, [, ]]])
Here’s what each part means:
<table>→ The table or list of values you want to rank.<expression>→ The calculation used for ranking (e.g., SUM of sales).<value>→ The value to rank (optional; often omitted).<order>→ The sorting order —DESC(default) for highest-to-lowest, orASCfor lowest-to-highest.<ties>→ Defines how to handle ties (whether to skip or repeat ranks).
Example:
Suppose I want to rank products by their total sales:
Product Rank =
RANKX(
ALL(Products[ProductName]),
CALCULATE(SUM(Sales[Revenue])),
,
DESC,
SKIP
)
This formula removes any filters on Products (using ALL), calculates total revenue for each product, and then ranks them in descending order. “SKIP” means if two products have the same sales value, the next rank is skipped (e.g., 1, 2, 2, 4).
Where I’ve applied it:
In one of my Power BI dashboards for sales analysis, I used RANKX to show the “Top 10 Products by Revenue” dynamically. This helped sales managers quickly identify which products were leading and which were underperforming.
Challenges faced:
When using RANKX inside a visual with filters (like region or category), ranks sometimes recalculated within that subset, giving inconsistent global rankings. To fix this, I used the ALL() function to ignore filters when I wanted a global rank and ALLEXCEPT() when I wanted ranking within a category.
Limitation:
The RANKX function can be computationally expensive on large datasets, especially when the expression involves complex calculations like CALCULATE(SUMX(...)). In those cases, I sometimes pre-aggregate data in Power Query or create summary tables to improve performance.
In short:
RANKX is a powerful function that dynamically assigns ranks based on expressions and filters. It’s flexible and widely used for leaderboard-style visuals in Power BI — provided we handle filters and performance carefully.
