4

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?

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Apr 28, 2021 at 22:39
1
  • 2
    Question seems unclear. What are you calculating exactly? Commented Apr 29, 2021 at 2:59

1 Answer 1

6

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.

answered Apr 28, 2021 at 23:05
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. Commented Apr 29, 2021 at 20:57
  • Edited based on your feedback. Able to calculate field3 from field1 and field2 Commented Apr 29, 2021 at 22:40
  • Perfect! It works!! Commented Apr 30, 2021 at 22:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.