TOPN DAX Function (Table manipulation)

Returns a given number of top rows according to a specified expression.

Syntax

TOPN ( <N_Value>, <Table> [, <OrderBy_Expression> [, [<Order>] [, <OrderBy_Expression> [, [<Order>] [, … ] ] ] ] ] )
Parameter Attributes Description
N_Value

The number of rows to return.

Table
Iterator

An expression that defines the table from which rows are to be returned.

OrderBy_Expression
Row Context
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

Table An entire table or a table with one or more columns.

A table with the top N_value rows of Table or an empty table if N_value is 0 (zero) or less.

Remarks

If there is a tie, in OrderBy_Expression 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 than 0, then TOPN returns an empty table.

TOPN does not guarantee any sort order for the results.

» 5 related articles
» 1 related function

Examples

--  TOPN retrieves the top N items from a table after sorting
--  them by the result of the third argument.
--  Multiple sorting criteria can be provided in further parameters.
EVALUATE
    TOPN ( 
        3, 
        ADDCOLUMNS ( 
            VALUES ( 'Product'[Product Name] ),
            "@Sales Amount", [Sales Amount]
        ),
        [@Sales Amount],
        DESC
    )
ORDER BY [@Sales Amount] DESC
Product Name @Sales Amount
Adventure Works 26″ 720p LCD HDTV M140 Silver 1,303,983.46
A. Datum SLR Camera X137 Grey 725,840.28
Contoso Telephoto Conversion Lens X400 Silver 683,779.95
--  TOPN might return more than the requested rows in presence of ties.
EVALUATE
    TOPN ( 
        3, 
        ADDCOLUMNS ( 
            VALUES ( 'Product'[Product Name] ),
            "@Sales Amount", MROUND ( [Sales Amount], 500000 )
        ),
        [@Sales Amount],
        DESC
    )
ORDER BY [@Sales Amount] DESC

--  Multiple sorting criteria can be provided in further parameters.
EVALUATE
    TOPN ( 
        3, 
        ADDCOLUMNS ( 
            VALUES ( 'Product'[Product Name] ),
            "@Sales Amount", MROUND ( [Sales Amount], 500000 )
        ),
        [@Sales Amount],
        DESC,
        [Product Name], 
        ASC
    )
ORDER BY [@Sales Amount] DESC
Product Name @Sales Amount
Adventure Works 26″ 720p LCD HDTV M140 Silver 1500000
Contoso Projector 1080p X980 White 500000
SV 16xDVD M360 Black 500000
A. Datum SLR Camera X137 Grey 500000
Contoso Telephoto Conversion Lens X400 Silver 500000
Product Name @Sales Amount
Adventure Works 26″ 720p LCD HDTV M140 Silver 1500000
Contoso Projector 1080p X980 White 500000
A. Datum SLR Camera X137 Grey 500000

Related articles

Learn more about TOPN in the following articles:

Related functions

Other related functions are:

Last update: Nov 7, 2024   » Contribute   » Show contributors

Contributors: Alberto Ferrari, Marco Russo, Kenneth Barber

Microsoft documentation: https://docs.microsoft.com/en-us/dax/topn-function-dax

2018-2024 © SQLBI. All rights are reserved. Information coming from Microsoft documentation is property of Microsoft Corp. » Contact us   » Privacy Policy & Cookies

Context Transition

This function performs a Context Transition if called in a Row Context. Click to read more.

Row Context

This expression is executed in a Row Context. Click to read more.

Iterator

Not recommended

The use of this function is not recommended. See Remarks and Related functions for alternatives.

Not recommended

The use of this parameter is not recommended.

Deprecated

This function is deprecated. Jump to the Alternatives section to see the function to use.

Volatile

A volatile function may return a different result every time you call it, even if you provide the same arguments. Click to read more.

Deprecated

This parameter is deprecated and its use is not recommended.

DirectQuery compatibility

Limitations are placed on DAX expressions allowed in measures and calculated columns.
The state below shows the DirectQuery compatibility of the DAX function.

Contribute

Want to improve the content of TOPN? Did you find any issue?
Please, report it us! All submissions will be evaluated for possible updates of the content.


This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.