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":
Any suggestion using the field calculator of QGIS?
-
2Question seems unclear. What are you calculating exactly?wingnut– wingnut2021年04月29日 02:59:38 +00:00Commented Apr 29, 2021 at 2:59
1 Answer 1
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.
-
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– Mark2021年04月29日 20:57:01 +00:00Commented Apr 29, 2021 at 20:57
-
Edited based on your feedback. Able to calculate field3 from field1 and field2ThomasG77– ThomasG772021年04月29日 22:40:46 +00:00Commented Apr 29, 2021 at 22:40
-