cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
moia79
Helper I
Helper I

Cumulative sum in a table

Hello all,

 

I'm struggling to calculate a cumulative difference between two columns. 

 

My dataset is a table that, for each month of the year, reports two values: "Sum of Requested orders" and "Sum of Confirmed orders".

Each sum is the amount of orders requested or confirmed in each month.

 

I would like to add another column, for each month, that calculates the difference between the two columns. This additional column however should also be comulative and sum the diffences for the whole year.

 

Data example:

JanuaryFebruaryMarchApril
RequestedConfirmed**bleep** DifferenceRequestedConfirmed**bleep** DifferenceRequestedConfirmed**bleep** DifferenceRequestedConfirmed**bleep**. Difference
2400230010010008003001900175045020001500950

 

I arrive to calculate the Difference in each month, but struggle to create the cumulative difference

 

any suggestion from the guru of DAX? 

🙂

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @moia79 
Please try

Difference =
VAR CurrentMonth =
    MAX ( 'Date'[Month Number] )
VAR T1 =
    CALCULATETABLE (
        VALUES ( 'Date'[Month] ),
        'Date'[Month Number] <= CurrentMonth
    )
VAR T2 =
    ADDCOLUMNS (
        T1,
        "@Difference",
            VAR CurrentMonth1 = 'Date'[Month]
            RETURN
                CALCULATE (
                    [Requested] - [Confirmed],
                    ALL ( 'Date'[Month] ),
                    'Date'[Month] = CurrentMonth1
                )
    )
RETURN
    SUMX ( T2, [@Difference] )

View solution in original post

4 REPLIES 4
Stachu
Community Champion
Community Champion

That looks like an output table, i.e. matrix visual.

What is your input data? If it looks like the table below then it's an easy setup

MonthRequestedConfirmed
Jan24002300
Feb1000800

 



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Proud to be a Super User!

hi @Stachu 

 

yes, table is as you reported. For each row I need to calculate the difference (Requested - Confirmed). 

 

Moreover, I need that this difference is cumulative, so that each month difference is summed to the one calculated in the previous month

 

e.g.:

Jan = 2.400 - 2.300 = 100

Feb = (1.000 - 800) + January = 200 + 100 = 300

March = (xxx - xxx) + Feb = ( ... ) + 300

tamerj1
Super User
Super User

Hi @moia79 
Please try

Difference =
VAR CurrentMonth =
    MAX ( 'Date'[Month Number] )
VAR T1 =
    CALCULATETABLE (
        VALUES ( 'Date'[Month] ),
        'Date'[Month Number] <= CurrentMonth
    )
VAR T2 =
    ADDCOLUMNS (
        T1,
        "@Difference",
            VAR CurrentMonth1 = 'Date'[Month]
            RETURN
                CALCULATE (
                    [Requested] - [Confirmed],
                    ALL ( 'Date'[Month] ),
                    'Date'[Month] = CurrentMonth1
                )
    )
RETURN
    SUMX ( T2, [@Difference] )

thanks @tamerj1 it works perfectly

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors
Top Kudoed Authors