cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Post Patron
Post Patron

Iterative Stock Cover Calculation

Hi all,
There is a very complicated situation i am trying to solve. Actually i tried a lot of thing but i couldn't find a solution yet.
If someone have any idea to solve this please let me know.
I want to make stock coverage calculation.
I have a opening value, i calculated it from another measure.
The opening value is 100. I would look at how much I could cover according to the sales forecast data if there was no production. For the first week, our Sales Forecast Value is  50, and 60 for the second week. I can cover 50 stock up in first week and  50/60 for second week. In this case, the coverage value for the first week will be 1 + 5/6. When I went to the second week, my new stock will be 90 with the calculation of 100 + 40-50 since I made 40 production and made 50 sales in the previous week. I will do the same calculation for this week's coverage calculation. My stock is 90. The forecast for the second week is 60, which can cover the first week. my sales forecast for the third week is 30, I can cover this week. but from now on, I can't cover the next week because my stock is over. The coverage value for this week will be 2 with 1 + 1 calculation. With this calculation method, the coverage value for the 3rd week is 1.5 with the calculation of 1 + 1/2. My last week coverage will be 3/4.

I'm  sharing example data set for my case.
*The bold values are coming from my table, others were calculated


                  Opening Value           ProductionPlan           SalesForecast       Cover
w1                      100                                40                             50                 1.83
w2                        90                                 20                             60                   2
w3                        50                                 10                             30                  1.5
w4                        30                                 0                               40                 0.75

1 ACCEPTED SOLUTION

Accepted Solutions
Super User I
Super User I

Hi @alsasloren

 

Here is an example pbix showing one way to do the calculations.

PBIX link

 

I put a table in the PBIX showing the key figures:image.png

 

 

 

 

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.image.png

 

 

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
    CoverResult

This 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


Owen Auger

Did I answer your question? Mark my post as a solution!

Connect on Twitter
Connect on LinkedIn

View solution in original post

6 REPLIES 6
Super User I
Super User I

Hi @alsasloren

 

Here is an example pbix showing one way to do the calculations.

PBIX link

 

I put a table in the PBIX showing the key figures:image.png

 

 

 

 

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.image.png

 

 

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
    CoverResult

This 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


Owen Auger

Did I answer your question? Mark my post as a solution!

Connect on Twitter
Connect on LinkedIn

View solution in original post

Hello @OwenAuger

 

For this data set it's working fine, but when my data set change cover calculation breakes.

If the Balance Opening Valeu is greater than cumulative sales, it doesn't calculate cover measure.

I'm sharing PBIX Link for this dataset.

Thanks for your helps.

@alsasloren

Thanks for pointing out this issue.

 

The situation you have highlighted is that when the Opening Balance is so high that it is never exhausted by forecast sales, my measure can't find a week where it is exhausted, and returns blank.

 

In this situation, it might make more sense to return the number of weeks remaining, because the Opening Balance will cover at least those weeks.

 

I have updated my file with this measure, and changed the Opening value to 190 as in your latest example.

 

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] )
        )
    )
VAR WeeksRemaining = MAXX ( FutureSales, [WeekIndex] ) + 1
RETURN
    IF ( NOT ISBLANK ( CoverResult ), CoverResult, WeeksRemaining )

Does this make sense as an alternative, or would you want to do something different?

 

Regards,

Owen


Owen Auger

Did I answer your question? Mark my post as a solution!

Connect on Twitter
Connect on LinkedIn
Helper IV
Helper IV

Hi there,

Thanks for this, it helped me solve a problem on my end.  Now i am faced with a new issue, i want to export the results of these measures into a table.  Every time I use the SUMMARIZE function the iteration breaks.  Ahy help on how to extract the visualized table results into another table?

@GeraldZ - 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(...),...).


Owen Auger

Did I answer your question? Mark my post as a solution!

Connect on Twitter
Connect on LinkedIn

Hi Owen, thanks for responding.  I started my own thread on this here:

 

https://community.powerbi.com/t5/Desktop/Extracting-iterative-calculations-in-visualization-to-new-t...

 

i will add sample data here shortly. Thanks for any help you can provide!

 

@OwenAuger 

Helpful resources

Announcements
November Update

Check it Out!

Click here to read more about the November 2020 Updates!

Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

secondImage

Power Platform October Community Highlights

Check out the top community contributors across all of the communities

secondImage

Create an end-to-end data and analytics solution

Learn how Power BI works with the latest Azure data and analytics innovations at the digital event with Microsoft CEO Satya Nadella.

Top Solution Authors
Top Kudoed Authors