Forum Discussion

obesli's avatar
obesli
Frequent Visitor
8 years ago
Solved

Calculating cumulative values

    I have data lite this table. Year mont column and sales amount in each mont. I would like to calculate mothly cumulative value, but I couldn't do it. IS there any method to propose?    ...
  • Anonymous's avatar
    Anonymous
    8 years ago

    Hi obesli,


    You can try to use below measures:

    Spoiler
    Cumulative = 
    VAR _current =
        SELECTEDVALUE ( 'Table'[DateKey] )
    VAR _previous =
        MAXX ( FILTER ( ALL ( 'Table' ), [DateKey] < _current ), [DateKey] )
    RETURN
        IF (
            RIGHT ( VALUE ( _current ), 2 ) <> "01",
            SUMX (
                FILTER (
                    ALL ( 'Table' ),
                    [DateKey] < _current
                        && LEFT ( VALUE ( [DateKey] ), 4 ) = LEFT ( VALUE ( _current ), 4 )
                ),
                [Sales]
            ),
            MAX ( 'Table'[Sales] )
                + LOOKUPVALUE ( 'Table'[Sales], 'Table'[DateKey], _previous )
        )
    
    
    Cumulative(Jan replace current + Previous Total) = 
    VAR _current =
        SELECTEDVALUE ( 'Table'[DateKey] )
    VAR _previous =
        MAXX ( FILTER ( ALL ( 'Table' ), [DateKey] < _current ), [DateKey] )
    RETURN
        IF (
            RIGHT ( VALUE ( _current ), 2 ) <> "01",
            SUMX (
                FILTER (
                    ALL ( 'Table' ),
                    [DateKey] < _current
                        && LEFT ( VALUE ( [DateKey] ), 4 ) = LEFT ( VALUE ( _current ), 4 )
                ),
                [Sales]
            ),
            MAX ( 'Table'[Sales] )
                + SUMX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[DateKey] <= _previous
                            && LEFT ( VALUE ( [DateKey] ), 4 ) = LEFT ( VALUE ( _previous ), 4 )
                    ),
                    [Sales]
                )
        )
    

    Result:

     

    Regards,

    Xiaoxin Sheng