1

I have a QGIS Geopackage for a layer and I want to update the "Coverage" column based on the contents of the "ID_1" column. In the example below, the ID_1 column contains 1,2,3 or 4 separate entities (each starting with "E" and separated by "|"). I'd like the Coverage column to represent that number, e.g. 1,1,2,1,2,3 for the first few rows.

In Excel I would just count the number of "E" in the column. How can I do that in QGIS, I have tried 'count' in the field calculator, but for every row, I get to total number of rows in the whole ID_1 column, not the number of E in each row.

The other option would be to paste the column from my Excel into the coverage column in QGIS, but I can't select a whole column in the QGIS Table - is that possible? This layer contains geometry so exporting it as a csv and then reimporting it won't work (I assume). enter image description here

1
  • Please provide the complete expression you tried for count(). Also, you assumption is wrong.
    – Erik
    Commented Sep 6, 2022 at 11:17

1 Answer 1

2

The count() function is not what you are looking for, since it is an aggregate function (it looks across all features in your layer).

How about this:

length( regexp_replace( "ID_1", '[^E]+' ,''))

Basically it deletes all characters (except for the E), than the lenght function counts the number of surviving characters ;)

enter image description here

1
  • 1
    The answer works perfectly and thank you for the explanation of why count() didn't work, and what your function does to get the result.
    – WillH
    Commented Sep 6, 2022 at 22:59

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.