# How calculate sequential values based on group field?

In field calculator i calculate sequential values in field [ORDER_ID] What need change in script to calculate sequential values based on group field [GROUP_ID]. I select values manually and use autoincrement () in Field Calculator.

Parser: Python Expression: autoIncrement() Pre-Logic Script Code:

``````rec=0
def autoIncrement():
global rec
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return rec
``````

This is the result. Field ORDER_ID is already calculated manually (i select GROUP_ID and use autoincrement).  What changes script needed to use it in field calculator?

You can do this with an UpdateCursor easily in the Python Window:

``````import arcpy

vals = []
fields = ['ORDER_ID', 'GROUP_ID'] #your field names
with arcpy.da.UpdateCursor(your_lyr, fields) as rows:
for r in rows:
vals.append(r)
r = vals.count(r)
rows.updateRow(r)
``````

where the `your_lyr` variable is a reference to the table you're updating.

• -1. This equal to simple autoincrement. OP specifically asks about groups – FelixIP Dec 22 '15 at 4:41
• @FelixIP he's incrementing by the count for each group. I don't see what's wrong with his answer. – ianbroad Dec 22 '15 at 5:53
• not exactly what I was asking. but it works. if I understand correctly numbering going by creation date/ – Jannik Dec 22 '15 at 7:20
• @ian you are right. My mistake, i'll upvote it back as soon as i can. Misread count as len. – FelixIP Dec 22 '15 at 8:52
• @Jannik - this is doing an increment by the value in your `GROUP_ID` field. So it should incremental counts for each group. @ian - thanks for backing me up :) – crmackey Dec 22 '15 at 13:47

I realise this post is a little dated and with an already accepted answer, but the question specifically asked for alterations to the existing auto-increment codeblock for the field calculator, which should be achievable simply by using the GroupID as a variable to test.

Pre-logic script would be:

``````rec=0
group=1
def autoIncrement(GroupId):
global rec
global group
pStart = 1
pInterval = 1
if (GroupId == group):
rec += pInterval
else:
group += pInterval
rec = pStart
return rec
``````

And the expression:

``````autoIncrement(!GroupId!)
``````

With the parser set to python. The assumptions are a) Your groups start at 1; and b) your groups are sequentially numbered.