Search code examples
powerbidaxpowerbi-desktop

Calculating cumulative sum with date filtering in a table


I have a table with two columns (DateTime and Result) and I need a cumulative sum of the results, starting from 0 for each day.

DataTime Result
2023-12-20 09:02:00 -10
2023-12-20 11:02:00 20
2023-12-20 12:04:00 45
2023-12-22 08:05:00 50
2023-12-22 09:14:00 32
2023-12-22 11:55:00 -10

The expected result is

DataTime Result Daily Result
2023-12-20 09:02:00 -10 -10
2023-12-20 11:02:00 20 10
2023-12-20 12:04:00 45 55
2023-12-22 08:05:00 50 50
2023-12-22 09:14:00 32 82
2023-12-22 11:55:00 -10 72

The expression

Daily Result = 
CALCULATE(
    SUM('Original'Result);
    FILTER(
        ALL('Original');
        Original[DateTime] <= EARLIER(Original[DateTime])
    )
)

returns the daily sum added to the previous day daily sum, like this:

DataTime Result Daily Result
2023-12-20 09:02:00 -10 55
2023-12-20 11:02:00 20 55
2023-12-20 12:04:00 45 55
2023-12-22 08:05:00 50 127
2023-12-22 09:14:00 32 127
2023-12-22 11:55:00 -10 127

Solution

  • you can try to create to create a column

    Column =
    SUMX (
        FILTER (
            'Table',
            INT ( 'Table'[DataTime] ) = INT ( EARLIER ( 'Table'[DataTime] ) )
                && 'Table'[DataTime] <= EARLIER ( 'Table'[DataTime] )
        ),
        'Table'[Result]
    )
    

    enter image description here