Introduction to the RANKX and TOPN DAX functions in Power BI

by | Power BI

One of the key aspects of analyzing data is to be able to rank or compare different elements based on certain criteria. This is where the RANKX and TOPN functions in Power BI’s Data Analysis Expressions (DAX) come into play.

The RANKX and TOPN functions are powerful tools that allow you to calculate rankings and identify the top N elements in a dataset based on specified criteria. These functions are particularly useful in scenarios where you want to focus on the top-performing products, customers, or other entities, or when you want to identify outliers or underperforming elements.

To use RANKX and TOPN functions in Power BI, you need to understand their syntax, which consists of multiple arguments. These arguments include the expression, value, and order. Understanding these arguments is crucial for applying these functions effectively.

In this article, we’ll provide an overview of the RANKX and TOPN functions, their syntax, and their practical applications in Power BI. We’ll also demonstrate their usage with examples to help you get started with implementing them in your own Power BI projects.

Let’s get into it!

What Are the RANKX and TOPN DAX Functions?

The RANKX and TOPN functions are part of the DAX (Data Analysis Expressions) language used in Power BI. These functions help you rank and sort data based on a particular set of criteria.

In the sections below, we’ll break down the syntax of these functions and explore their applications through examples.

1. RANKX Function

The RANKX function is used to calculate a numerical rank for each row of data in a column, based on the value of a specific expression. This rank can be based on either a single column or a table. It assigns a unique rank value to each row.

The basic syntax of the RANKX function is as follows:

  • Table: The table or table expression that you want to rank.
  • Expression: The expression that you want to rank by. It can be a measure or an expression that results in a single scalar value.
  • Value: The value for which you want to calculate the rank.
  • Order: An optional argument that specifies whether the rank should be calculated in ascending or descending order. The default value is ascending.

2. TOPN Function

The TOPN function is used to return a table with a specified number of rows from the input table. This function is often used to identify the top N values in a dataset.

The basic syntax of the TOPN function is as follows:

  • N: The number of rows to return. This can be a whole number or a reference to a measure that returns a whole number.
  • Table: The table to filter.
  • FilterTable: The table that provides the filtering context.
  • Expression: An expression that results in a scalar value. The table is filtered to include only rows where the expression is true.
  • OrderColumn: The name of the column used for sorting.
  • Order: An optional argument that specifies whether the sorting should be done in ascending or descending order. The default value is 1, indicating ascending order.

Now that we’ve gone over the syntax and structure of the RANKX and TOPN functions, let’s take a look at how they can be applied in practice.

Implementing the TOPN Function in DAX

Prerequisites

  • Assume data is imported and available in a table called Sales.

Using the TOPN Function

-- Define Variables
VAR TopNValue = 5  -- Number of top records to return

-- Create a Top Sales Table
EVALUATE
TOPN(
    TopNValue,        -- Number of top records needed
    Sales,            -- Table from which top records are selected
    Sales[Amount],    -- Column to be sorted for determining top records
    DESC              -- Sort Order (Descending)
)

-- Filter Context Example
-- Create a Top Sales Measure in a Filtered Context
TOP_SALES :=
CALCULATE (
    SUM (Sales[Amount]),
    TOPN (
        TopNValue, 
        Sales, 
        Sales[Amount], 
        DESC
    )
)

Applying the Measure in a Report

  1. Create a table visual.
  2. Add the Salesperson or any relevant dimension field.
  3. Add the TOP_SALES measure.
  4. Apply necessary filters for visual clarity.

Additional TopN Usage Example

Top Products by Sales Amount

-- Table of Top Selling Products
EVALUATE
VAR TopNProduct = 10

RETURN 
TOPN(
    TopNProduct,    
    SUMMARIZE(
        Sales,
        Sales[Product],
        "TotalSales", SUM(Sales[Amount])
    ),    
    [TotalSales],    
    DESC
)

-- Measure for Top Selling Products
TOP_PRODUCTS :=
CALCULATE (
    SUM (Sales[Amount]),
    TOPN (
        TopNProduct,
        SUMMARIZE (
            Sales,
            Sales[Product],
            "SalesAmount", SUM (Sales[Amount])
        ),
        [SalesAmount],
        DESC
    )
)

Conclusion

Utilize these DAX snippets directly in your data model to extract and visualize top N records efficiently. Adjust the variable TopNValue or TopNProduct to suit your data analysis requirements.

Practical Examples Using TOPN in DAX

Example 1: Top 5 Products by Sales

  1. Create a table to display the top 5 products by sales.
  2. Assume you have a FactSales table with columns ProductID and SalesAmount.
  3. Use the following DAX query:
Top5Products =
TOPN(
    5, 
    SUMMARIZE(FactSales, Product[ProductID], "TotalSales", SUM(FactSales[SalesAmount])),
    [TotalSales], 
    DESC
)
  1. Add this table to your report to visualize the top 5 products by sales.

Example 2: Top 3 Performing Salespeople

  1. Assume you have a FactSales table with columns SalespersonID and SalesAmount.
  2. Use the following DAX query:
Top3SalesPeople =
TOPN(
    3, 
    SUMMARIZE(FactSales, Salesperson[SalespersonID], "TotalSales", SUM(FactSales[SalesAmount])),
    [TotalSales], 
    DESC
)
  1. Use this table to display the top 3 performing salespeople in your report.

Example 3: Top 10 Customers by Order Quantity

  1. Assume you have a FactOrders table with columns CustomerID and OrderQuantity.
  2. Use the following DAX query:
Top10Customers =
TOPN(
    10, 
    SUMMARIZE(FactOrders, Customer[CustomerID], "TotalOrders", SUM(FactOrders[OrderQuantity])),
    [TotalOrders], 
    DESC
)
  1. Visualize this data to understand your top 10 customers by order quantity.
How to Use the RANKX and TOPN Functions in Power BI

Exploring the RANKX Function in DAX

Practical Implementation: Using RANKX in DAX

Below, you’ll find practical examples using the RANKX function in DAX to create rank calculations in your data models. Assume you have an existing Power BI report with relevant data imported.

Example 1: Ranking Sales by Product

  1. Create a new measure to rank products based on their sales.

    Product Sales Rank = 
    RANKX(
    ALL('Product'[ProductName]),
    [Total Sales],
    ,
    DESC,
    Dense
    )
    • ALL('Product'[ProductName]): Applies the ranking across all products.
    • [Total Sales]: The measure used for ranking.
    • DESC: Sorting the rankings in descending order.
    • Dense: Method of ranking.

Example 2: Ranking Employees by Performance

  1. Add a new column in your Employee table to rank employees based on performance score.

    Employee Performance Rank = 
    RANKX(
    ALL('Employee'),
    'Employee'[PerformanceScore],
    ,
    DESC,
    Skip
    )
    • ALL('Employee'): Ensures ranking is globally applied.
    • 'Employee'[PerformanceScore]: Column used for ranking.
    • Skip: Method of ranking.

Example 3: Dynamically Ranking Within a Category

  1. Rank products within each category based on sales.

    Product Category Sales Rank = 
    RANKX(
    FILTER(
    ALL('Product'),
    'Product'[Category] = EARLIER('Product'[Category])
    ),
    [Total Sales],
    ,
    DESC,
    Dense
    )
    • FILTER(ALL('Product'), 'Product'[Category] = EARLIER('Product'[Category])): Filters products within the same category.
    • [Total Sales]: Measure for ranking.
    • DESC: Sorting order.
    • Dense: Ranking method.

These examples can immediately be put into practice within Power BI or other DAX-supported tools to provide actionable insights using the RANKX function.

Practical Examples Using RANKX

Example 1: Rank products by total sales

ProductRank = 
RANKX(
    ALL(Product[ProductName]), 
    CALCULATE(SUM(Sales[TotalSales])), 
    , 
    DESC, 
    DENSE
)

Example 2: Rank customers by purchase frequency

CustomerRank = 
RANKX(
    ALL(Customer[CustomerName]), 
    CALCULATE(COUNT(Sales[SalesID])), 
    , 
    DESC, 
    DENSE
)

Example 3: Rank regions by total profit

RegionRank = 
RANKX(
    ALL(Region[RegionName]), 
    CALCULATE(SUM(Sales[TotalProfit])), 
    , 
    DESC, 
    DENSE
)

Example 4: Rank employees by sales within a specific year

EmployeeRank = 
RANKX(
    FILTER(
        ALL(Employee[EmployeeName]), 
        Sales[Year] = 2023
    ), 
    CALCULATE(SUM(Sales[TotalSales])), 
    , 
    DESC, 
    DENSE
)

Example 5: Rank products by average rating

ProductRatingRank = 
RANKX(
    ALL(Product[ProductName]), 
    CALCULATE(AVERAGE(Reviews[Rating])), 
    , 
    DESC, 
    DENSE
)

Comparative Analysis and Choosing the Right Function

Data Definitions

Assume we have the following table called Sales:

  • Columns: Product, Category, SalesAmount, Date

Functions Defined

We will compare TOPN and RANKX functions to choose the right one based on two different scenarios.

Scenario 1: Top 5 Products by SalesAmount

Use TOPN when you need to retrieve the top N records.

Top5Products = 
TOPN(
    5, 
    Sales, 
    Sales[SalesAmount], 
    DESC
)

Scenario 2: Ranking Products by SalesAmount

Use RANKX when you need to rank each product within its category.

RankedProducts = 
ADDCOLUMNS(
    Sales, 
    "ProductRank", 
    RANKX(
        FILTER(
            Sales, 
            Sales[Category] = EARLIER(Sales[Category])
        ), 
        Sales[SalesAmount],
        ,
        DESC
    )
)

Comparison and Selection Table

Based on the comparison from scenarios:

FunctionUse Case
TOPNRetrieve top N records based on a condition
RANKXRank each record based on a condition within a group

Practical Use

Use TOPN when:

  1. You need to show the top 5 products without the context of categories.

Use RANKX when:

  1. You need the rank within context, for example, ranking products within their categories.

Frequently Asked Questions

Frequently Asked Questions

What is the difference between RANKX and TOPN in DAX?

RANKX and TOPN are both DAX functions used in Power BI for ranking data, but they serve different purposes.

RANKX is used to calculate a numerical rank for each row in a table, based on the value of a specific expression. It assigns a unique rank value to each row.

TOPN, on the other hand, is used to return the top N rows of a table, based on a specified order. This function can be used to identify the top-performing items in a dataset.

What is the purpose of the RANKX function in DAX?

The RANKX function in DAX is used to calculate the rank of values in a table, based on a specific expression. It assigns a unique rank value to each row in the table, with the rank value being determined by the order of the expression’s result.

This can be useful in scenarios where you need to identify the top or bottom values in a dataset, or when you want to compare the relative performance of different items in a given context.

Related Posts

Comprehensive Data Analysis using Power BI and DAX

Data Model Discovery Library

An interactive web-based application to explore and understand various data model examples across multiple industries and business functions.