Forum Discussion
Kopek
Helper IV
1 year agoVariable changes the outcome
Hi! I have built the following measure, and wanted to otimize it with variable, but ufortunately it does not work as expected. When variable is used i am missing totals from the table, and the o...
DataNinja777
Super User
1 year agoHi Kopek ,
The issue with your optimized measure using variables is likely related to how Power BI handles evaluation contexts and the SUMMARIZE function. When using variables inside SUMMARIZE, their context can become detached from the rows you’re trying to calculate totals for. Shown below is a solution that optimizes the logic while addressing the missing totals and adding the additional filter you mentioned.
MeasureVar =
VAR Calc = [Current] - [Demand] * 7 + [Average] * 7
VAR PriceAvg = AVERAGE(Table[Price])
RETURN
CALCULATE(
-1 * SUMX(
FILTER(
SUMMARIZE(
Query,
Query[Flag],
Products[ID],
"Total",
IF(
Calc >= 0,
0,
Calc * PriceAvg
)
),
[Total] <> BLANK()
),
[Total]
),
Calendar[Today] = TRUE
)
By calculating variables outside SUMMARIZE, we ensure the correct context is passed. Additionally, SUMMARIZE now only focuses on grouping and returning the necessary values, while CALCULATE manages the filtering logic effectively.
Best regards,