How to create a ranking with RANKX in DAX: step by step
Sorting products, salespeople or regions by performance is one of the most common questions in Business Intelligence. In DAX, the RANKX function lets you build a dynamic ranking that reacts to the report filters, assigning each element its position — 1st, 2nd, 3rd — based on a measure. This tutorial shows, step by step and with an example, how to create a ranking with RANKX in Power BI.
Prerequisites
- Power BI Desktop installed (free version).
- A model with a fact table (e.g.
Vendas) and a dimension (e.g.Produto). - Basic knowledge of DAX measures (how to create a measure with
SUM).
Step 1: Create the base measure
Before ranking, we need something to rank. Let's create a simple measure that sums the sales value. Right-click the Vendas table, choose New measure and type:
Total Vendas = SUM ( Vendas[Valor] )
This measure returns total sales in the current context — by product, by month or overall, depending on the visual where you place it. The ranking will be calculated over this value.
Step 2: Create the ranking with RANKX
Now we create the ranking measure. The idea is to compare each product with all the others and return its position. Create a new measure:
Ranking Produtos =
RANKX (
ALL ( Produto[Nome] ),
[Total Vendas],
,
DESC,
Dense
)
Explaining each argument: ALL ( Produto[Nome] ) returns the full list of products; [Total Vendas] is the value evaluated for each product; the empty argument that follows is the optional value parameter, which we leave blank so RANKX uses the current row's value; DESC puts the highest value in 1st place; and Dense controls how ties are handled (see Step 4).
Step 3: Understand the role of ALL
This is the step where most people slip up. If you write RANKX ( Produto, [Total Vendas] ) without ALL, every product shows a ranking of 1. Why? Because, in a table or matrix, each row is already filtered to a single product — the filter context reduces the table to that one row, and a lone value is always first.
The ALL function removes that filter and returns every product, giving RANKX a complete list to compare against. Rule of thumb: the first argument of RANKX must contain the entire list you want to rank against.
Step 4: Control ties and order
The order argument accepts DESC (highest value = 1st, the most common in a sales ranking) or ASC (lowest value = 1st, useful for cost or time rankings). The last argument defines ties:
- Dense: two products tied in 2nd are both 2nd and the next is 3rd (1, 2, 2, 3).
- Skip: two tied in 2nd are both 2nd and the next jumps to 4th (1, 2, 2, 4).
Choose Dense when you want a continuous ranking with no gaps, and Skip when you want to reflect how many elements finished ahead.
Step 5: Show only the Top 5
A frequent use of ranking is to highlight the best performers. You can reuse the measure to show only the top five products:
Top 5 Vendas =
IF ( [Ranking Produtos] <= 5, [Total Vendas], BLANK () )
Place this measure in a visual and filter out the blank values, and only the Top 5 remains. Because RANKX is dynamic, the ranking recalculates whenever the user applies year, category or region filters.
Check the result
Create a table visual with the Produto[Nome] field and the Total Vendas and Ranking Produtos measures. Sort the ranking column ascending: the product with the most sales should show 1, the second should show 2, and so on. If they all show 1, the ALL is missing from the first argument. Apply a filter (for example, a specific year) and confirm the positions recalculate.
Conclusion
With a single RANKX measure you can already build dynamic rankings that respond to report filters, control the order and manage ties. The natural next step is to combine the ranking with CALCULATE to rank within categories (for example, the Top 3 of each product family) using ALLEXCEPT. What will be the first ranking you build in your report — products, salespeople or regions?