Forum Discussion
Calculating stock based on weekly changes
- 4 years ago
See if this works for you. (BTW, there was an inactive relationhip in the model you sent, which needs to made active)
Since it's not clear whether you wish to see the value as the initial or the final stock of the week...
Initial weekly Stock = VAR _Current = CALCULATE ( LASTNONBLANKVALUE ( 'WeekYear Start'[Year-Week], [Sum Stock] ), ALL ( 'WeekYear Start' ) ) VAR _RunningStock = CALCULATE ( [Sum Weekly Changes], FILTER ( ALL ( 'WeekYear Start' ), 'WeekYear Start'[Index] >= MAX ( 'WeekYear Start'[Index] ) ) ) RETURN _Current - _RunningStockOr...
Final weekly Stock = VAR _Current = CALCULATE(LASTNONBLANKVALUE('WeekYear Start'[Year-Week], [Sum Stock]), ALL('WeekYear Start')) VAR _RunningStock = CALCULATE([Sum Weekly Changes], FILTER(ALL('WeekYear Start'), 'WeekYear Start'[Index] >= MAX('WeekYear Start'[Index]))) VAR _PW = _RunningStock - [Sum Weekly Changes] RETURN _Current - _PWOr both even
Btw, I the table for the current stock will only ever have one value per product, you can probably use a simple CALCULATE([Sum Stock], ALL(Weekyear Start)) instead of the CALCULATE(LASTNONBLANK... in the _Current variables
I've attached the sample PBIX file
So I thought I solved it, but I realized one mistake that I cannot fix.
First table is current stock, second table are stock movements and the measure on the top of the picture is used in that table and it works fine.
Thing is I realized that the last week's value in the top table already contains the stock movement. So the running total (running total is a separate measure) should calculate all the selected weeks except for the last week.
I tried modifying the measure like this but it results in the last table 🙂
Can you please help me? danextian PaulDBrown Sample file is attached below.
See if this works for you. (BTW, there was an inactive relationhip in the model you sent, which needs to made active)
Since it's not clear whether you wish to see the value as the initial or the final stock of the week...
Initial weekly Stock =
VAR _Current =
CALCULATE (
LASTNONBLANKVALUE ( 'WeekYear Start'[Year-Week], [Sum Stock] ),
ALL ( 'WeekYear Start' )
)
VAR _RunningStock =
CALCULATE (
[Sum Weekly Changes],
FILTER (
ALL ( 'WeekYear Start' ),
'WeekYear Start'[Index] >= MAX ( 'WeekYear Start'[Index] )
)
)
RETURN
_Current - _RunningStock
Or...
Final weekly Stock =
VAR _Current = CALCULATE(LASTNONBLANKVALUE('WeekYear Start'[Year-Week], [Sum Stock]), ALL('WeekYear Start'))
VAR _RunningStock = CALCULATE([Sum Weekly Changes], FILTER(ALL('WeekYear Start'), 'WeekYear Start'[Index] >= MAX('WeekYear Start'[Index])))
VAR _PW = _RunningStock - [Sum Weekly Changes]
RETURN
_Current - _PW
Or both even
Btw, I the table for the current stock will only ever have one value per product, you can probably use a simple CALCULATE([Sum Stock], ALL(Weekyear Start)) instead of the CALCULATE(LASTNONBLANK... in the _Current variables
I've attached the sample PBIX file
- Petanek3334 years agoHelper III
Paul, I really appreciate your help. Thank you very much!