Forum Discussion
Running sum per category
Hi,
I have the following data
| Week | Country | City | Forecasted opening stock | Changes in stock |
| 2.2020 | USA | LA | 194 | 13 |
| 3.2020 | USA | LA | 180 | 27 |
| 4.2020 | USA | LA | 166 | 29 |
| 5.2020 | USA | LA | 110 | 9 |
| 2.2020 | USA | NYC | 157 | 0 |
| 3.2020 | USA | NYC | 132 | 17 |
| 4.2020 | USA | NYC | 136 | 10 |
| 5.2020 | USA | NYC | 155 | 0 |
Stock data for weeks, per cities in US.
I'd like to add "actual" opening stock per each week and city, based on the following logic:
1. For the first week the opening stock equals the forecasted one.
2. For next weeks opening stock = previous week opening stock + previous week changes in stock.
So, the expected output is the following:
| Week | Country | City | Forecasted opening stock | Changes in stock | Opening stock |
| 2.2020 | USA | LA | 194 | 13 | 194 |
| 3.2020 | USA | LA | 180 | 27 | 207 |
| 4.2020 | USA | LA | 166 | 29 | 234 |
| 5.2020 | USA | LA | 110 | 9 | 263 |
| 2.2020 | USA | NYC | 157 | 0 | 157 |
| 3.2020 | USA | NYC | 132 | 17 | 157 |
| 4.2020 | USA | NYC | 136 | 10 | 174 |
| 5.2020 | USA | NYC | 155 | 0 | 184 |
Thank you in advance
Hi, pmfk
Based on your description, I create data to reproduce your scenario. The pbix file is attached in the end.
Table:
Calculated column:
Year = VALUE(MID([Week],3,4)) WeekNum = VALUE(LEFT([Week],1))You may create a measure or a calculated column as below.
Measure:
Opening stock Measure = var minweek = CALCULATE( MIN('Table'[Week]), ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]) ) return IF( MAX([Week])=minweek, MAX([Forecasted opening stock]), CALCULATE( SUM('Table'[Forecasted opening stock]), FILTER( ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]), [Week]=minweek ) )+ SUMX( FILTER( ALL('Table'), [Country]=MAX('Table'[Country])&& [City]=MAX('Table'[City])&& [Year]=MAX('Table'[Year])&& [Week]<MAX('Table'[Week]) ), [Changes in stock] ) )Calculated column:
Opening stock Column = var minweek = CALCULATE( MIN('Table'[Week]), ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]) ) return IF( [Week]=minweek, [Forecasted opening stock], CALCULATE( SUM('Table'[Forecasted opening stock]), FILTER( ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]), [Week]=minweek ) )+ SUMX( FILTER( ALL('Table'), [Country]=EARLIER('Table'[Country])&& [City]=EARLIER('Table'[City])&& [Year]=EARLIER('Table'[Year])&& [Week]<EARLIER('Table'[Week]) ), [Changes in stock] ) )Result:
Best Regards
Allan
If this post helps,then consider Accepting it as the solution to help other members find it faster.
3 Replies
- v-alq-msft
Community Support
Hi, pmfk
Based on your description, I create data to reproduce your scenario. The pbix file is attached in the end.
Table:
Calculated column:
Year = VALUE(MID([Week],3,4)) WeekNum = VALUE(LEFT([Week],1))You may create a measure or a calculated column as below.
Measure:
Opening stock Measure = var minweek = CALCULATE( MIN('Table'[Week]), ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]) ) return IF( MAX([Week])=minweek, MAX([Forecasted opening stock]), CALCULATE( SUM('Table'[Forecasted opening stock]), FILTER( ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]), [Week]=minweek ) )+ SUMX( FILTER( ALL('Table'), [Country]=MAX('Table'[Country])&& [City]=MAX('Table'[City])&& [Year]=MAX('Table'[Year])&& [Week]<MAX('Table'[Week]) ), [Changes in stock] ) )Calculated column:
Opening stock Column = var minweek = CALCULATE( MIN('Table'[Week]), ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]) ) return IF( [Week]=minweek, [Forecasted opening stock], CALCULATE( SUM('Table'[Forecasted opening stock]), FILTER( ALLEXCEPT('Table','Table'[Country],'Table'[City],'Table'[Year]), [Week]=minweek ) )+ SUMX( FILTER( ALL('Table'), [Country]=EARLIER('Table'[Country])&& [City]=EARLIER('Table'[City])&& [Year]=EARLIER('Table'[Year])&& [Week]<EARLIER('Table'[Week]) ), [Changes in stock] ) )Result:
Best Regards
Allan
If this post helps,then consider Accepting it as the solution to help other members find it faster.
- amitchandak
Super User
pmfk , Create a week/date table and week there and join with this table.
Add column to week table
Week Rank = RANKX(all('Date'),'Date'[Week],,ASC,Dense)
Create measures
First Week Forecasted= CALCULATE(sum('Table'[Forecasted]), FILTER(ALLselected('Date'),'Date'[Week Rank]=min('Date'[Week Rank])))
Till Last Week stock Change = CALCULATE(sum('Table'[stock Changes]), FILTER(ALL('Date'),'Date'[Week Rank]<=max('Date'[Week Rank])-1))if(isblank([Last Week stock Change]) ,[First Week Forecasted] , [First Week Forecasted]+[Till Last Week stock Change])
- pmfkNew Member
Thanks!
Till Last Week stock Change = CALCULATE(sum('Table'[stock Changes]), FILTER(ALL('Date'),'Date'[Week Rank]<=max('Date'[Week Rank])-1))
Why is it a measure and not a column?