Calculating days of supply using DAX...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
Hi Team,

I'm wondering if it's possible to create a DAX formula that will calculate days of supply, as shown in the example below:


Week 1Week 2Week 3Week 4Week 5Week 6Week 7Week 8
Demand10151258201521
Supply0005000030
Opening Stock5040251358503015
Closing Stock4025135850301524
Days of Supply28.021.014.026.019.012.05.00.0

<tbody>
</tbody>

It is possible to do it in Excel, but it relies on data being arranged in a crosstab in order for the formula to be able to calculate the forward buckets of demand covered.

Bit of a long shot I'm thinking, but I thought I'd ask the question in case anyone has any bright ideas.

Cheers,

Matty
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Interesting, but without knowing the units of each row metric, it would only be a guess. Is demand in units/week?
 
Upvote 0
Interesting, but without knowing the units of each row metric, it would only be a guess. Is demand in units/week?

Hi,

Demand, Supply, Opening Stock and Closing Stock are all the same unit of measure - let's say they're boxes.

Days of Supply is obviously in days, based on how long to Closing Stock lasts versus the future Demand.

Hope this clarifies.

Cheers,

Matty
 
Upvote 0
Hi Matt,

Thanks for replying.

I'm keen to see your suggestion, but the link isn't working. Could you resend it, please?

Cheers,

Matty

Forget the above - the problem was my laptop.

I'll digest the solution and I'll come back with any questions.

Cheers!

Matty
 
Upvote 0
Hi Matt,

This looks a promising start, but it's days I need to calculate, not weeks.

To illustrate how this can be done using standard Excel functions:

Data as follows (occupying range A1:J11):

ProductKey FigureWeek 1Week 2Week 3Week 4Week 5Week 6Week 7Week 8
OrangeDemand10151258201521
OrangeSupply0005000030
OrangeOpening Stock5040251358503015
OrangeClosing Stock4025135850301524
OrangeDays of Supply28.021.014.026.019.012.05.0999.0
AppleDemand55645625501521
AppleSupply0005000030
AppleOpening Stock12512064196338-12-27
AppleClosing Stock12064196338-12-27-18
AppleDays of Supply24.617.610.612.35.30.00.00.0

<tbody>
</tbody>

Formula copied across from C6 to J6 and C11 to I11 as follows:

Code:
=IFERROR(IF(C5<=0,0,(SUMPRODUCT(--(SUBTOTAL(9,OFFSET(D2:$J2,,,,COLUMN(D2:$J2)-COLUMN(D2)+1))<=C5))+LOOKUP(0,SUBTOTAL(9,OFFSET(D2:$J2,,,,COLUMN(D2:$J2)-COLUMN(D2)+1))-D2:$J2-C5,(C5-(SUBTOTAL(9,OFFSET(D2:$J2,,,,COLUMN(D2:$J2)-COLUMN(D2)+1))-D2:$J2))/D2:$J2))*7),999)

Taking C11 as an example, the formula breaks down as follows...

Calculate the number of 'complete weeks' covered by the Closing Stock:

Code:
SUMPRODUCT(--(SUBTOTAL(9,OFFSET(D7:$J7,,,,COLUMN(D7:$J7)-COLUMN(D7)+1))<=C10))

Which returns 3 (56+45+6 = 107).

Now, identify the first 'part week' covered by the Closing Stock, and calculate how much of that week is covered:

Code:
LOOKUP(0,SUBTOTAL(9,OFFSET(D7:$J7,,,,COLUMN(D7:$J7)-COLUMN(D7)+1))-D7:$J7-C10,(C10-(SUBTOTAL(9,OFFSET(D7:$J7,,,,COLUMN(D7:$J7)-COLUMN(D7)+1))-D7:$J7))/D7:$J7)

Which returns 0.52 (120-107 = 13; 13/25 = 0.52).

The two are then added together (3.52) and multiplied by 7 to get total days (24.64).

A basic IF formula is used at the start to account for negative Closing Stock and the whole thing is wrapped in IFERROR to handle things when the Closing Stock exceeds the forward Demand.

Also, you'll see that there could be more than one product in the table, hence any solution would need to also consider this.

Hope this clarified what I'm trying to achieve and that it's possible to replicate the same logic using DAX.

Cheers,

Matty
 
Upvote 0
Hi Matty,

Matt alerted me to this post.
Here is a Power Pivot version that gives you the Days of Supply per Product (and aggregates if you are filtered on multiple Products).
https://www.dropbox.com/s/07gf1qxzfc8tfo3/Days Stock with Products.xlsx?dl=0

Note: I structured the data in 4 tables: Supply, Demand, Week, Product. Changing this would change the DAX of course.

The Days of Supply measures end up looking like this. You could combine them into a single measure if you wanted.
Code:
[B]Days of Supply[/B]
=
SUMX ( 
    VALUES ( 'Product'[Product] ),
    [Days of Supply Core Measure]
)

Code:
=
[B]Days of Supply Core Measure[/B]
VAR CurrentStock = [Closing Stock]
VAR MaxWeek =
    MAX ( Week[Week] )
VAR FutureWeeks =
    CALCULATETABLE ( VALUES ( Week[Week] ), Week[Week] > MaxWeek, ALL ( Week ) )
VAR StockExhaustionWeek =
    FIRSTNONBLANK (
        FutureWeeks,
        VAR CandidateFutureWeek = Week[Week]
        VAR FutureDemand =
            CALCULATE (
                SUM ( Demand[Demand] ),
                Week[Week] <= CandidateFutureWeek,
                Week[Week] > MaxWeek,
                ALL ( Week )
            )
        RETURN
            IF ( FutureDemand >= CurrentStock, 1 )
    )
VAR DaysSupply =
    IF ( CurrentStock < 0, 0,
        IF (
            NOT ( ISEMPTY ( StockExhaustionWeek ) ),
            VAR Fraction =
                    VAR FutureDemandToExhaustionWeek =
                    CALCULATE (
                        SUM ( Demand[Demand] ),
                        Week[Week] <= StockExhaustionWeek,
                        Week[Week] > MaxWeek,
                        ALL ( Week )
                    )
                VAR ExcessDemand = FutureDemandToExhaustionWeek - CurrentStock
                VAR DemandInExhaustionWeek =
                    CALCULATE ( [Demand Quantity], StockExhaustionWeek, ALL ( Week ) )
                RETURN
                    DIVIDE ( DemandInExhaustionWeek - ExcessDemand, DemandInExhaustionWeek )
            RETURN
                7
                    * ( StockExhaustionWeek - MaxWeek
                    + Fraction
                    - 1 )
        )
    )
RETURN
    DaysSupply

Also I used the convention that if Days of Supply is infinite then return blank. Of course you could change this to 999 as in your example.

Cheers
Owen :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,411
Members
448,894
Latest member
spenstar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top