RANKX and TOPN

Which product sells best, which region is in third place? RANKX assigns ranks, TOPN returns the best rows of a table, for example as a filter for the sales of the top 3.

In short: RANKX(ALL(Product[Product]), [Total Sales]) returns the product's rank among all products; the largest value has rank 1. TOPN(3, table, expression) returns the three best rows of the table.

Before this: SUMX and iterators.

Try it now

Task Create the measure "Rank": each product's rank by [Total Sales], the product with the highest sales has rank 1.

Sales 120 rows, 7 columns
OrderIDOrderDateShipDateProductIDRegionIDQuantityAmount
13 Oct 20266 Oct 20261221798
29 Nov 202519 Nov 20252351145
320 Sep 202627 Sep 2026544596
416 Jun 20252 Jul 20252151145
526 Mar 202631 Mar 20261343596
64 Dec 202510 Dec 2025545745
78 Jul 202526 Jul 2025614196
820 Feb 20251 Mar 202532359.7
99 Dec 202524 Dec 202562298
1012 Aug 202625 Aug 2026212458
117 Sep 202612 Sep 2026542298
122 Mar 202614 Mar 2026623147

Showing the first 12 of 120 rows.

Measures: [Total Sales]

Product 6 rows, 4 columns
ProductIDProductCategoryPrice
1LaptopHardware899
2MonitorHardware229
3MouseAccessories19.9
4KeyboardAccessories39.9
5Office suiteSoftware149
6AntivirusSoftware49
Region 4 rows, 2 columns
RegionIDRegion
1North
2South
3West
4East
Date 730 rows, 4 columns
DateYearMonthMonthName
1 Jan 202520251Jan
2 Jan 202520251Jan
3 Jan 202520251Jan
4 Jan 202520251Jan
5 Jan 202520251Jan
6 Jan 202520251Jan
7 Jan 202520251Jan
8 Jan 202520251Jan
9 Jan 202520251Jan
10 Jan 202520251Jan
11 Jan 202520251Jan
12 Jan 202520251Jan

Showing the first 12 of 730 rows.

Why ALL belongs in RANKX

RANKX is an iterator: it evaluates the expression for each row of the table and checks where the current cell's value falls. In the Laptop row, however, the product is filtered to Laptop. With RANKX(VALUES(Product[Product]), …) or RANKX(Product, …), RANKX only sees that one product, and every row shows 1. ALL brings all products back into the comparison.

Ties and order

ValuesDefaultwith DENSEwith ASC
10, 10, 51, 1, 31, 1, 22, 2, 1

The full form is RANKX(table, expression, value, order, ties). By default RANKX ranks descending and skips ranks after a tie. RANKX(ALL(Product[Product]), [Total Sales], , DESC, DENSE) assigns ranks without gaps.

TOPN: the best rows as a filter

TOPN returns a table. In CALCULATE it becomes a filter: only the three best products. If there is a tie in last place, TOPN returns all tied rows, so possibly more than three.

Task Create the measure "Top 3 Sales": the sales of the three best-selling products. TOPN returns the three products, CALCULATE uses them to calculate [Total Sales].

Sales 120 rows, 7 columns
OrderIDOrderDateShipDateProductIDRegionIDQuantityAmount
13 Oct 20266 Oct 20261221798
29 Nov 202519 Nov 20252351145
320 Sep 202627 Sep 2026544596
416 Jun 20252 Jul 20252151145
526 Mar 202631 Mar 20261343596
64 Dec 202510 Dec 2025545745
78 Jul 202526 Jul 2025614196
820 Feb 20251 Mar 202532359.7
99 Dec 202524 Dec 202562298
1012 Aug 202625 Aug 2026212458
117 Sep 202612 Sep 2026542298
122 Mar 202614 Mar 2026623147

Showing the first 12 of 120 rows.

Measures: [Total Sales]

Product 6 rows, 4 columns
ProductIDProductCategoryPrice
1LaptopHardware899
2MonitorHardware229
3MouseAccessories19.9
4KeyboardAccessories39.9
5Office suiteSoftware149
6AntivirusSoftware49
Region 4 rows, 2 columns
RegionIDRegion
1North
2South
3West
4East
Date 730 rows, 4 columns
DateYearMonthMonthName
1 Jan 202520251Jan
2 Jan 202520251Jan
3 Jan 202520251Jan
4 Jan 202520251Jan
5 Jan 202520251Jan
6 Jan 202520251Jan
7 Jan 202520251Jan
8 Jan 202520251Jan
9 Jan 202520251Jan
10 Jan 202520251Jan
11 Jan 202520251Jan
12 Jan 202520251Jan

Showing the first 12 of 730 rows.

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Power BI

  1. Create a measure with RANKX and put it in a table with Product.
  2. Sort by rank: click the column header in the visual.
  3. For a top 3 view without a measure, the Filters pane has the filter type "Top N" for a field of the visual.

Common mistakes

Rank 1 everywhere

The table in RANKX is filtered. Almost always, ALL is missing.

Rank 1 in the total row

Total sales are larger than any single value. With IF(HASONEVALUE(Product[Product]), RANKX(…)) the total row stays blank.

FAQ

Can I rank within a category?

Yes: RANKX(ALL(Product[Product]), …) only removes the filter on the product. With category and product in the rows, the category stays filtered and the rank applies within the category.

Up next: time intelligence

Year to date, previous year and previous month.

Next lesson

Write to us

Choose language

Select your preferred language