Forum Discussion
Getting Previous Row using DAX measure
- 3 years ago
Hi,
Thank you for your sharing.
Could you please try the below if it works?
Prev_value_Energy_consum = VAR Index = [Index] VAR Prev_datetime = MAXX ( FILTER ( ALLSELECTED ( FloatTable ), FloatTable[Date] = SELECTEDVALUE ( FloatTable[Date] ) && FloatTable[Time] < SELECTEDVALUE ( FloatTable[Time] ) ), FloatTable[Time] ) VAR Prev_date = MAXX ( FILTER ( ALLSELECTED ( FloatTable ), FloatTable[Date] < SELECTEDVALUE ( FloatTable[Date] ) ), FloatTable[Date] ) VAR Prev_time = MAXX ( FILTER ( ALLSELECTED ( FloatTable ), FloatTable[Date] = Prev_date ), FloatTable[Time] ) RETURN SWITCH ( TRUE (), NOT ISBLANK ( Prev_datetime ), CALCULATE ( SUMX ( FloatTable, VAR Index = FloatTable[TagIndex] RETURN FloatTable[Val] ), FILTER ( ALLSELECTED ( FloatTable ), FloatTable[Time] = Prev_datetime && FloatTable[Date] = SELECTEDVALUE ( FloatTable[Date] ) && FloatTable[TagIndex] = Index ) ), CALCULATE ( SUMX ( FloatTable, VAR Index = FloatTable[TagIndex] RETURN FloatTable[Val] ), FILTER ( ALLSELECTED ( FloatTable ), FloatTable[Time] = Prev_time && FloatTable[Date] = Prev_date && FloatTable[TagIndex] = Index ) ) )
Hi,
Thank you for your sharing.
Could you please try the below if it works?
Prev_value_Energy_consum =
VAR Index = [Index]
VAR Prev_datetime =
MAXX (
FILTER (
ALLSELECTED ( FloatTable ),
FloatTable[Date] = SELECTEDVALUE ( FloatTable[Date] )
&& FloatTable[Time] < SELECTEDVALUE ( FloatTable[Time] )
),
FloatTable[Time]
)
VAR Prev_date =
MAXX (
FILTER (
ALLSELECTED ( FloatTable ),
FloatTable[Date] < SELECTEDVALUE ( FloatTable[Date] )
),
FloatTable[Date]
)
VAR Prev_time =
MAXX (
FILTER ( ALLSELECTED ( FloatTable ), FloatTable[Date] = Prev_date ),
FloatTable[Time]
)
RETURN
SWITCH (
TRUE (),
NOT ISBLANK ( Prev_datetime ),
CALCULATE (
SUMX ( FloatTable, VAR Index = FloatTable[TagIndex] RETURN FloatTable[Val] ),
FILTER (
ALLSELECTED ( FloatTable ),
FloatTable[Time] = Prev_datetime
&& FloatTable[Date] = SELECTEDVALUE ( FloatTable[Date] )
&& FloatTable[TagIndex] = Index
)
),
CALCULATE (
SUMX ( FloatTable, VAR Index = FloatTable[TagIndex] RETURN FloatTable[Val] ),
FILTER (
ALLSELECTED ( FloatTable ),
FloatTable[Time] = Prev_time
&& FloatTable[Date] = Prev_date
&& FloatTable[TagIndex] = Index
)
)
)
Hi Jihwan_Kim
Thankyou so much measure is working correct,
but there is still one small issue remaining when I am trying to subtract the value from previous value using calculated column, then It is working fine but at same point of time when time difference b/w 2 consecutive rows is greater than 10 minutes it is showing same value rather than subtracting.
I think this could beacuse we've used switch function in the measure and calculted column is confused which value to take, I may not correct I am just thinking out loud.
Here is the sample file for the same : https://drive.google.com/drive/folders/1651tuMrez13NX48mrdh_UVSZuk7diGMP?usp=sharing
Thanks and Regards
Mihir
- Jihwan_Kim3 years agoSuper User
Hi,
Could you please try writing a measure like below, instead of creating Calcualted Column?
Test measure: = SUM(FloatTable[Val]) - [Prev_value_Energy_consum]- Anonymous3 years agoNot applicable
Hi Jihwan_Kim
Yeah It is working in measure, but I am prefering calculated column beacuse I need Sum of this and in measure it is giving incorrect value, or I am not sure to fix it in measure..
Can you help me with calculated column or measure which would aggregate same value as in calculated column?
Thanks and Regards
Mihir- Jihwan_Kim3 years agoSuper User
Hi,
Please check the below DAX formula for creating a calcualted column.
It will take quite a long time to load the newly created column, but I could not optimize it for now. (On my computer, it took more than 3 mintues to load a calcualted column).
My suggestion is still creating a measure to show the same result.
Thanks.
Energy_consum CC = VAR _configtable = FILTER ( FloatTable, FloatTable[TagIndex] = EARLIER ( FloatTable[TagIndex] ) ) VAR Prev_datetime = MAXX ( FILTER ( _configtable, FloatTable[Date] = EARLIER ( FloatTable[Date] ) && FloatTable[Time] < EARLIER ( FloatTable[Time] ) ), FloatTable[Time] ) VAR Prev_date = MAXX ( FILTER ( _configtable, FloatTable[Date] < EARLIER ( FloatTable[Date] ) ), FloatTable[Date] ) VAR Prev_time = MAXX ( FILTER ( _configtable, FloatTable[Date] = Prev_date ), FloatTable[Time] ) RETURN FloatTable[Val] - SWITCH ( TRUE (), NOT ISBLANK ( Prev_datetime ), MAXX ( FILTER ( _configtable, FloatTable[Time] = Prev_datetime && FloatTable[Date] = EARLIER ( FloatTable[Date] ) ), FloatTable[Val] ), MAXX ( FILTER ( _configtable, FloatTable[Time] = Prev_time && FloatTable[Date] = Prev_date ), FloatTable[Val] ) )