Forum Discussion
daily variance in DAX
How do I calculate the variance as below?
---i tried to write it and doesnt work.
Daily Variance =
VAR _maxvalue =
CALCULATE (
MAX ( table[Value] ),
FILTER (
table,
table[Date] < EARLIER ( table[Date] )
)
)
RETURN
table[Value] - _maxvalue
Thanks,
topazz11 I think that the following should work:
Daily Variance = VAR _CurrentDate = MAX( 'Table'[Date] ) VAR _PreviousDate = CALCULATE( MAX( 'Table'[Date] ), 'Table'[Date] = _CurrentDate - 1 ) VAR _CurrentValue = MAX( 'Table'[Value] ) VAR _PreviousValue = CALCULATE( MAX( 'Table'[Value] ), 'Table'[Date] = _PreviousDate ) VAR _Return = IF( _PreviousDate = BLANK(), 0, _CurrentValue - _PreviousValue ) RETURN _Return
5 Replies
- GeraldGEmerick
Memorable Member
topazz11 I think that the following should work:
Daily Variance = VAR _CurrentDate = MAX( 'Table'[Date] ) VAR _PreviousDate = CALCULATE( MAX( 'Table'[Date] ), 'Table'[Date] = _CurrentDate - 1 ) VAR _CurrentValue = MAX( 'Table'[Value] ) VAR _PreviousValue = CALCULATE( MAX( 'Table'[Value] ), 'Table'[Date] = _PreviousDate ) VAR _Return = IF( _PreviousDate = BLANK(), 0, _CurrentValue - _PreviousValue ) RETURN _Return - krishnakanth240
Super User
Hi topazz11
Can you please check this Calculated column and give headsup if it works ,if not please let us know.
Daily Variance =
VAR CurrentDate = table[Date]
VAR CurrentValue = table[Value]VAR PreviousValue =
CALCULATE (
MAX ( table[Value] ),
FILTER (
table,
table[Date] =
CALCULATE (
MAX ( table[Date] ),
FILTER ( table, table[Date] < CurrentDate )
)
)
)RETURN
IF (
ISBLANK ( PreviousValue ),
0,
CurrentValue - PreviousValue
) - Syk
Resident Rockstar
You can do this in a regular measure, sure. BUT! It's a lot easier in a visual calculation!
Right click on your table and hit 'New visual calculation' then versus previous.
You should already have 90% of what you want once you plug in the fields but if you want that first date not to be included make sure it looks like this: - phpimranNew Member
I think this can be easily handled using DATEADD function. Below is the code I have used in my sample dataset to calculate variance vs previous day:
Daily_Variance =VAR CurrentValue = SUM(DailyVar[Daily Value])VAR PreviousValue =CALCULATE(SUM(DailyVar[Daily Value]),DATEADD(DailyVar[Order Date],-1,DAY))RETURNCurrentValue - PreviousValueMy sample data setOrder Date Daily Value 12/10/2025 178 12/11/2025 192 12/12/2025 102 12/13/2025 182 12/14/2025 109 12/15/2025 187 12/16/2025 155 12/17/2025 119 12/18/2025 172 12/19/2025 120 Power BI result set:
I hope this helps.
Thanks.
- cengizhanarslan
Super User
Please check the formula below:
Daily Variance = VAR PrevValue = CALCULATE ( MAX ( 'Table'[Value] ), DATEADD ( 'Table'[Date], -1, DAY ) ) RETURN MAX ( 'Table'[Value] - PrevValue, 0 )