DAX function
TOPN — DAX function reference
Top N rows by an expression.
Syntax
dax
TOPN(<n_value>, <table>, <orderBy_expression>, [<order>], [<orderBy_expression2>, <order2>, …])
Examples
dax
TOPN(5, Product, [Total Sales], DESC)
→ A table
TOPN returns a table, so SUMX re-evaluates the measure per row to total just those top rows.
dax
SUMX(TOPN(3, VALUES(Product[Name]), CALCULATE(SUM(Sales[Amount])), DESC), CALCULATE(SUM(Sales[Amount])))
→ Sum of just the top 3 products' sales
dax
TOPN(5, Product, Product[Category], ASC, Product[Name], ASC)
→ Top 5 rows sorted by two columns, useful when the first column has ties
Common mistakes & gotchas
- TOPN can return more than n rows when there’s a tie for the last spot (unless you add a tiebreaker orderBy expression) — TOPN(3, …) might return 4 or 5 rows if several are tied for 3rd place.
- The order argument matters: DESC (the default) returns the highest values first for a typical "top N," but always specify it explicitly rather than relying on the default.
Related DAX functions
From Tables & set operations