QGIS Attribute Table – How to Autofill Columns Based on Another Cell’s Values

attribute-tableexpressionfield-calculatorqgis

I am working with vegetation types within a project site. Each vegetation type belongs to a broader group termed a "vegetation group". For example, in the screen shot below, I have the two vegetation types 'WC1' and 'WC2' which belong to the 'Wet Coniferous Forest Group'.

Is there a way by expression to populate a new column with the the appropriate vegetation group based on the vegetation types in a column? In this example, all 'WC1' and 'WC2' would be in the 'Wet Coniferous Forest Group' which would be in a new column.

enter image description here

Best Answer

Use the Field calculator with a CASE-statement. The expression looks like this, where "vegetation_type" is the name of the field containing the WC1, WC2 values:

case 
    when "vegetation_type" in ('WC1', 'WC2') then 'Wet Coniferous Forest Group'
end

Before the end statement, you can add as many similar when ... then statements as you like:

when "vegetation_type" in ('WC3', 'WC4') then 'Another Group'

You can also add an else statement that returns the output in all other cases, something like else 'other'. Add this after the last when statement, before end:

case 
    when "vegetation_type" in ('WC1', 'WC2') then 'Wet Coniferous Forest Group'
    when "vegetation_type" in ('WC3', 'WC4') then 'Another Group'
    else 'other'
end
Related Question