RANKX DAX Function (Statistical)

Returns the rank of an expression evaluated in the current context in the list of values for the expression evaluated for each row in the specified table.

Syntax

RANKX ( <Table>, <Expression> [, <Value>] [, <Order>] [, <Ties>] )
Parameter Attributes Description
Table
Iterator

A table expression.

Expression
Row Context

An expression that will be evaluated for row of the table.

Value Optional

An expression that will be evaluated in the current context. If omitted, the Expression argument will be used.

Order Optional

The order to be applied. 0/FALSE/DESC – descending; 1/TRUE/ASC – ascending.

Ties Optional

Function behavior in the event of ties. Skip – ranks that correspond to elements in ties will be skipped; Dense – all elements in a tie are counted as one.

Return values

Scalar A single integer value.

The rank number of Value among all possible values of Expression evaluated for all rows of Table numbers.

Remarks

The default value for the Order argument is DESC.
The default value for the Ties argument is SKIP.

» 5 related articles
» 1 related function

Examples

--  RANKX computes the ranking of an expression over a table
--  The expression is evaluated during the iteration over the
--  table and then in the evaluation context of RANKX.
--  The result is the position of the outer evaluation in the 
--  lookup table built during the iteration
DEFINE
    VAR BrandsAndSales =
        ADDCOLUMNS (
            VALUES ( 'Product'[Brand] ),
            "@Amt", [Sales Amount]
        )
EVALUATE
ADDCOLUMNS (
    BrandsAndSales,
    "Rank",
        RANKX (
            BrandsAndSales,
            [@Amt]
        )
)
ORDER BY [@Amt] DESC
Brand @Amt Rank
Contoso 7,352,399.03 1
Fabrikam 5,554,015.73 2
Adventure Works 4,011,112.28 3
Litware 3,255,704.03 4
Proseware 2,546,144.16 5
A. Datum 2,096,184.64 6
Wide World Importers 1,901,956.66 7
Southridge Video 1,384,413.85 8
The Phone Company 1,123,819.07 9
Northwind Traders 1,040,552.13 10
Tailspin Toys 325,042.42 11
--  The third argument of RANKX is useful when the outer 
--  evaluation requires a different expression than the
--  inner one. For example, when ranking an expression over
--  a pre-built lookup table
DEFINE
    VAR SalesLevels =
        SELECTCOLUMNS ( { 6000000, 3000000, 1500000, 750000, 0 }, "@Limit", [Value] )

EVALUATE
ADDCOLUMNS (
    VALUES ( Product[Brand] ),
    "Sales Amount", [Sales Amount],
    "Level", RANKX ( SalesLevels, [@Limit], [Sales Amount] )
)
ORDER BY [Level] ASC
Brand Sales Amount Level
Contoso 7,352,399.03 1
Litware 3,255,704.03 2
Adventure Works 4,011,112.28 2
Fabrikam 5,554,015.73 2
Wide World Importers 1,901,956.66 3
Proseware 2,546,144.16 3
A. Datum 2,096,184.64 3
Southridge Video 1,384,413.85 4
Northwind Traders 1,040,552.13 4
The Phone Company 1,123,819.07 4
Tailspin Toys 325,042.42 5
--  The fourth argument of RANKX specifies the order of ranking
--  it can be DESC (default) or ASC
DEFINE
    VAR BrandsAndSales =
        ADDCOLUMNS ( VALUES ( 'Product'[Brand] ), "@Amt", [Sales Amount] )
EVALUATE
ADDCOLUMNS (
    BrandsAndSales,
    "Rank ASC",  RANKX ( BrandsAndSales, [@Amt], [@Amt], ASC ),
    "Rank DESC", RANKX ( BrandsAndSales, [@Amt], [@Amt], DESC ),
    "Rank (default)", RANKX ( BrandsAndSales, [@Amt], [@Amt] )
)
ORDER BY [@Amt] DESC
Brand @Amt Rank ASC Rank DESC Rank (default)
Contoso 7,352,399.03 11 1 1
Fabrikam 5,554,015.73 10 2 2
Adventure Works 4,011,112.28 9 3 3
Litware 3,255,704.03 8 4 4
Proseware 2,546,144.16 7 5 5
A. Datum 2,096,184.64 6 6 6
Wide World Importers 1,901,956.66 5 7 7
Southridge Video 1,384,413.85 4 8 8
The Phone Company 1,123,819.07 3 9 9
Northwind Traders 1,040,552.13 2 10 10
Tailspin Toys 325,042.42 1 11 11
--  The fifth argument of RANKX specifies the behavior in
--  case of ties. SKIP (default) skips positions, whereas
--  DENSE guarantees a 1-step increment in the ranking
DEFINE
    VAR BrandsAndSales =
        ADDCOLUMNS (
            VALUES ( 'Product'[Brand] ),
            "@Amt", MROUND ( [Sales Amount], 1E6 )
        )
EVALUATE
ADDCOLUMNS (
    BrandsAndSales,
    "Rank SKIP",  RANKX ( BrandsAndSales, [@Amt], [@Amt], DESC, SKIP ),
    "Rank DENSE", RANKX ( BrandsAndSales, [@Amt], [@Amt], DESC, DENSE )
)
ORDER BY [@Amt] DESC
Brand @Amt Rank SKIP Rank DENSE
Contoso 7,000,000 1 1
Fabrikam 6,000,000 2 2
Adventure Works 4,000,000 3 3
Litware 3,000,000 4 4
Proseware 3,000,000 4 4
Wide World Importers 2,000,000 6 5
A. Datum 2,000,000 6 5
Northwind Traders 1,000,000 8 6
The Phone Company 1,000,000 8 6
Southridge Video 1,000,000 8 6
Tailspin Toys 0 11 7

Related articles

Learn more about RANKX in the following articles:

Related functions

Other related functions are:

Last update: Sep 12, 2021   » Contribute   » Show contributors

Contributors: Alberto Ferrari, Marco Russo

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

2018-2021 © 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.

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 RANKX? 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.