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?
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] DESCTOPN 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?
- Microsoft Learn: TOPN function (DAX)
- Microsoft Learn: Use what-if parameters to visualize variables
- Microsoft Learn: EXCEPT function (DAX)
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.