# Field Calculate unique ID, based on another column, overriding FID / Object ID order [duplicate]

I have a couple thousand polygons across several hundred towns. I want the polygons within each town be labelled as such - first 3 letters of town name + _ + incrementing ID, so that they might look like this:

``````Ale_01
Ale_02
Bur_01
Bur_02
``````

And so on.

Using the Python Parser of the Field Calculator, I have tried using an augmented version of the standard ID incrementing script floating around (pasted below):

``````prev_town = ""
rec=0
def autoIncrement(%Town%):
global rec
global prev_town
if town != prev_town:
rec = 0
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec += pInterval
id = town[0:3] + "_" + str(rec).zfill(2)
return id
``````

The problem is that, the records within and across towns, were added at different times (i.e. Bur_01 was added before Ale_02), so that even after sorting by town, when applying the above script, I can get repeat ID (Ale_01 twice) due to the fact that the field calculator is applied in the order of ObjectID, and it doesn't (of course) keep track of previous IDs.

So, I have tried to augment the above code like so:

``````prev_town = ""
rec=0
ids = []
id = ""
def autoIncrement(town):
global prev_town
global rec
global ids
global id
if town != prev_town:
rec = 0
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec += pInterval
id = town[0:3] + "_" + str(rec).zfill(2)
while( id in ids):
if town != prev_town:
rec = 0
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec += pInterval
id = town[0:3] + "_" + str(rec).zfill(2)
ids.append(id)
return id
``````

But, this seems to hang and never complete - so I can't even tell if it is working or not....

Also, this data layer is a feature service, so I am trying to do this on a local replica and sync back - that of course brings with it its own limitations.

Any suggestions?

• Is the "standard ID incrementing script" that you mention being implemented using the Python Parser of the ArcGIS Field Calculator, or in some other way? I suspect it would be easier to use an update cursor.
– PolyGeo
Dec 22, 2017 at 22:18
• The Python Parser of the Field Calculator. Dec 22, 2017 at 22:32