# Calculating cumulative SUM in QGIS Attribute table

I'm trying to get the cumulative sum of the column "uren_duur" in a new column "uren_tot" based on the recorded water height "NUMERIEKEWAARDE". The water height ranges from -194 to 262 and since this I'm trying to figure out the total time each water height has been submerged over a 3 month period. The column "uren_duur" contains the time submerged per water height.

So for the top row ("NUMERIEKWAARDE" = 262) the cumulative sum should be 0,166.. Then for "NUMERIEKWAARDE" = 261 the cumulative sum should be 0,166..+ 0,166.. = 0,333, 257 should then be 0,166..+ 0,166..+ 0,166.. = 0,4999 and so on...

So when the height is -194 this should likely be around 2100 hours cumulative sum. I tried using this post (Add cumulative sum column to attribute table) as you can see in the expression bar but it wouldn't work for me.

This step is so easy to do in excel by just filling the first value (i.e. C1 = 1) and then using B2 + C1 to calculate C2 and just dragging the formula down. But I'm trying to automize a process in a small model so I don't want to perform this step manually.

• It should work. The expression in the screenshot seems wrong near the end, as your are referencing to another layer (calulated_a28a... instead of uren_duur (layer)) and another field (NUMERIEKWAARDE instead of uren_duur (attribute))
– JGH
Dec 22, 2021 at 14:54
• See this post for an alternative method using a virtual layer, where you can specify the sort order too
– JGH
Dec 22, 2021 at 14:55
• Please, do not forget about "What should I do when someone answers my question?" May 24, 2022 at 5:21

To calculate the cumulative sum of the field "value", based on a condition of the field "order", create "order" as a variable that you can refer to in the filter condition of the sum() function. Like this:

with_variable (
'order',
order,
sum( value, filter:=order<=@order)
)

You can add further conditions to the filter condition, like only calculate the cumulative sum based on order field and if the value of field "id" can be divided by 3:

with_variable (
'order',
order,
sum(value, filter:=order<=@order and id%3 = 0)
)

• Thank you so much this works for me! :) Jan 3, 2022 at 14:29
• You're welcome. As a new user, please be adviced that this side tries to avoid "thank you" messages. Instead, it encourages you to accept the answer and/or upvote it: click the checkmark right to the answer to accept it - see: gis.stackexchange.com/help/someone-answers Just click the checkmark, see here: meta.stackexchange.com/questions/5234/… Jan 3, 2022 at 14:38

In your Field Calculator, you can also try this. The "id" field is your order by field and "uren" the sum field.

with_variable(
'id1',
"id",
round(array_sum(array_agg("uren", @id1 >= "id")), 2)
)

Here is an expression which does that. You need two fields:

• A field that contains the order of your cumulation "your order field"
• A field that contains the number you want the sum of "your sum field"
array_sum(
array_slice(