I don't know how you created your data. However, the behaviour is not normal. Normally, you should not need to care about the id this way, as QGIS would handle this automatically the way you want. Saving your data as Geopackage, you automatically get a fid
that exactly does what you describe.
So you probably miss a point here, but you don't provide enough information to see what goes wrong. Still, to answer your question in case you really want to continue to do this manually using the following expression (see below for explanation). Be aware: in my case, fid
is the name of the id-value in Set_1
; in Set_2
it's called id
(change this accordingly):
if (
"id" is NULL,
aggregate ('Set_1', 'max', "fid") +
@row_number -
array_length (
aggregate(
@layer,
'array_agg',
"id",
filter:= "id" is not NULL
)
),
"id"
)
Find the largest existing id value in Set_1 with aggregate( 'Set_1','max',fid)
. In your screenshot, this would return 8
.
Find the number of remaining features (rows, lines) that already have an id value in Set_2
with array_length (array_agg ("id", filter:= "id" is not NULL))
. Returns 6
in your example.
Now for each of the additional rows (lines 7, 8,...), calculate a sequential series of how much you have to add: add 1 for line 7, 2 for line 8 etc. To calculate this, use @row_number - [result from step 2]
- replace the part in angular braces []
with the expression from step 2.
You want to start with id=9 (next larger value from step 1) in line 7 (next feature from step 2). Thus add value from step 1 with value from step 3.
You want to update only id's that are empty, but keep pre existing values