Forum Discussion
Anonymous
4 years agoNot applicable
Previous rows value based on date (Lag functionality from SQL in Power BI logic)
Hi, I am confused about how to achieve the values from previous row in Power BI (which means the lag functionality in SQL) I have a table where the cumulative_inventory is already calculated based ...
- 4 years ago
Hi Anonymous ,
You can try formula like below:
M_Pre = VAR pre_ = CALCULATE ( MAX ( 'Table'[Date] ), FILTER ( ALL ( 'Table' ), 'Table'[Date] <= MAX ( 'Table'[Date] ) - 1 ) ) RETURN CALCULATE ( MAX ( 'Table'[Volumn] ), FILTER ( ALL ( 'Table' ), 'Table'[Date] = pre_ ) )If converted into a calculated column formula, you can use the top function to get the value of any of the first few lines (or summation). Specific reference to the following formula:
Column = VAR pre_row = TOPN ( 1, FILTER ( 'Table', 'Table'[Month] < EARLIER ( 'Table'[Month] ) && 'Table'[Employee] = EARLIER ( 'Table'[Employee] ) ), [Month], DESC ) VAR pre_val = MINX ( pre_row, [Sales] ) RETURN 'Table'[Sales] + pre_valIf the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.
Best Regards,
Henry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
tamerj1
Community Champion
4 years agoHi Anonymous
This is the solution assuming calculated columns https://www.dropbox.com/t/0zUQp0PAocSjazNy
Beginning_Inv =
VAR CurrentPlant_MaterialTable =
CALCULATETABLE ( Short_Ship, ALLEXCEPT ( Short_Ship,Short_Ship[Plant], Short_Ship[Material_ID] ) )
VAR CurrentDate =
Short_Ship[Planned_Dates]
VAR PreviousTable =
FILTER ( CurrentPlant_MaterialTable, Short_Ship[Planned_Dates] < CurrentDate )
VAR PreviousDate =
MAXX ( PreviousTable, Short_Ship[Planned_Dates] )
VAR PreviousRecord =
FILTER ( PreviousTable, Short_Ship[Planned_Dates] = PreviousDate )
RETURN
MAXX ( PreviousRecord, Short_Ship[Cumulative_Inv] )