3

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.

'N'+to_string(1000+1)

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.

1
  • Sorry, I am using QGIS 3.10.8-A Coruña I would like to update the attribute 'Bar' with an expression if possible, no plugins. – Ghostwriter84 Apr 16 at 13:37
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.

3
  • 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 – Ghostwriter84 Apr 16 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 Apr 16 at 13:53
  • Well I do have a filter in place, I don't know if that is relevant. – Ghostwriter84 Apr 16 at 13:57
3

'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.

3
  • I used it but got an error saying: "An error occurred while evaluating the calculation string: Cannot convert 'N10008895' to double" – Ghostwriter84 Apr 16 at 13:57
  • 1
    Since N is no number, but a string, you chose the wrong field type, I guess. – Erik Apr 16 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. – Ghostwriter84 Apr 16 at 14:06
3

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)
        layer.updateFeature(feature)
        # increase counter
        i += 1
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? – Ghostwriter84 Apr 17 at 16:43

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.