Languages Advisor | DAX

Correcting DAX Code to Get Top 5 Products by Total Profit

This explanation provides a correct implementation of DAX code to get a table of sales for the top 5 products based on total profit. It explains why the original code doesn't work and provides the necessary modifications to achieve the desired result...


Empty image or helper icon

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

Prompt

CALCULATETABLE(
    Sales,
    RANKX(ALL(Sales[Product]), [Total Profit], ,Descending) <=5
)

Answer

The DAX code you've provided is attempting to get a table of sales for the top 5 products based on total profit but it will not yield the expected results because RANKX function must be used inside a measure.

RANKX requires a row context to function correctly. However, inside a CALCULATETABLE, there is no inherent row context, thus RANKX will not be able to determine a ranking for each product correctly.

To get your desired result, you need to first create a measure that calculates the total profit.

TotalProfit = SUMX(Sales, Sales[Quantity] * Sales[Unit Price] - Sales[Quantity] * Sales[Unit Cost])

Then calculate a ranking measure for your products based on total profit.

ProductRank = RANKX(ALL(Sales[Product]), [TotalProfit], , DESC, Dense)

This measure will correctly assign a rank to each individual product based on the total profit.

Now, you can create a calculated table where you only include the top 5 products. Here's a table expression for this:

Top5Products = 
FILTER(
    ALL(Sales[Product]),
    [ProductRank] <= 5
)

Now, this will create a new table with just the top 5 products in it, as per the total profit.

Lastly create a measure which will calculate total sales for these top 5 products:

Top5ProductSales = 
CALCULATE(
    SUM(Sales[SalesAmount]),
    FILTER(
        Sales,
        Sales[Product] IN Top5Products[Product]
    )
)

Note: the SalesAmount is an assumed column in the sales table that represents the total sales amount. If the naming is different in your data model, please adjust it accordingly.

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 explanation focuses on correcting the given DAX code that attempts to retrieve a table of sales for the top 5 products based on total profit. The issue with the original code is that it uses the RANKX function incorrectly, as it needs to be used inside a measure. Without a row context, RANKX cannot determine the ranking for each product accurately. The corrected approach involves creating a measure to calculate the total profit and then using RANKX to rank the products based on total profit. A calculated table can then be created to include only the top 5 products. Additionally, a measure is provided to calculate the total sales for these top 5 products.