Sales of the top 3 products with TOPN

Build a table of the three best sellers with TOPN and add up their sales — per category, region or the whole business.

You will learn to

  • Return the top N rows of a table with TOPN
  • Sum a measure over a TOPN table
  • Decide whether the TOPN table should respect the current filters

How it works

`TOPN(n, table, orderBy)` returns a **table**: the n rows of the table with the highest orderBy values. If two rows tie for the last place, both are kept.

Because it returns a table, TOPN pairs naturally with an iterator. `SUMX(TOPN(5, Customer, [Total Sales]), [Total Sales])` finds the five best customers, then adds up their sales.

Which table you rank matters. `Product` respects the current filters, so in the Bikes row the contest is among bikes only. `ALL(Product)` ignores them, so every row would show the same company-wide top 3. In a card with no filters the two give identical results, which is why the mistake is easy to miss.

Practice it hands-on with a real data model and a live report that updates as you type.

Top 5 Customer Sales = SUMX(TOPN(5, Customer, [Total Sales]), [Total Sales])