Prompt
Answer
Formula for Total Sum Calculation
Objective
To create a formula that calculates the total sum of a measure for every year while ignoring product and type dimensions, but still respecting other filters, such as company.
Formula
Using DAX (Data Analysis Expressions) for Power BI, the following formula achieves the desired outcome:
Total_Sum_By_Year =
CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Sales[Product], Sales[Type]),
VALUES(Sales[Year])
)
Explanation of the Formula Components
CALCULATE Function:
- It modifies the filter context to enable more complex calculations.
- In this context, it allows summing the
SalesAmount
, while manipulating which dimensions are considered.
SUM Function:
- This function calculates the total of the
SalesAmount
column from theSales
table.
- This function calculates the total of the
ALL Function:
- This function removes filters from the specified columns, namely
Product
andType
. - By including these columns, the formula ensures the total sum is calculated without taking their values into account.
- This function removes filters from the specified columns, namely
VALUES Function:
- This function returns a table that contains the unique values in the
Year
column. - By using this within the
CALCULATE
function, it maintains year-wise granularity in the output while disregardingProduct
andType
.
- This function returns a table that contains the unique values in the
Practical Example
Assuming we have a Sales
table with the following structure:
SalesAmount
: Numeric amount for each saleProduct
: Product descriptionType
: Sale type (e.g., Online, In-store)Year
: Year of the saleCompany
: Company responsible for the sale
Data Sample
Year | Product | Type | SalesAmount | Company |
---|---|---|---|---|
2021 | A | Online | 100 | ABC Corp |
2021 | B | In-store | 200 | ABC Corp |
2022 | A | Online | 150 | XYZ Inc |
2022 | B | In-store | 100 | XYZ Inc |
2022 | C | Online | 300 | ABC Corp |
Result Interpretation
When you apply the Total_Sum_By_Year
measure with the context filtered to a specific company (e.g., ABC Corp), the results would show:
For 2021:
- Total without product/type filter = 300 (100 + 200)
For 2022:
- Total without product/type filter = 400 (150 + 100 + 300)
This output provides the total sum per year for the selected company, unaffected by the Product
and Type
dimensions.
Conclusion
The DAX formula provided successfully calculates the total sum for each year while ignoring specific dimensions. By ensuring that the total respects other filters like company, it serves as a robust solution in Power BI data models. For further learning on such DAX functions and their applications, I recommend courses on the Enterprise DNA Platform.
Description
This guide provides a DAX formula to calculate the total sales amount by year in Power BI, ignoring product and type filters while considering other filters like company. It includes an explanation and practical usage example.