Languages Advisor | DAX

Creating a Report for Top 5 Product Categories by Total Sales within E...

This guidance explains how to use DAX functions like TOPN, RANKX, CALCULATETABLE, and SUMMARIZE to create a report that shows the top 5 product categories by total sales within each region.


Empty image or helper icon

This Query related with thread "Calculating New Customers with DAX"

Prompt

Given that you have a good understanding of how TOPN and RANKX functions work in DAX, let's see how you can apply them in a real-world scenario. Imagine you are analyzing sales data for a large international company with multiple product categories. Could you write a DAX query that uses both the TOPN and RANKX functions to return a table with the top 5 product categories in each region, ranked by the total sales? Here's a hint: You may need to use nested TOPN and RANKX functions within CALCULATETABLE or SUMMARIZE functions.

Answer

Sure, I can provide guidance on how to solve this problem using DAX. The aim is to create a report that shows the top 5 product categories by total sales within each region. We would use the DAX functions like TOPN, RANKX, CALCULATETABLE, and SUMMARIZE to achieve this.

Firstly, let's explain the functions involved:

  • TOPN: This function returns the top N rows of the specified table or expression.

  • RANKX: This function returns the ranking of a number in a list of numbers for each row in the table argument.

  • CALCULATETABLE: This function evaluates a table expression in a context modified by filters.

  • SUMMARIZE: This function creates a summary of the input table grouped by the specified columns.

Here's a step by step walk-through using these functions:

Step 1: First, Calculate Total Sales

For this, we may use SUM or SUMX function, but to keep it generalized, suppose we've already calculated the Total Sales measure. Let's say you've calculated Total Sales like so:

Total Sales = SUM('Sales'[SalesAmount])

Step 2: Apply RANKX and TOPN Function Using SUMMARIZE

We use the RANKX function within a TOPN function so that it evaluates the RANKX results only for the top 5 sales. This will be within a CALCULATETABLE function to ensure the entire DAX expresssion is calculated correctly.

Here is how the final DAX measure would look like:

Top5ProductsbyRegion = 
CALCULATETABLE(
    ADDCOLUMNS(
        SUMMARIZE(
            'Sales',
            'Sales'[Region],
            'Sales'[Product Category]
        ),
        "Sales Rank", RANKX(
            ALL('Sales'[Product Category]),
            [Total Sales],
            ,
            DESC,
            Dense
        )
    ),
    TOPN( 
        5, 
        VALUES('Sales'[Product Category]),
        [Total Sales],
        DESC
    )
)

In this expression, CALCULATETABLE is used to modify the context in which the DAX expression is evaluated. Within CALCULATETABLE, SUMMARIZE creates a summary table grouped by Region and Product category, then the newly created column "Sales Rank" stores the ranking of Total Sales for each region and product category. The result will then be restricted to the TOPN 5 product categories by Total Sales for each region.

For further learning, I recommend checking out the courses on the Enterprise DNA Platform. They cover these types of advanced DAX topics.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This guidance provides step-by-step instructions on how to create a report that displays the top 5 product categories by total sales within each region using DAX functions. The DAX functions used include TOPN, RANKX, CALCULATETABLE, and SUMMARIZE. The process involves calculating the total sales, applying the RANKX and TOPN functions using SUMMARIZE, and creating a measure that calculates the top 5 products by region. The provided DAX expression can be used as a reference to achieve the desired outcome. Additional learning resources on advanced DAX topics are recommended.