2

I have a layer with 3 fields: fieldA and fieldB are texts, fieldC are figures

I try to sum fieldC only when "fieldB"= currentrow attribute 'fieldA'.

In the picture below, the sum for the first line should 9 (5+4)

I read several answers on the website but I cannot find the expression using field calculator.

My formula is sum( "fieldC" , filter:=("fieldB" = attribute($currentfeature , 'fieldA'))) but it is not working.

attribute table

1 Answer 1

2

It looks like the variable scope for sum() can't handle fieldB being evaluated for the entire dataset but having fieldA only referring to the current feature.

A workaround is to store the current value of fieldA as a seperate variable using with_variable() then plugging that into sum().

Example expression below:

with_variable('currfieldA',
              "fieldA",
              sum("fieldC",
                  filter:= "fieldB" = @currfieldA))

Result:

enter image description here

1
  • Wonderful! This is working perfectly! It is working with static fields, dynamics fields, with features added before or after the use of the formula (as soon as the layer is refreshed after) Oct 13, 2022 at 7:48

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.