Select to view content in your preferred language

Generating a value by substracting the value of 2 records

409
2
Jump to solution
05-23-2023 11:24 AM
RudyJamet
Emerging Contributor

In one of the fields of my attribute table, I have been trying to fill the rows with the numerical difference between the value of 2 records from another field.

Basically, in a spreadsheet, the process would be B2 = A2 - A1, B3 = A3 - A2, B4 = A4 - A3, etc.

In my attribute table, the process would look like this : the value of OID 2 in field B equals the difference between the values of OID 2 & 1 in field A.

I don't know what the script, either in Arcade or Python, could be. Does anyone have an idea?

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Python:

#YourField = 
calc(!OBJECTID!)

#Code Block
last_val = None
def calc(val):
    global last_val
    diff = (val - last_val) if last_val is not None else None
    last_val = val
    return diff

 

Arcade

var oid = $feature.OBJECTID
var last_row = First(OrderBy(Filter($featureset, "OBJECTID < @oid"), "OBJECTID DESC"))
if(last_row == null) { return null }
return $feature.OBJECTID - last_row.OBJECTID

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

Python:

#YourField = 
calc(!OBJECTID!)

#Code Block
last_val = None
def calc(val):
    global last_val
    diff = (val - last_val) if last_val is not None else None
    last_val = val
    return diff

 

Arcade

var oid = $feature.OBJECTID
var last_row = First(OrderBy(Filter($featureset, "OBJECTID < @oid"), "OBJECTID DESC"))
if(last_row == null) { return null }
return $feature.OBJECTID - last_row.OBJECTID

Have a great day!
Johannes
RudyJamet
Emerging Contributor

Thanks Johannes. I used the Python script and it worked fine.

0 Kudos