top of page
Search
Writer's picturePowerBI Zone

DAX TOPN Function in Power BI

Usage of TOPN : Returns the top N rows of the specified table.

Syntax of TOPN: TOPN(<N_Value>, <Table>, <OrderBy_Expression>, [<Order>[, <OrderBy_Expression>, [<Order>]]…])


ParameterDefinitionN_ValueThe number of rows to return.

Table Any DAX expression that returns a table of data from where to extract the top 'n' rows. OrderBy_ExpressionAny DAX expression where the result value is used to sort the table Order(Optional) A value that specifies how to sort OrderBy_Expression values:


- 0 (zero) or FALSE. Sorts in descending order of values of Order.


- 1 or TRUE. Hence It can be ASC or DESC

Example 1: It can be used to create a table as shown below without specifying the order

Top 5 Rows (DB) = 
TOPN(
    5,financials)

Example 2: It can be used to create a table as shown below with specifying the order

Top 5 Rows (Sales) = 
TOPN(
        5,
    financials,
    financials[ Sales],ASC
)

Example 3: It can be used to refer to an Virtual table created by Power BI

Top 3 Countries (Sales) = 
TOPN(
    3,
    SUMMARIZE(
        financials,
        financials[Country],
        "Sales",Sum(financials[ Sales])
        ),
    [Sales],
    DESC
    ) 

Example 4: The TOPN Dax Function can be used to create a measure as shown below

TOP 2 Countries(Avg Sales) = 
AVERAGEX(
            TOPN(
                2,
            SUMMARIZE(
                    financials,
                    financials[Country],
                    "Sales1",Sum(financials[ Sales])
                    ),
            [Sales1]),
            [Sales1]

)


Output of this measure is a average line as shown in the pic below:

TOPN Usage as a measure in Power BI

For more do watch the below video



3 views0 comments

Recent Posts

See All

Comentarios


Post: Blog2_Post
bottom of page