Forum Discussion
George1973
Helper V
5 years agoVariable as a single measure
Hi, I have a complex measure with some variables: Weighted Average =
VAR OneLessMonthDailySales=DIVIDE(
CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-1,MONTH)),
CALCULATE([...
AlexisOlson
Super User
5 years agoI'm not sure if it's necessarily possible to optimize significantly. Because the context is different each time, you need to evaluate each of the prior months separately. I don't see a way to get around that since you can't actually reuse a computation.
That said, you might be able to get some degree of parallelization if you write it differently. See if this works any better:
Weighted Average =
VAR PriorMonths =
ADDCOLUMNS (
GENERATESERIES ( 1, 5 ),
"MonthDailySales",
DIVIDE (
CALCULATE ( [Sold Prod Qnty], DATEADD ( 'Date'[Date], -[Value], MONTH ) ),
CALCULATE ( [Unique Sales Day Count], DATEADD ( 'Date'[Date], -[Value], MONTH )
)
),
"Weight",
( 7 - [Value] ) / 2
)
VAR WtdAvg =
DIVIDE (
SUMX ( PriorMonths, [Weight] * [MonthDailySales] ),
SUMX ( PriorMonths, [Weight] )
)
RETURN
DIVIDE ( WtdAvg, [Unique Sales Day Count] )George1973
Helper V
5 years agoWOW, very interesting. I will try it and let you know. Thanks a lot