6

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.

Here is the table with data I'm using.

3
  • 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
    Commented 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
    Commented Dec 22, 2021 at 14:55
  • Please, do not forget about "What should I do when someone answers my question?"
    – Taras
    Commented May 24, 2022 at 5:21

3 Answers 3

8

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)
    )

enter image description here

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)
    )

enter image description here

2
  • Thank you so much this works for me! :)
    – timdub
    Commented 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/…
    – Babel
    Commented Jan 3, 2022 at 14:38
5

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(
  array_agg("your sum field",order_by:="your order field"),
  0,
  array_find(
   array_agg($id,order_by:="your order field"),
   $id
  )
 )
)
0
5

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)
    )

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.