# How to calculate cumulative decreasing sum in ArcGIS-10.2

I need to calculate the cumulative decreasing sum as a field in a rasters attribute table. The cumulative decreasing sum is the accumulation of the last values to the first, so it is basically the ordinary cumulative (or cumulative increasing) sum in reverse order. I recently asked a similar question; how to calculate the ordinary cumulative sum, which was answered by pasting the following in the pre-logic script box of the field calculator,

``````total = 0
def cumsum(inc):
global total
total+=inc
``````

before running the "cumsum" operation on the source field.

This works well. I would prefer if someone could adapt this script to calculate the cumulative decreasing sum instead, but I'm open to any suggestions.

Please note that I have very limited scripting tools, so if the solution you provide is not simply an adaptation of the above script, please detail your solution in a step by step manner.

• So you want to start with the full sum and subtract each value in order? Oct 9, 2014 at 3:57

Edited: this expression relies on knowing the total (23) beforehand, but would be possible to calculate in the script, if needed. For the example series in the comments (3, 6, 5, 9) this expression returns 23, 20, 14, 9.

``````total = 23
prev = 0
def cumsum(inc):
global total
global prev
total -= prev
prev = inc
``````
• When I do this, the first value in the sequence of cumulative sums (the value in the last row of the field) is zero, the accumulation begins after this. For example, if I have the sequence: 3, 6, 5, 9, The result of the function you have suggested is: 20, 14, 9, 0 when I need it to be: 23, 20, 14, 9 Therefore your method doesn't quite accomplish the task. Thanks for trying to help me out.
– jc22
Oct 9, 2014 at 9:22
• See edited answer above. Oct 9, 2014 at 16:23
• Thanks for that, I was able to find my own solution in the time it took for you to respond, I'm going to assume that this is correct and accept this solution.
– jc22
Oct 10, 2014 at 13:41

Okay, I found my own solution not as straight forward as phloem's but it works:

1. Make a new table which contains a field with values identical to those of a field in the original table but in reverse order (back to front)

2. Copy this new table (so you can repeat the procedure)

3. Join the field which contains the values you want to calculate the decreasing cumulative sum of to the copied table, using the field which is reversed in the new table as the output join field, and the reversed field itself as the input join field. (this just flips the field)

4. Give a new field in the new table the exact values of the joined field (just to change the name)

5. Run the "cumsum" function given in the question on the renamed field (this calculates the cumulative decreasing sum, only the field is back to front)

6. Join the field back to the original the same way (this flips it back to the correct order)