Forum Discussion
Running total by ignoring negative values
- 4 years ago
cumul =
VAR d = MAX ( 'Table'[Month] )
VAR t =
SUMMARIZE (
FILTER ( ALLSELECTED ( 'Table' ), [Month] <= d ),
'Table'[Month],
"a", SUM ( 'Table'[A] ),
"b", SUM ( 'Table'[B] )
)
RETURN 0 + SUMX ( t, [a] + MAX ( 0, [b] ) )
Here is one version using CALCULATE
cumul =
var d = max('Table'[Month])
return calculate(sum('Table'[A]),ALLSELECTED('Table'),'Table'[Month]<=d)+calculate(sum('Table'[B]),ALLSELECTED('Table'),'Table'[Month]<=d,'Table'[B]>0)
Here is another one using SUMX
cumul2 =
var d = max('Table'[Month])
return SUMX(FILTER(ALLSELECTED('Table'),'Table'[Month]<=d),[A]+max([B],0))Hi lbendlin Thanks for the response. Its better than the old calc but not working as expected since we have more details at the table. Just to simplify i had given the summarized values at month level but the actual table with direct query has few other dimensions which breaks one row in to 1000's of rows.
The filter 'Table'[B]>0 applies at row level and it considers all positive adjusted rows in that month but the expectation is to summarize the adjusted value of that month and if that is>0 then consider.
In the below example Nov-18 has one +ve row with 507 and the overall adjusted for that month is -5892. The provided formula just adds 507 to the prev month value in the running total.
| Month | A | B | C | C Cumulative | |
| Jan-17 | 0 | ||||
| Feb-17 | 0 | 0 | 0 | ||
| Mar-17 | 0 | 0 | 0 | ||
| Apr-17 | 0 | 0 | 0 | ||
| May-17 | -4637 | 0 | 0 | ||
| Jun-17 | 300000 | 0 | 300000 | 300000 | |
| Jul-17 | 0 | 0 | 300000 | ||
| Aug-17 | 0 | 0 | 300000 | ||
| Sep-17 | 0 | 0 | 300000 | ||
| Oct-17 | 0 | 0 | 300000 | ||
| Nov-17 | -253 | 0 | 300000 | ||
| Dec-17 | 0 | 0 | 300000 | ||
| Jan-18 | -37 | 0 | 300000 | ||
| Feb-18 | 0 | 0 | 300000 | ||
| Mar-18 | 24299 | 0 | 24299 | 324299 | |
| Apr-18 | 0 | 0 | 324299 | ||
| May-18 | -1 | 0 | 324299 | ||
| Jun-18 | -2 | 0 | 324299 | ||
| Jul-18 | 300000 | 664 | 300664 | 624963 | |
| Aug-18 | 530 | 530 | 625494 | ||
| Sep-18 | 81600 | 27842 | 109442 | 734936 | |
| Oct-18 | 30000 | 424 | 30424 | 765360 | |
| Nov-18 | -5892 | 0 | 765867 | 507 | |
| Dec-18 | 12000 | -68 | 12000 | 777867 | |
| Jan-19 | 1 | 1 | 777871 |
- lbendlin4 years ago
Super User
Please provide sanitized sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.