How can I allocate the $rownum value in reverse order in QGIS?

I have a created a grid layer to use in an atlas and the ID allocated is from top left to bottom right - which is what I want if I want to create a $rownum attribute to use as the map number in my atlas (e.g. 1 - 78)

But I have selected only those grid polygons that have relevant data beneath them for my maps, so I have created a grid layer that is a subset of the original. In this subset grid, the ID numbers have been reversed - that is, they go from bottom right to top left so if I create the $rownum field the numbers will be in that reverse [new] order. (e.g. 45 - 1).

So I want to allocate that $rownum attribute in reverse order so that the atlas maps read from top left to bottom right.

Can anyone help?

1 Answer 1


Internally you cannot modify Row Numbers. Your calculation into a new field is the number of rows - $rownum:

enter image description here

This will reverse the $rownum into a field. $rownum (row number) objects cannot be modified as they are handled by the driver/database. In the case of shapefiles they are transient as edited features move to the end and then get compressed back to 0 based, unique and ascending on save edits.

  • that looks as if it will change one row id. For the sake of the example in my question I pulled 45 out of the air. I actually have 118 grid polygons. I'm not keen to do this individually for each grid.
    – Johanna
    Mar 12, 2015 at 0:51
  • I guess what I'm looking for is an 'auto increment' function that I can use on a new field in the table.
    – Johanna
    Mar 12, 2015 at 0:59
  • The whole point of the field calculation is that it does it for the field on every row in the table. If you have 118 polygons then it's 118 - $rownum. There are ways to auto increment on create but that requires some programming. Mar 12, 2015 at 1:03
  • Give this a try, it should solve your issue. If it doesn't you can always just rollback the edits.
    – Nathan W
    Mar 12, 2015 at 1:06
  • Thank you, it didn't look like it was going to work, but it did.
    – Johanna
    Mar 12, 2015 at 2:11

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.