sequential difference in a single column with field calculator

1457
4
Jump to solution
05-27-2018 08:45 AM
youngpark
New Contributor III

Hello,

I have a column for elevation for a point data set.  I would like to create a new column to show a difference between Row(n) and Row(n+1) values.  well, I assume that the easiest way is calculating in EXCEL (=abs(A1-A2)) then make a join at ArcGIS.  However, I would like to the process with "Field calculator"   Does anyone know how to do it? 

Thanks in advance

Tags (1)
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

To clarify something... you cannot read the next row and store the result in the current row. You can however, remember the previous row and store the difference in the current row. See the example below in ArcGIS Pro (same thing will apply in ArcMap):

You will have to use the Python parser and use the following code in the code block:

prev_val = None
def SequentialDif(curr_val):
    global prev_val
    if (prev_val == None): 
        prev_val = curr_val 
    dif_val = curr_val - prev_val
    prev_val = curr_val
    return dif_val

And use this as expression:

SequentialDif(!YourFieldName!)

Each record will have the difference between the current and the previous record of the given field.

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

To clarify something... you cannot read the next row and store the result in the current row. You can however, remember the previous row and store the difference in the current row. See the example below in ArcGIS Pro (same thing will apply in ArcMap):

You will have to use the Python parser and use the following code in the code block:

prev_val = None
def SequentialDif(curr_val):
    global prev_val
    if (prev_val == None): 
        prev_val = curr_val 
    dif_val = curr_val - prev_val
    prev_val = curr_val
    return dif_val

And use this as expression:

SequentialDif(!YourFieldName!)

Each record will have the difference between the current and the previous record of the given field.

DanPatterson_Retired
MVP Emeritus
0 Kudos
DanPatterson_Retired
MVP Emeritus

and if you need the absolute difference, you can get that as well during the calculation process

Python parser of course

Code block below

Expression .... diff_between( !YourFieldName!, absol=True)

""" -----------------------------------------
diff_between(fld)
input:      a field
returns:    difference between successive records
expression: diff_between(!Shape!)
"""
val = 0.0
is_first = True
def diff_between(fld, absol):
    global val
    global is_first
    if is_first:
        is_first = False
        val = fld
        return 0.0
    diff = fld - val
    val = fld
    #val = diff
    if absol:
        return abs(diff)
    else:
        return diff
#__esri_field_calculator_splitter__
#diff_between(!YourFieldHere!, True)
youngpark
New Contributor III

Thanks Dan,

I should have changed just the  field of question.  I really appreciate your helps.

0 Kudos