Forum Discussion
Iterative Stock Cover Calculation
- 7 years ago
Hi Anonymous
Here is an example pbix showing one way to do the calculations.
I put a table in the PBIX showing the key figures:
Generally, when you have Sales/Production data and you want to derive balances at any point in time, you need to create a series of cumulative calculations.
In this case I started with a table pretty much as you described. Opening Value exists only in the first week.
Then created a set of base measures (Opening, Production, Sales) and corresponding cumulative measures.
Opening = SUM ( Forecast[Opening Value] ) Production = SUM ( Forecast[ProductionPlan] ) Sales = SUM ( Forecast[SalesForecast] ) Cumulative Opening = VAR MaxWeek = MAX ( Forecast[Week] ) RETURN CALCULATE ( [Opening], Forecast[Week] <= MaxWeek + 1 ) Cumulative Production = VAR MaxWeek = MAX ( Forecast[Week] ) RETURN CALCULATE ( [Production], Forecast[Week] <= MaxWeek ) Cumulative Sales = VAR MaxWeek = MAX ( Forecast[Week] ) RETURN CALCULATE ( [Sales], Forecast[Week] <= MaxWeek )Then created Closing/Opening balance measures, representing "stock" on hand at the end/start of each week:
Balance Closing = [Cumulative Opening] + [Cumulative Production] - [Cumulative Sales] Balance Opening = VAR MaxWeek = MAX ( Forecast[Week] ) RETURN CALCULATE ( [Balance Closing], Forecast[Week] = MaxWeek - 1 )Then the Cover measure makes use of the above measures.
Cover = VAR BalanceOpening = [Balance Opening] VAR MaxWeek = MAX ( Forecast[Week] ) VAR FutureWeeks = FILTER ( ALL ( Forecast[Week] ), Forecast[Week] >= MaxWeek ) VAR PreviousSalesCumulative = CALCULATE ( [Cumulative Sales], Forecast[Week] < MaxWeek ) VAR FutureSales = GENERATE ( FutureWeeks, VAR SalesInWeek = [Sales] VAR FutureSalesCumulative = [Cumulative Sales] - PreviousSalesCumulative VAR FutureSalesCumulativePre = FutureSalesCumulative - SalesInWeek RETURN ROW ( "WeekIndex", Forecast[Week] - MaxWeek, "SalesInweek", SalesInWeek, "FutureCumulativeSales", FutureSalesCumulative, "FutureCumulativeSalesPre", FutureSalesCumulativePre ) ) VAR CoverResult = MINX ( FutureSales, IF ( [FutureCumulativeSales] >= BalanceOpening && [FutureCumulativeSales] - [SalesInweek] <= BalanceOpening, [WeekIndex] + DIVIDE ( BalanceOpening - [FutureCumulativeSalesPre], [SalesInWeek] ) ) ) RETURN CoverResultThis particular measure finds the earliest week where the Opening Balance is exhausted by future sales, then calculates the fractional week index where this occurs. The FutureSales variable stores the key values used in the calculation, then CoverResult finds the appropriate row and does the final calculation (using MINX). The above code could well be improved!
Regards,
Owen
Anonymous - can you clarify what you're trying to do, and provide sample data from your model? If you want to create a calculated table that groups by certain columns and adds a column containing a measure, you should be able to use SUMMARIZECOLUMNS or ADDCOLUMNS(SUMMARIZE(...),...).
Hi Owen, thanks for responding. I started my own thread on this here:
i will add sample data here shortly. Thanks for any help you can provide!