I'd like to create a new field considering duplicate and numeric values.

For example, I have an attribute table with duplicate values in the field1 and numeric values in the field2. I'd like to create a field3 in which there are the suffix _1, _2...on the field1 attributes, when they are duplicates and when "field2">0.

In case "field2"=0 or not duplicated value in the "field1" -> "field3"="field1".

The suffix should not be set randomly, but following the spatial direction (or left or right); and for each group of duplicated values.






Any suggestion to use a string in the field calculator to do this in QGIS?


You can try the following expression:

array_agg( $geometry,
WHEN array_find(@my_array,$geometry)=-1
THEN to_string("field1")
ELSE to_string("field1")  ||  to_string(array_find(@my_array,$geometry)+1)

a short explanation: array_agg takes all geometries into an array with the same value in field1 (the GROUP_BY), without the ones having a 0 in field2 and order it by the easting value(x). In the CASE WHEN expression it will proof, if the current value (geometry) is in the array, if yes, it will be added by a position value from the array, if not, it will be nothing added. You may have to play around with the conditions a bit to get the result you want to achieve.

  • Thank you so much!! It works! In this code, i don't understand where is the condition where "not duplicated value in the "field1" has been set for not to take the suffix ("field1"='c' example). Also, i don't understand where you set "_" before the suffix number 1,2,3. Thanks.
    – Mark
    Jun 9 '20 at 21:33
  • to set the suffix you should change: ELSE to_string("field1") || '_' || to_string(array_find(@my_array,$geometry)+1) the 'c' is in fact a problem...
    – eurojam
    Jun 10 '20 at 5:18
  • about the second string....maybe, we can adding a condition not duplicated value in the "field1" in the row -> filter:="field2">0 ? I'm thinking -> filter:="field2">0 and count_distinct("id","field1")>1,....it should work
    – Mark
    Jun 10 '20 at 18:15
  • In this case, we are considering the 'x' spatial direction [order_by:=x($geometry))]. If there isn't a prevalent direction (x or y), and the points are positioned along a not regular route (presence of curves and intersections); is there a solution to order these points considering the street direction? Thanks a lot!
    – Mark
    Jun 19 '20 at 8:15
  • yes I think that is possible if you have the direction under your control, see gis.stackexchange.com/questions/25337/…
    – eurojam
    Jun 20 '20 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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