Thread

Insights into Store Sales and Simplified DAX Ranking

A comprehensive exploration of store sales growth rankings and innovative methods for simplifying DAX ranking calculations without relying on the EARLIER function.

Empty image or helper icon

Insights into Store Sales and Simplified DAX Ranking

Description

A comprehensive exploration of store sales growth rankings and innovative methods for simplifying DAX ranking calculations without relying on the EARLIER function.

Store Sales Growth Ranking

Tool: Formula Fixer

Created: 07/24/2024

Prompt

StoreRank = 
RANKX(
    ALL(Store[StoreName]), 
    CALCULATE(
        CALCULATE(SUM(Sales[TotalSales]), DATESINPERIOD(Date[Date], LASTDATE(Date[Date]), -1, MONTH))
    ), 
    , 
    DESC, 
    DENSE
)

User Concerns Analysis

The user is concerned that the provided code might not accurately calculate the ranking of stores by their monthly sales growth.

Code Inspection

  1. The primary measure within the RANKX function is nested CALCULATE calls.
  2. The inner CALCULATE statement may not be correctly capturing the sales growth for the past month.
  3. The use of DATESINPERIOD might not align with the intended calculation of monthly sales growth.
  4. The logical flow of capturing the total sales for each store and then ranking them needs careful handling.

Original Code

StoreRank =
RANKX(
    ALL(Store[StoreName]), 
    CALCULATE(
        CALCULATE(SUM(Sales[TotalSales]), DATESINPERIOD(Date[Date], LASTDATE(Date[Date]), -1, MONTH))
    ), 
    , 
    DESC, 
    DENSE
)

Identified Issues

  1. Nested CALCULATE statements may lead to unexpected results.
  2. Incorrect use of DATESINPERIOD might not properly reflect the past month's sales data.
  3. The formula needs to ensure that it calculates the sales difference month-over-month before ranking.

Code Rectification

To accurately rank stores by monthly sales growth, we need to:

  1. Calculate the total sales for the current month.
  2. Calculate the total sales for the previous month.
  3. Compute the growth (current month sales - previous month sales).
  4. Rank the stores based on this computed growth.

Corrected Code

StoreRank = 
VAR CurrentMonthSales = 
    CALCULATE(
        SUM(Sales[TotalSales]), 
        DATESMTD(Date[Date])
    )

VAR PreviousMonthSales = 
    CALCULATE(
        SUM(Sales[TotalSales]), 
        DATESMTD(DATEADD(Date[Date], -1, MONTH))
    )

VAR SalesGrowth = CurrentMonthSales - PreviousMonthSales

RETURN 
    RANKX(
        ALL(Store[StoreName]), 
        SalesGrowth, 
        , 
        DESC, 
        DENSE
    )

Commenting on Corrections

  1. Computation of Current Month Sales: The use of DATESMTD ensures that we only consider sales data for the current month.

    VAR CurrentMonthSales = 
        CALCULATE(
            SUM(Sales[TotalSales]), 
            DATESMTD(Date[Date])
        )
  2. Computation of Previous Month Sales: The DATEADD function is applied with DATESMTD to shift back one month and capture sales for the entire previous month.

    VAR PreviousMonthSales = 
        CALCULATE(
            SUM(Sales[TotalSales]), 
            DATESMTD(DATEADD(Date[Date], -1, MONTH))
        )
  3. Sales Growth Calculation: Subtracting previous month's sales from current month's sales provides the required growth metric.

    VAR SalesGrowth = CurrentMonthSales - PreviousMonthSales
  4. Ranking by Sales Growth: The RANKX function ranks the stores based on the computed SalesGrowth, sorted in descending order, with dense ranking.

    RETURN 
        RANKX(
            ALL(Store[StoreName]), 
            SalesGrowth, 
            , 
            DESC, 
            DENSE
        )

Summary

  • The revised code is structured to first compute the sales for the current and previous months.
  • It then calculates the growth in sales and ranks stores based on this growth.
  • The corrected approach ensures that the data interval is correctly handled, and the ranking reflects the changes in monthly sales accurately.
Simplified DAX Ranking without EARLIER

Tool: Formula Fixer

Created: 07/24/2024

Prompt

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

User Concerns Analysis

The user wants to eliminate the use of the EARLIER function in the DAX formula. The EARLIER function can be complex and less efficient. A more modern approach using variables can simplify the code and improve performance.

Code Inspection

  • The original code using EARLIER is aimed at ranking products within their respective categories based on sales amounts.
  • Using EARLIER makes nesting and reading the code more difficult.
  • Modern DAX techniques involving variables can replace EARLIER, making the code cleaner and more efficient.

Code Rectification

Let's refactor the code to avoid using EARLIER by leveraging variables (VAR) and RANKX.

Original Code

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

Corrected Code

RankedProducts =
VAR SalesWithCategory =
    ADDCOLUMNS(
        Sales,
        "CategorySalesAmount", Sales[SalesAmount],
        "ProductCategory", Sales[Category]
    )
RETURN
ADDCOLUMNS(
    SalesWithCategory,
    "ProductRank",
    RANKX(
        FILTER(
            SalesWithCategory,
            SalesWithCategory[ProductCategory] = Sales[Category]
        ),
        SalesWithCategory[CategorySalesAmount],
        ,
        DESC
    )
)

Explanation of Corrections

  • Variable Declaration:

    • SalesWithCategory: This variable creates a table with additional columns for ProductCategory and CategorySalesAmount to be used within the ranking logic.
    • Benefits include better readability and possibly enhanced performance by storing intermediate results.
  • Replacing EARLIER:

    • The refactored RANKX function uses SalesWithCategory[ProductCategory] directly in a FILTER statement to compare against Sales[Category], removing the need for EARLIER.
  • Impact on Functionality:

    • The revised formula preserves the intended logic of ranking products within categories based on sales amount.
    • By eliminating EARLIER, the code is more intuitive and potentially faster due to reduced context switching.

Summary

The revised code leverages the power of variables to simplify and optimize the ranking logic. Using VAR enhances the readability and maintainability of the DAX code, ensuring it remains efficient and logical without altering the core functionality.