I current have attribute bar all empty with null values. I would like to fill that attribute starting from 10001, adding +1 to each of the features and having N before the number

So far I have come up with this.


This is only giving N10001 to all features. How can I tell it that it needs to go alongside the column and increment it from the value the feature above has?

I have another attribute that could be used for comparison, attribute object id already has a number sequence for all those null features.


3 Answers 3


To extend @Eriks answer: just in case your ID's do not start with 1, you can use this expression:

'N' || to_string(10000 + array_find(array_agg($id),$id) + 1)

It will always start at 10001, no matter what your actual ID values are.

  • I tried it but unfortunately it's giving me some random numbers, I guess the id values have something to do with it. I got 1234, then 2474, all random numbers Commented Apr 16, 2021 at 13:50
  • Shouldnt be random, as array_find() returns the index of the current ID in the array. The only reason I can think of, why it may appear random to you, is the sorting of your attribute table, which probably is not sorted by feature ids.
    – MrXsquared
    Commented Apr 16, 2021 at 13:53
  • Well I do have a filter in place, I don't know if that is relevant. Commented Apr 16, 2021 at 13:57

'N1000'||to_string($id+1) should do what you are looking for. Since the ID starts out with 0, we need to add +1 in order to start with N10001.

  • I used it but got an error saying: "An error occurred while evaluating the calculation string: Cannot convert 'N10008895' to double" Commented Apr 16, 2021 at 13:57
  • 1
    Since N is no number, but a string, you chose the wrong field type, I guess.
    – Erik
    Commented Apr 16, 2021 at 14:04
  • I am using a template, I can't change they field types, the field is of the type Qstring, that's why I was thinking of converting it to string in the end of the expression. Commented Apr 16, 2021 at 14:06

Here is a PyQGIS solution:

layer = iface.activeLayer() # choose the layer to edit
fieldname = 'mycolumn' # name of the column to change
i = 10001 # value to start counting at

# no changes needed below #

# start edit mode of the layer
with edit(layer):
    # add the new field
    # layer.dataProvider().addAttributes([QgsField(fieldname, QVariant.String)])
    # layer.updateFields()
    # loop through features
    for feature in layer.getFeatures():
        # change value of the new field to "Nxxxxx"
        feature[fieldname] = 'N' + str(i)
        # increase counter
        i += 1
  • That worked well thank you. Could you tell me why you made a new field tho? Would it be harder to do the changes in a existing attribute? Commented Apr 17, 2021 at 16:43

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.