4

I am hoping to build a field in one of my QField forms that auto-increments a number associated with a particular value from a different field.

ie. if field "Sampler" has value "AA", a new field, "Station_ID" will auto-populate AA-001. If another point is taken by sampler "AA", the "Station_ID" will auto-increment to AA-002. If another point is taken by sampler "BB", the "Station_ID" will auto-populate to BB-001.

This will allow me to associated a unique station ID with each individual samplers collected data.

I'm working on QGIS 3.22.5

How can I complete this?

enter image description here

3 Answers 3

5

Try this, configure the form for Station_ID this way

basically is to turn off the editable option, turn on apply defaults on update and paste this expression in the default value:

format(
    '%1-%2',
    "Sampler",
    with_variable(
        'n',
        array_count(
            array_agg(
                "Sampler"
                ),
                "Sampler"
            ) + 1,
        lpad(@n, 3, '0')
        )
    )
3
  • you rock! Thank-you! Commented Jun 22, 2022 at 1:45
  • Thanks. It is working well but doesn not seem to work on qfield. Do you have any ideas why this might not work? Commented Jun 24, 2022 at 2:21
  • Another option than the array methods would have been count("sampler",group_by:="sampler"), using count_distinct would have been an extra safety too.
    – Al rl
    Commented Jun 29, 2022 at 0:10
2

Python code does not work in QField. So you need an expression.

Default of 'Station-ID' should be as follows:

sampler||'-'||LPAD(count(sampler=sampler),3,0)
1
  • Thanks Kadir. I finally had a chance to test this a bit more and this code produces the default Station-ID "sampler"-000. On QGIS it automatically increments by sampler. ie. AA-000, AA-001, AA-002, but when exported to QField the default still does not auto-increment. Instead, each time a new point is made with the same sampler, the Station-ID stays at AA-000. Any thoughts on how to fix? Commented Mar 9, 2023 at 0:44
2

I'm trying to do the same thing than you. Add +1 to increment. This is the expression I use :

coalesce("sampler") || '-' || coalesce(lpad((count("sampler",("sampler")) + 1),3,0)))

It seems to me that QGIS counts when you create a station while Qfield counts after you saved your station. But I have my own problem. Please tell me if you have the same or maybe someone will find a solution : By using my expression, because the default value is applying on update, all my stations are renamed automatically in background when I use Qfield (but it works fine in QGIS). For example, if I had three AA station (AA-001, AA-002 and AA-003), every three stations become AA-003.

1
  • Welcome and thank you for your willingness to provide an answer to a question. If you encounter a problem, please open your own topic. Please do not ask questions in answers to other people's questions.
    – GisUser
    Commented Jul 18, 2023 at 8:23

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.