Code Generator | DAX

Total Sales Calculation by Product Type

This DAX function computes total sales based on product types using `SUMX`, `VALUES`, `CALCULATE`, and `SWITCH`. It returns net or gross sales depending on the product type, suitable for Power BI reports.


Empty image or helper icon

Prompt

Total Sales by Product Type =
SUMX(VALUES(Product[ProductTypeId]), Product Types
VAR NETSALES = CALCULATE([NetSales])
RETURN
SWITCH(
Product[ProductTypeId],
2, NETSALES,
1, [GrossSales],
3, [GrossSales],
4, [GrossSales],
5, [GrossSales]))

Answer

DAX Function: Total Sales by Product Type

This DAX function calculates the total sales based on different product types. It uses a combination of SUMX, VALUES, CALCULATE, and SWITCH to compute the required values efficiently.

Function Overview

Total Sales by Product Type = 
SUMX(
    VALUES(Product[ProductTypeId]), 
    VAR NETSALES = CALCULATE([NetSales])
    RETURN
    SWITCH(
        TRUE(),
        Product[ProductTypeId] = 2, NETSALES,
        Product[ProductTypeId] = 1, [GrossSales],
        Product[ProductTypeId] = 3, [GrossSales],
        Product[ProductTypeId] = 4, [GrossSales],
        Product[ProductTypeId] = 5, [GrossSales],
        0 // Default case if no matches
    )
)

Code Explanation

1. SUMX:

This function iterates through each unique product type obtained from VALUES(Product[ProductTypeId]), allowing for row-by-row calculations.

2. VALUES Function:

Extracts distinct values of ProductTypeId to ensure calculations are based on unique categories.

3. VAR NETSALES:

Calculates the net sales for the current row context before evaluating other cases. This avoids recalculating [NetSales] multiple times.

4. RETURN:

Contains the SWITCH function that handles conditional logic based on ProductTypeId.

5. SWITCH:

  • The SWITCH function evaluates the ProductTypeId conditions.
  • For ProductTypeId 2, it returns the value of NETSALES.
  • For ProductTypeId 1, 3, 4, and 5, it returns [GrossSales].
  • A default case (0) is included for cases not matching any types.

Important Considerations

  • Documentation: Ensure any measures like [NetSales] and [GrossSales] are well-defined and computed in your model.
  • Robustness: The function defaults to 0 if the ProductTypeId does not match any specified conditions.

Usage Example

This function can be used in a Power BI report to dynamically calculate total sales by product type. Here's how you might use it:

  1. Create a measure in your data model with the function defined above.
  2. Add a visual (e.g., a table or chart) to your Power BI report.
  3. Drag Product[ProductTypeId] into the visual along with the new measure Total Sales by Product Type.

Example Output

  • For a product with ProductTypeId = 1, the visual will show the associated [GrossSales].
  • For a product with ProductTypeId = 2, it will show the corresponding NETSALES.

Conclusion

This DAX function is scalable and adaptable to changing data models. To further enhance your DAX skills, consider exploring courses on the Enterprise DNA Platform, which offers a variety of resources for mastering DAX and Power BI analysis.

This implementation ensures best practices with efficient and clear DAX code to calculate total sales by product type effectively.

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 DAX function computes total sales based on product types using SUMX, VALUES, CALCULATE, and SWITCH. It returns net or gross sales depending on the product type, suitable for Power BI reports.