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_Id Date Stock Qty _Stock Last Day Stock <> 0 Nb of days 1 30/04/2020 3000 3000     1 24/06/2020 -5 2995     1 16/09/2020 -1 ...
  • v-yadongf-msft's avatar
    3 years ago

    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.