Calculating Cumulative Field Values in ArcMap using a case field

1197
3
Jump to solution
05-09-2020 05:42 AM
SlobodanKomatina2
New Contributor II

I want to calculate cumulative field values using an case field from another column in ArcGIS 10.4. 

For example, if I have an "ID" column and "Count" column inside my attribute table, I want to calculate cumulative values for each year separately. 

I have used this:

How to: Calculate cumulative shape length values of a feature class using the Field Calculator 

I can do that if I select all rows with the same ID and run the field calculator, but that is a lot of manual work, so I want to use "ID" field as case field. 

What is the best way to do that?

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor
count = 0
id_list =[]
def cummulativeCount(increment, id_row_value):
    global count
    global id_list

   
    id_list.append(id_row_value)

    if len(id_list)>=2 and id_row_value == id_list[-2]: 
        count += int(increment)
        

    elif len(id_list)<2:
        count += int(increment)
        

    else:
        count = int(increment)
        

    return count

cummulativeCount( !count_! , !id! )

View solution in original post

0 Kudos
3 Replies
DavidPike
MVP Frequent Contributor
count = 0
id_list =[]
def cummulativeCount(increment, id_row_value):
    global count
    global id_list

   
    id_list.append(id_row_value)

    if len(id_list)>=2 and id_row_value == id_list[-2]: 
        count += int(increment)
        

    elif len(id_list)<2:
        count += int(increment)
        

    else:
        count = int(increment)
        

    return count

cummulativeCount( !count_! , !id! )

0 Kudos
SlobodanKomatina2
New Contributor II

Thank you very much!

0 Kudos
DavidPike
MVP Frequent Contributor

нема на чему