Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreWe've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now
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:
| January | February | March | April | ||||||||
| Requested | Confirmed | **bleep** Difference | Requested | Confirmed | **bleep** Difference | Requested | Confirmed | **bleep** Difference | Requested | Confirmed | **bleep**. Difference |
| 2400 | 2300 | 100 | 1000 | 800 | 300 | 1900 | 1750 | 450 | 2000 | 1500 | 950 |
I arrive to calculate the Difference in each month, but struggle to create the cumulative difference
any suggestion from the guru of DAX?
🙂
Solved! Go to Solution.
Hi @Anonymous
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] )
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
| Month | Requested | Confirmed |
| Jan | 2400 | 2300 |
| Feb | 1000 | 800 |
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
Hi @Anonymous
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] )
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 9 | |
| 8 | |
| 3 | |
| 2 | |
| 2 |
| User | Count |
|---|---|
| 23 | |
| 14 | |
| 10 | |
| 6 | |
| 5 |