2

I'd like to know how can i calculate in QGIS a field (field3) based on how many times a value is repeated (field2) considering an another field (field1). For example, the field to fill with the field calculator is the "field3":

enter image description here

Any suggestion using the field calculator of QGIS?

1
  • 2
    Question seems unclear. What are you calculating exactly?
    – wingnut
    Apr 29, 2021 at 2:59

1 Answer 1

4

Not sure I understood well (if you want to count occurrence from column field3 or if it's the column you want to fill with a count from another column).

I have considered you want to update the field3 using count from field1. In this case, you need to try the following expression to update field3 with count of occurrences from field1 using QGIS field calculator.

aggregate(
 layer:=@layer,
 aggregate:='count',
 expression:="field1"
 ,filter:="field1" = attribute(
@parent, 'field1'))

Translated as a sentence, above code says:

For each feature, loop on all other features to filter if any match the field1 value from current line. Keep only these features. Then, count how many there are for the particular feature.

Edit:

What you want to do is the same as counting on multiple columns from your comment. The following do exactly what you want (tested)

aggregate(
 layer:=@layer,
 aggregate:='count',
 expression:="field1" || "field2"
 ,filter:="field1" || "field2" = attribute(
@parent, 'field1') || attribute(
@parent, 'field2'))

PS: for an unknown reason, I got crash after crash while using the recipe with virtual field but not when calculating to fill an existing or new column.

3
  • Thanks Thomas for the reply. Unfortunately, your solution it doesn't work. The expected result is yet in"field3" in the image attached as example. I should consider the repetition of the values in field2 based on field1.
    – Mark
    Apr 29, 2021 at 20:57
  • Edited based on your feedback. Able to calculate field3 from field1 and field2
    – ThomasG77
    Apr 29, 2021 at 22:40
  • Perfect! It works!!
    – Mark
    Apr 30, 2021 at 22:17

Your Answer

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

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