TOPN

Updated on

TOPN is a DAX function that returns the top N rows from a table based on a specified expression.

Syntax

TOPN(
   N_Value,
   Table,
   [ OrderBy_Expression, ... ],
   [ Order, ... ]
)
Argument Properties Description
N_Value The number of rows to return.
Table An expression that defines the table from which rows are to be returned.
OrderBy_Expression Optional, Repeatable Expression to be used for sorting the table.
Order Optional, Repeatable The order to be applied. 0/FALSE/DESC – descending; 1/TRUE/ASC – ascending.

Return Values

A table with the top N rows of Table or an empty table if N_Value is 0 (zero) or less. Rows are not sorted in any particular order.

Remarks

  • If there is a tie, in Order_By values, at the N-th row of the table, then all tied rows are returned. Then, when there are ties at the N-th row the function might return more than n rows.

  • If N_Value is 0 (zero) or less, TOPN returns an empty table.

  • TOPN does not guarantee any sort order for the results.

  • This function is not supported for use in DirectQuery mode when used in calculated columns or row-level security (RLS) rules.

Example

The following measure formula returns the top 10 sold products by sales amount.

= SUMX(
        TOPN(
            10, 
            SUMMARIZE(
                    InternetSales, 
                    InternetSales[ProductKey], 
                    "TotalSales", SUM(InternetSales[SalesAmount])
            ),
            [TotalSales], DESC
        ),
        [TotalSales]
)
 

Other functions related to TOPN are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/dax/topn-function-dax

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy