Forum Discussion

MarieAmell's avatar
MarieAmell
Frequent Visitor
3 years ago
Solved

Running total compare to same date last year

Hi,

 

I have a dataset just as below :

ART_IdDateStock Qty_StockLast Day Stock <> 0Nb of days
130/04/202030003000  
124/06/2020-52995  
116/09/2020-12994  
128/09/2020-502944  
106/10/2020-502894  
119/10/2020-52889  
121/10/2020-1022787  
113/11/2020-302757  
107/12/2020-4602297  
114/12/2020-10001297  
112/01/202101297  
109/03/2021-501247  
111/03/2021-600647  
128/05/202130003647  
108/06/2021-1003547  
105/07/2021-1003447  
116/07/2021-503397  
128/10/2021-5002897  
110/12/2021-5002397  
114/12/20212502647  
129/12/2021-2647014/12/202115

 

  • The "_Stock" column is a measure, to have the running total : 
_Stock =
CALCULATE(
    SUM(Inventory[Stock Qty])
    , FILTER(ALL('Time'[Date]),
    'Time'[Date]<=MAX('Time'[Date])
    )
)
 
  • I'd like to know which articles have a stock = 0 today, so with this measure, I can select the ones with _Stock = 0. And For these ones, I'd like to have the last day the stock was different than 0, and the number of days between them.
I've created the following measure, but it doesn't work (it returns the date just as in the 2nd column) :
_LastStockDate = CALCULATE(MAX('Time'[Date]), FILTER(Inventory, [_Stock]<>0))
_NbdaysStock0 = DATEDIFF([_LastStockDate], [Today], DAY)
 
  • With this, I'd like to be able to tell they have x articles with zero stock today, with y number of days without any stock, and compare to the same day last year.
 
Also, I have a lot of lines in my data set, so if you have any tips to make these calculations easier and faster, it would be very helpful.
 
Thanks a lot in advance for your help,
 
Kind regards,
 
Marie
  • Hi MarieAmell ,

     

    This is my test table:

     

    Please try following DAX to crete new columns:

     

     

    Stock =
    CALCULATE (
        SUM ( 'Table'[Stock Qty] ),
        FILTER ( 'Table', 'Table'[Date] <= EARLIER ( 'Table'[Date] ) )
    )
    
    Last Day Stock <> 0 =
    IF (
        'Table'[Stock] = 0,
        CALCULATE (
            MAX ( 'Table'[Date] ),
            FILTER ( 'Table', 'Table'[Date] < EARLIER ( 'Table'[Date] ) )
        )
    )
    
    Nb of days =
    DATEDIFF ( 'Table'[Last Day Stock <> 0], 'Table'[Date], DAY )

     

     

     

    You will get the table like this:

     

    The result you want:

     

    Best regards,

    Yadong Fang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

6 Replies

  • MarieAmell , try like

     

    _Stock =
    var _max1= MAX('Time'[Date])
    var _max = date(year(_max1) -1, month(_max1), Day(_max1))
    return
    CALCULATE(
    SUM(Inventory[Stock Qty])
    , FILTER(ALL('Time'[Date]),
    'Time'[Date]<=_max
    )
    )

  • MarieAmell's avatar
    MarieAmell
    Frequent Visitor

    Thank you amitchandak , it should be good to calculate the stock SPLY.

     

    My first issue is to find the last date where my stock was not at 0 : in my example above, the stock for this article is at 0 the 29/12, and was not at 0 the 14/12 (No movement between). So I'd like to find the 14/12 (And then calculate the number of days Difference between the two dates).

     

    Thank you again for your time.

     

    Best regards

  • MarieAmell's avatar
    MarieAmell
    Frequent Visitor

    Sorry my request wasn't clear.

    I have the following dataset, with quantity in and out each day, for several articles (Here, just one articles).

    ART_IdDateStock Qty
    130/04/20203000
    124/06/2020-5
    116/09/2020-1
    128/09/2020-50
    106/10/2020-50
    119/10/2020-5
    121/10/2020-102
    113/11/2020-30
    107/12/2020-460
    114/12/2020-1000
    112/01/20210
    109/03/2021-50
    111/03/2021-600
    128/05/20213000
    108/06/2021-100
    105/07/2021-100
    116/07/2021-50
    128/10/2021-500
    110/12/2021-500
    114/12/2021250
    129/12/2021-2647
    130/12/20212397
    121/02/2022-500
    125/02/2022-100
    118/03/2022-100
    106/04/2022-50
    128/04/2022-1000
    119/05/2022-150
    103/06/2022-100
    116/06/2022-200
    124/06/2022-197

     

    I need the following calculation :

    Art_IdDateRunning stockLast date with stock > 0Nb of days with stock = 0
    129/12/2021014/12/202115
    124/06/2022016/06/20228

     

    The running stock doesn't have a starting date (or when it's at 0; it can start from this point).

    The last day with stock > 0 is the day with movments on the stock pre"vious to the date when it's at 0.

    And finally, I have the difference between the dates.

     

    At the end, I need to have a 

    TODAY    
    Art_IdDateRunning stockLast date with stock > 0Nb of days with stock = 0
    316/11/2022010/11/20226
    516/11/2022009/11/20227
         
    Total    
    2 articles out of stock today13

     

    SPLY    
    Art_IdDateRunning stockLast date with stock > 0Nb of days with stock = 0
    116/11/2021008/11/20216
    216/11/2021030/10/20217
    616/11/2021015/11/20211
    Total    
    3 articles out of stock SDLY   14

     

    I hope it's clearer,

     

    Thanks a lot for your help,

     

    Kind regards,

  • v-yadongf-msft's avatar
    v-yadongf-msft
    Icon for Community Support rankCommunity Support

    Hi MarieAmell ,

     

    This is my test table:

     

    Please try following DAX to crete new columns:

     

     

    Stock =
    CALCULATE (
        SUM ( 'Table'[Stock Qty] ),
        FILTER ( 'Table', 'Table'[Date] <= EARLIER ( 'Table'[Date] ) )
    )
    
    Last Day Stock <> 0 =
    IF (
        'Table'[Stock] = 0,
        CALCULATE (
            MAX ( 'Table'[Date] ),
            FILTER ( 'Table', 'Table'[Date] < EARLIER ( 'Table'[Date] ) )
        )
    )
    
    Nb of days =
    DATEDIFF ( 'Table'[Last Day Stock <> 0], 'Table'[Date], DAY )

     

     

     

    You will get the table like this:

     

    The result you want:

     

    Best regards,

    Yadong Fang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • MarieAmell's avatar
    MarieAmell
    Frequent Visitor

    Sorry for my very late reply, I was connected with a former account and missed the last messages. Thanks a lot for your help