I have a table with columns Product and Month and three measures X1, X2, and X3. I placed Product and Month as Rows and X1, X2, and X3 as Values in matrix visualization in Power BI. I need a DAX measure that is calculated as X1*X2/X3 at the level of Month and as SUMPRODUCT(X1,X2)/X3 at the Product level. SUMPRODUCT is Excel function. I need something similar in DAX.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To achieve the desired calculation in Power BI using DAX, you'll need to create a measure that dynamically adjusts its calculation based on the level of granularity currently being displayed in your matrix visualization (i.e., switching between the Month level and the Product level). This can be accomplished by detecting the context in which the measure is being evaluated and then applying the appropriate calculation.
Below is a DAX measure that should meet your requirements. This measure uses the HASONEFILTER and ISINSCOPE functions to determine the current context (whether the filter context is on Month, Product, or possibly both), and then applies either the simple product division for the Month level or a custom equivalent of SUMPRODUCT divided by X3 for the Product level.
First, ensure you have basic sum measures defined for X1, X2, and X3 if you haven't already:
These measures are essential as they define the basic aggregation for the columns X1, X2, and X3 in your table.
Now, here’s the dynamic measure:
dax
Dynamic Measure =
VAR IsMonthInScope = ISINSCOPE('Table'[Month])
VAR IsProductInScope = ISINSCOPE('Table'[Product])
RETURN
IF(
IsMonthInScope && NOT IsProductInScope, // Only Month is in scope
DIVIDE(
[X1] * [X2],
[X3]
),
IF(
IsProductInScope && NOT IsMonthInScope, // Only Product is in scope
DIVIDE(
SUMX('Table', 'Table'[X1] * 'Table'[X2]),
SUM('Table'[X3])
),
BLANK() // Neither or Both are in scope, return blank or adjust as needed
)
)
How it works:
Variables for Context Detection:
IsMonthInScope checks if the Month column is in the current filter context.
IsProductInScope does the same for the Product column.
Conditional Logic (IF Statement):
First Condition (IsMonthInScope && NOT IsProductInScope):
If only Month is in scope, the measure calculates X1 * X2 / X3 directly using the [X1], [X2], and [X3] measures. These measures aggregate the values of X1, X2, and X3 (e.g., sum) based on their definitions.
Second Condition (IsProductInScope && NOT IsMonthInScope):
If only Product is in scope, it uses SUMX to iterate through each row of the table (in the context of each product), multiplying X1 by X2 for each row and then summing these products. This result is then divided by the sum of X3 across all relevant rows (equivalent to your described SUMPRODUCT(X1, X2)/X3 at the Product level).
Else (Neither or Both in Scope):
Returns BLANK(). You can adjust this to suit your needs if the measure is evaluated in a context where neither or both Month and Product are filtered (e.g., grand total, different level of aggregation, etc.).
Assumptions:
The table name is 'Table'; replace this with your actual table name if different.
Note: The DIVIDE function is used to prevent division-by-zero errors, ensuring that the measure returns BLANK() if the divisor (X3) is zero.