Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
androo
Advocate II
Advocate II

Conditional cumulative sum as new calculated column in datamodel

In the table below my desired result is the final column (it looks horrid here but pastes nicely enough into Excel).

Index:Reference:PrevRowRef:Amount:Cumulative Amount:Desired Cumulative Result:

1A   0
2AA  0
3AA26.6426.6426.64
4AA39.8266.4666.46
5AA46.5112.96112.96
6AA40.91153.87153.87
7AA38.13192192
8AA33.63225.63225.63
9AA49.64275.27275.27
10AA56.09331.36331.36
11AA66.21397.57397.57
12AA78.79476.36476.36
13AA 476.36476.36
14AA 476.36476.36
15AA 476.36476.36
16BA 476.360
17BB 476.360
18BB31.06507.4231.06
19BB45.94553.3677
20BB57.06610.42134.06
21BB58.37668.79192.43
22BB52.27721.06244.7
23BB54.52775.58299.22
24BB13.56789.14312.78
25BB 789.14312.78
26BB 789.14312.78
27BB 789.14312.78
28BB 789.14312.78
29BB 789.14312.78
30BB 789.14312.78
31CB 789.140
32CC 789.140
33CC46.65835.7946.65
34CC68.92904.71115.57
35CC81.98986.69197.55
36CC83.91070.59281.45
37CC77.731148.32359.18
38CC76.341224.66435.52
39CC 1224.66435.52
40CC 1224.66435.52
41CC 1224.66435.52
42CC 1224.66435.52
43CC 1224.66435.52
44CC 1224.66435.52
45CC 1224.66435.52
46DC 1224.660
47DD 1224.660
48DD28.241252.928.24
49DD42.151295.0570.39
50DD45.11340.15115.49
51DD 1340.15115.49
52DD 1340.15115.49
53DD 1340.15115.49
54DD 1340.15115.49
55DD 1340.15115.49
56DD 1340.15115.49
57DD 1340.15115.49
58DD 1340.15115.49
59DD 1340.15115.49
60DD 1340.15115.49
61ED 1340.150
62EE 1340.150
63EE22.691362.8422.69
64EE341396.8456.69
65EE47.861444.7104.55
66EE51.441496.14155.99
67EE35.311531.45191.3
68EE35.331566.78226.63
69EE6.531573.31233.16
70EE 1573.31233.16
71EE 1573.31233.16
72EE 1573.31233.16
73EE 1573.31233.16
74EE 1573.31233.16
75EE 1573.31233.16

I have been able to get the "Cumulative Amount" column with this

 

Cumulative = Var index_1 = 'Calc Table'[Index]

VAR CumSum = if ([PrevRowRef] = [Reference],

    calculate(

        Sum([Amount]),

        Filter(

            'Calc Table',

            'Calc Table'[Index] < index_1

            )

),0)


Return

    CumSum + [Amount]

 

I have also been able to do this on a sample in Excel (structured references), which doesn't need the Index: or the PrevRowRef: column to do it. But, my data has 500k rows (and this is a cutdown of it there are many other columns too) and I'm concerned that XL may not cope well and also, I'd prefer to build the whole solution in PBi where I can also publish the result explaining it to my colleagues.

1 ACCEPTED SOLUTION
ryan_mayu
Super User
Super User

@androo 

is this what you want?

Column = sumx(FILTER('Table','Table'[Reference]=EARLIER('Table'[Reference])&&'Table'[Index]<=EARLIER('Table'[Index])),'Table'[Amount])+0

11.PNG

 

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

2 REPLIES 2
androo
Advocate II
Advocate II

Thanks very much for this neat solution which runs quickly over my almost 500k lines. I did try using the SUMX but I don't think I've ever got one to work in the way I expected or thought it would and this was no exception. In the past I've generally given up with them but have always been able to find some other way of doing whatever I was trying to do. However, this has brought me some understanding of what it is they are doing and so perhaps I will now find a way to use one and avoid repeating some of my more elaborate past workarounds!

ryan_mayu
Super User
Super User

@androo 

is this what you want?

Column = sumx(FILTER('Table','Table'[Reference]=EARLIER('Table'[Reference])&&'Table'[Index]<=EARLIER('Table'[Index])),'Table'[Amount])+0

11.PNG

 

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.