AnsweredAssumed Answered

Auto Increment by Attribute

Question asked by MTCGIS on May 31, 2018
Latest reply on Jun 3, 2018 by Dan_Patterson

Hello, 

 

I'm trying to create a function that leverages the auto increment function which is provided as a field calculator example in ArcGIS Pro's Calculate Field Python examples. The auto increment function is pretty straight-forward however, I'm looking to increment based on the attributes of another field. I've attached an example of the desired output. Below I've provided a snippet of the code where I've attempted to create a function that does what I've described but it doesn't seem to be working and I'm probably missing some important information about how field calculator works with tables. In the autoincrement function, you set the initial value to 0. But in an attempt to keep my function generic, I want to set the initial value to whatever is passed in when the function is called, then keep comparing.

 

new = ''
def autoIncrementbyAttr(attribute):
    global new
    exist = attribute
    if (exist == new):
        return autoIncrement()
    else:
        new = attribute
        rec=0

rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd  
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):  
  rec = pStart  
else:  
  rec = rec + pInterval  
return rec

Outcomes