Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to use the field calculator in QGIS to create a range of values in a count sequence with a specified input range? For example, if I wanted to have the values 1 to 5 repeated within my table according to the first record in the table.


share|improve this question
Do you want to fill in 1,2,3,4,5,1,2,3,4,5,... or values dependent on another attribute? – underdark Nov 19 '12 at 16:11
Yes Underdark, that is what I want. 1,2,3,4,5 until all records are calculated. – Ryan Garnett Nov 19 '12 at 16:16
up vote 8 down vote accepted

This will do it in Field Calculator:

 ( $rownum-1 ) % 5 +1

enter image description here

share|improve this answer
Thank you, that is exactly what I was looking for. – Ryan Garnett Nov 19 '12 at 16:27
I love seeing stuff like this, and figured out some of the stuff by myself. But where can I find a more comprehensive documentation of all the things possible with the Field Calculator? – SAnderka Nov 19 '12 at 17:10
@SAnderka The new field calculator comes with a long list of documented functions - all visible in the GUI. Possibilities are near endless :) – underdark Nov 19 '12 at 17:12
I know, but somehow I am missing some basics here, I believe. What exactly does the "%5+1" part do? Also, my copy of QGIS (installed and updated via OSGEO4W) only shows "Oops! QGIS can't find help for this function." in the help window? – SAnderka Nov 19 '12 at 17:16
% is the modulo operator - like in many computer languages. It returns the remainder of the devision (by 5 in this case). On my OSGeo4W system docs are broken in 1.8 too but work fine in dev1.9. – underdark Nov 19 '12 at 17:21

My solution for this kind of problem is to use Libre or OpenOffice Calc. First of all check if your FID column is intact and correct (you'll need it to restore the attribute file to its original order if you resort the dbf table in Calc - otherwise the features will have the wrong data attached afterwards).

Create a Backup of the dbf, just in case.

Then, just open the dbf-file in Calc (don't forget to use the right codepage setting when opening the file, otherwise you might lose umlauts etc.), do whatever you want to do, e.g. autofill a column with data via vlookup(), and save the file again as dbf.

Check the result in QGIS, but everything should work just fine now.

share|improve this answer
Thank you, I know of this approach, but I wanted a means of doing this within QGIS. – Ryan Garnett Nov 19 '12 at 15:23
Also, I misunderstood your question - I thought you were looking for a way to add values depending on a changing other variable (not that I would have known the way to do this within QGIS... :)) – SAnderka Nov 19 '12 at 17:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.