TOPN in DAX: top N customers, products and Others

How TOPN returns the top N rows of a table in DAX, ties that return more rows, revenue and share of the top 10, a dynamic N from a slicer, top N with an Others row, TOPN in queries with ORDER BY, and how it compares with the visual Top N filter.

Short answer: TOPN ( n, table, orderBy [, order] ) returns the first n rows of a table ranked by an expression, by default descending. Wrapped in CALCULATE, it filters a measure to those rows: CALCULATE ( [Revenue], TOPN ( 10, ALLSELECTED ( Customer[Customer] ), [Revenue] ) ) is the revenue of the top 10 customers. Ties at the N-th place return all tied rows, so you can get more than N. The rows are not sorted; sort in the visual or with ORDER BY in a query.

"How much of our revenue comes from the top 10 customers?" is a question about concentration risk, and it needs TOPN, not a filtered visual.

What does TOPN return?

05001,0001,5002,000420Contoso310Fabrikam260Northwind190Adventure Works150Tailspin1,590All others (812 customers)
Revenue of the top 5 customers and the rest (thousands)
Top 5 Revenue =
CALCULATE ( [Revenue], TOPN ( 5, ALLSELECTED ( Customer[Customer] ), [Revenue] ) )

Top 5 Share = DIVIDE ( [Top 5 Revenue], CALCULATE ( [Revenue], ALLSELECTED ( Customer[Customer] ) ) )

Here the top 5 bring 1,330 of 2,920 thousand, a share of 46%.

How do you make N a slicer?

Create a what-if parameter Top N (1 to 50) and use its value:

Top N Revenue =
VAR N = SELECTEDVALUE ( 'Top N'[Top N Value], 10 )
RETURN
    CALCULATE ( [Revenue], TOPN ( N, ALLSELECTED ( Customer[Customer] ), [Revenue] ) )

How do you show top N and Others?

A bar chart with the top customers and one "Others" bar needs a table that contains an "Others" item. Add it to the customer list with a small calculated table:

Customer Names =
UNION ( VALUES ( Customer[Customer] ), ROW ( "Customer", "Others" ) )

Put Customer Names[Customer] on the axis and use:

Revenue Top N and Others =
VAR N = SELECTEDVALUE ( 'Top N'[Top N Value], 10 )
VAR TopList =
    TOPN ( N, ALLSELECTED ( Customer[Customer] ), [Revenue] )
VAR Item = SELECTEDVALUE ( 'Customer Names'[Customer] )
RETURN
    IF (
        Item = "Others",
        CALCULATE ( [Revenue], EXCEPT ( ALLSELECTED ( Customer[Customer] ), TopList ) ),
        IF (
            Item IN SELECTCOLUMNS ( TopList, "C", Customer[Customer] ),
            CALCULATE ( [Revenue], TREATAS ( { Item }, Customer[Customer] ) )
        )
    )

Customer Names has no relationship, so TREATAS applies the name to the real customer column. Customers outside the top N return blank and disappear from the chart.

How do you use TOPN in a query?

EVALUATE
TOPN ( 10, ADDCOLUMNS ( VALUES ( 'Product'[Product] ), "Revenue", [Revenue] ), [Revenue], DESC )
ORDER BY [Revenue] DESC

TOPN picks the rows; ORDER BY sorts them. Without it the order is not guaranteed.

What about the visual Top N filter?

In the filter pane, Filter type: Top N on a field, by a measure, does the same for one visual without DAX. Use DAX TOPN when you need the top N as a number (share of top 10), as an Others row, or inside other measures.

What goes wrong?

  • More than N rows. Ties at the cutoff are all returned; add a tie-breaker to the order, or accept it.
  • VALUES instead of ALLSELECTED. Inside a row of the visual, VALUES ( Customer[Customer] ) has one customer; rank over all of them.
  • Expecting sorted output. Sort in the visual or the query.
  • N of 0 or less. Returns an empty table.

Where can you check this?

Where do you start?

Add Top 5 Share to a card on your customer page: it is a one-number answer to concentration risk. RANKX gives each customer its position instead.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.