I am working with a simple attribute table that has 4 columns; 'Xcoord' 'Ycoord' 'id' 'id2'

'id2' is a new field that i added to the attribute table. 'id' is a field showing land use classes with values ranging from 0-210.

I wish to make 'id2' be filled up with new values ranging from 1-6, based on their value in the 'id' field.

For example, I wish to write an expression for;

when 'id' are values 10,20,30 and 40, i want them to have a value of 1 in 'id2' and 'id' values of 50,60,70 as 'id2' value of 2 and so on and so forth till an 'id2' value of 6.

Basically like a hierarchical aggregation of land cover classes.

I am trying to self-learn the QGIS expression builder but I am encountering many difficulties trying to code the expression properly.

Here is an image of the

attribute table

  • Welcome to GIS SE. As a new user, please take the Tour. Please try to make sure you specify what you've tried, in addition to what you want.
    – Vince
    Commented Jan 17, 2018 at 4:07
  • Thank you kindly Vince. I have still yet to take the Tour, but will be more explicit with updating my questions as well, as the steps I have performed prior to posting the question
    – Johanness
    Commented Jan 22, 2018 at 3:07

1 Answer 1


You need to use Field calculator, select Update existing field and choose the correct field id2, and write the following expression, and:

When "ID" in (10,20,30,40) then 1
When "ID" in (50,60,70) then 2
When "ID" in (#,#,#) then 3

where # is the other values, and repeat When statement to change values after then from 4 to 6

  • Thank you very much @ahmadhanb, your solution worked perfectly. May i ask where I can find online sources to learn more about the Expression Builder or is the QGis Documentation supposedly sufficient?
    – Johanness
    Commented Jan 17, 2018 at 4:05
  • @Johanness Please make sure to accept the answer if it helped you (use the green mark under the answer). Regarding online sources, QGIS documentation has really good examples to start you working with the software, but it can't give you everything. You need to test and experiment by yourself to learn more. This site has really impressive examples and there are really talented people that posted their solution on complex questions which become a source for deep learning of QGIS and GIS in general.
    – ahmadhanb
    Commented Jan 17, 2018 at 4:28
  • I my experience, the expressionbuilder is quite selfexplanatory. Just test snippets of code until they work (just below the code-window there's a preview what your code will do to the first cell of your column), save it elsewhere and then patch it all together. Getting the right amount of brackets usually is the hardest thing.
    – Erik
    Commented Jan 17, 2018 at 7:56
  • So, actually, in the end, the answer by @ahmadhanb was supposed to work but for some reason, mine still defaulted to 'NULL' values. I looked into other threads on this site, and found after trial-and-error that when operating with the QGIS graphical modeler, the one that I had success with was with GRASS GIS 7's r.reclass algorithm, whereby one only needs to specify the '.txt' file by which the reclassification rules lie in. The other alternative from the Field Calculator, was through SAGA's Reclassify Values algorithm, which did not work for me in the Graphical Modeler. Thank you one and all!
    – Johanness
    Commented Jan 22, 2018 at 3:10

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.