Languages Advisor | DAX

Calculating Top 5 Product Categories by Total Sales in Each Region

This solution provides a step-by-step approach for calculating the top 5 product categories in each region based on their total sales. It involves grouping the data by region and product categories, ranking the product categories by their total sales...


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

To solve this, you can first use the SUMMARIZE function to group by region and product categories, then use the RANKX function to rank each product category by total sales within each region, and finally, use the TOPN function to limit the result to only the top 5 product categories in each region.

Here is a step-by-step approach:

  1. Create a base table using SUMMARIZE

Firstly, use the SUMMARIZE function to group your data by region and product categories while summing up the total sales for each product category within each region.

BaseTable = 
SUMMARIZE(
    SalesTable, 
    SalesTable[Region], 
    SalesTable[Product Category], 
    "Total Sales", SUM(SalesTable[Sales])
)

This will result in a table grouped by 'Region' and 'Product Category' with a corresponding 'Total Sales' for each grouping.

  1. Rank the Product Categories within each Region using RANKX

Next, add a new ranking column to your base table, ranking your product categories by 'Total Sales' within each region.

BaseTableWithRank = 
ADDCOLUMNS(
    BaseTable, 
    "Rank", RANKX(ALLSELECTED(BaseTable[Product Category]), [Total Sales], , DESC)
)

The RANKX function will rank your product categories from the highest 'Total Sales' (rank 1) to the lowest within each region.

  1. Select the Top 5 Product Categories in each Region using TOPN

Finally, filter your ranked base table to only show the top 5 product categories for each region.

FinalTable = 
CALCULATETABLE(
    BaseTableWithRank, 
    TOPN(5, BaseTableWithRank, [Rank])
)

The above TOPN function will sort 'BaseTableWithRank' by 'Rank' and return the top 5 rows from that table for each region.

The CALCULATETABLE function is then used to apply the filter context from TOPN to 'BaseTableWithRank'.

So, your final result will be a table showing the top 5 product categories in each region, ranked by total sales.

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

The solution begins by creating a base table using the SUMMARIZE function to group the data by region and product categories while calculating the total sales for each product category within each region. Then, the RANKX function is used to add a ranking column to the base table, ranking the product categories by their total sales within each region. Finally, the TOPN function is utilized to filter the ranked base table and select the top 5 product categories in each region. The final result is a table displaying the top 5 product categories in each region, sorted by their total sales.