Stop 09 · Advanced DAX PatternsTopics · 4/10

TOPN

Part of the Power BI Roadmap

In short

Managers love "top 5": top 5 products, top 5 stores, top 5 customers. TOPN returns exactly those rows as a table, which you can then add up, count or compare with everything else.

TOPN returns the top N rows of a table by an expression, like the five best-selling products. It returns a table, so you usually wrap it in CALCULATE or an iterator.

The jaadu lifts five shiny trophies off a long shelf of smaller cups and carries them in a basket.

Think of it like this

Picking the class toppers

The principal says "Send me the top five students by marks." The teacher sorts the register and tears off the first five names. TOPN is that torn-off list. On its own it's just a list, so you still need to do something with it, like add up their marks.

Watch it happen

How a TOPN measure works

All products
Score each
Sort
Keep top 5
CALCULATE

1/5ALL ( Product[Product Name] ) lists every product name.

The big ideas

What you need to know

Returns a table

TOPN ( 5, table, expression ) gives rows, not a number. Use it as a CALCULATE filter or inside SUMX, COUNTROWS or CONCATENATEX.

Order matters

DESC (default) gives the highest. Use ASC for the bottom five, like slowest-moving items.

Ties can add rows

If several rows tie at the cut-off, all of them are returned. Add a tie-breaker expression if you need exactly N.

Visual filter alternative

For a simple chart, the Top N option in the Filters pane is quicker. Use TOPN when you need the number in a measure.

See it in code

Walk through the code

dax
1Top 5 Products Sales =2CALCULATE (3    [Total Sales],4    TOPN ( 5, ALL ( Product[Product Name] ), [Total Sales], DESC )5)6 7Top 5 Share % =8DIVIDE ( [Top 5 Products Sales], [Total Sales] )9 10Top 5 Product List =11CONCATENATEX (12    TOPN ( 5, ALL ( Product[Product Name] ), [Total Sales], DESC ),13    Product[Product Name],14    ", ",15    [Total Sales], DESC16)

1/3TOPN builds the five-row table, and CALCULATE uses it as a filter on product names.

Sales from the top five products, their share of the total and a text list for a card.

Save yourself the pain

Common mistakes

Oops

Putting TOPN directly in a card as a measure.

Do this instead

A measure must return one value. Wrap TOPN in CALCULATE, SUMX, COUNTROWS or CONCATENATEX.

Oops

Using TOPN ( 5, Product, [Total Sales] ) on a visual that already filters by product.

Do this instead

Inside each product row the table holds only that product. Use ALL ( Product[Product Name] ) so all products compete.

Your mission

Top 5 share by region

~20 min

Quick check

Why can TOPN ( 5, ... ) sometimes return six rows?

Go deeper

  1. 1Write Top 5 Products Sales with CALCULATE and TOPN.
  2. 2Compare its result with the built-in Top N visual filter.
  3. 3Check what happens when two products tie at the cut-off.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge