<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Field Calculator Subtract Value From Previous Value in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378874#M12952</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The sort ascending must also be done in the field calculator before doing the calculation.&amp;nbsp; &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry, Stuart, I have bad news for you. The Calculate Field&amp;nbsp; code block is executed one row at a time - so you are dependent on the sort order of the input table. (The Sort tool was (finally) introduced at 10.0 -- I have wanted it since 8.0 was released!)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Seems to me the best approach to this would be to do this in Python with a sorted update cursor instead of Calculate Field. You could create a script tool, but I bet you can get by using the Calculate Value tool in Model Builder:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression: &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; font-family: Courier New;"&gt;CalcDiff(r"pipetable.dbf", "PIPE_LENGTH", "DISTANCE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcgisscripting
gp = arcgisscripting.create(9.3)
def CalcDiff(tbl,valField,diffField):
&amp;nbsp; procFields = valField + ";" + diffField
&amp;nbsp; sortField = valField
&amp;nbsp; Rows = gp.UpdateCursor(tbl, "", "", procFields, sortField)
&amp;nbsp; lastVal = 0
&amp;nbsp; for Row in Rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; val = float(Row.getValue(valField))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # calc the difference between the last value and this one
&amp;nbsp;&amp;nbsp;&amp;nbsp; diff =&amp;nbsp; val - lastVal
&amp;nbsp;&amp;nbsp;&amp;nbsp; Row.setValue(diffField,diff)
&amp;nbsp;&amp;nbsp;&amp;nbsp; Rows.updateRow(Row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; lastVal = val
&amp;nbsp; del Row, Rows
&amp;nbsp; return tbl
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Data Type: &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;Table&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:29:55 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2021-12-11T17:29:55Z</dc:date>
    <item>
      <title>Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378873#M12951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a field in my attribute table representing pipe lengths.&amp;nbsp; In an empty field (Distance) I would like to subtract each pipe length from the previous value, thereby calculating the distance.&amp;nbsp; Also, in the field calculator, I need to be able to sort the Pipe_Length field in ascending order before doing the calculation.&amp;nbsp; For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pipe_Length.........Distance&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;0........................0&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1615...................1615&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4494...................2879&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4630...................136&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have ArcGIS 9.3 and this MUST be done in the &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;field calculator&lt;/SPAN&gt;&lt;SPAN&gt;.&amp;nbsp; I have no access to ArcGIS 10 nor can I download an evaluation copy of it.&amp;nbsp; It must be done using &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;9.3&lt;/SPAN&gt;&lt;SPAN&gt; in the &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;field calculator&lt;/SPAN&gt;&lt;SPAN&gt;.&amp;nbsp; The sort ascending must also be done in the field calculator before doing the calculation.&amp;nbsp; Unfortunately I am not able to divert from 9.3 or the field calculator.&amp;nbsp; The result must be exactly how it shows in the above example.&amp;nbsp; My VB logic skills are pretty weak so that it why I am posting here!&amp;nbsp; Thank you for your help!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Mar 2013 13:17:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378873#M12951</guid>
      <dc:creator>StuartBlumberg</dc:creator>
      <dc:date>2013-03-29T13:17:15Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378874#M12952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The sort ascending must also be done in the field calculator before doing the calculation.&amp;nbsp; &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry, Stuart, I have bad news for you. The Calculate Field&amp;nbsp; code block is executed one row at a time - so you are dependent on the sort order of the input table. (The Sort tool was (finally) introduced at 10.0 -- I have wanted it since 8.0 was released!)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Seems to me the best approach to this would be to do this in Python with a sorted update cursor instead of Calculate Field. You could create a script tool, but I bet you can get by using the Calculate Value tool in Model Builder:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression: &lt;/SPAN&gt;&lt;SPAN style="font-style: italic; font-family: Courier New;"&gt;CalcDiff(r"pipetable.dbf", "PIPE_LENGTH", "DISTANCE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcgisscripting
gp = arcgisscripting.create(9.3)
def CalcDiff(tbl,valField,diffField):
&amp;nbsp; procFields = valField + ";" + diffField
&amp;nbsp; sortField = valField
&amp;nbsp; Rows = gp.UpdateCursor(tbl, "", "", procFields, sortField)
&amp;nbsp; lastVal = 0
&amp;nbsp; for Row in Rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; val = float(Row.getValue(valField))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # calc the difference between the last value and this one
&amp;nbsp;&amp;nbsp;&amp;nbsp; diff =&amp;nbsp; val - lastVal
&amp;nbsp;&amp;nbsp;&amp;nbsp; Row.setValue(diffField,diff)
&amp;nbsp;&amp;nbsp;&amp;nbsp; Rows.updateRow(Row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; lastVal = val
&amp;nbsp; del Row, Rows
&amp;nbsp; return tbl
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Data Type: &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;Table&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:29:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378874#M12952</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T17:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378875#M12953</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Curt,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for responding!&amp;nbsp; I am very interested in your recommendation of the Calculate Value tool.&amp;nbsp; I tried running the tool using the expression and code you provided, however, I am having some issues.&amp;nbsp; I am getting the following error: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000539: Error running expression: CalcDiff(r"Export_Output.dbf", "PIPELENGTH", "DISTANCE") &amp;lt;type 'exceptions.RuntimeError'&amp;gt;: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A locator with this name does not exist.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (CalculateValue).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Do I need to change anything in the Code Block to reflect my specific data?&amp;nbsp; Does the table need to be in the ArcMAP TOC?&amp;nbsp; My Python skills are similar to my VBA skills.....weak!&amp;nbsp; Thanks for your help on this.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Apr 2013 17:53:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378875#M12953</guid>
      <dc:creator>StuartBlumberg</dc:creator>
      <dc:date>2013-04-01T17:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378876#M12954</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Stuart, I don't see the problem, two things to check:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1. Make sure your dbf table listed there is in the current workspace or a fully specified path, for example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-family:Courier New;"&gt;CalcDiff(r"C:\workspace\pipetable.dbf", "PIPE_LENGTH", "DISTANCE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;if this is a model element named &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;Input table&lt;/SPAN&gt;&lt;SPAN&gt; you can do this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-family:Courier New;"&gt;CalcDiff(r"%Input table%", "PIPE_LENGTH", "DISTANCE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Function and variable names case-sensitive in Python, so make sure you have the caps right.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Apr 2013 20:08:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378876#M12954</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2013-04-01T20:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378877#M12955</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Curt,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried using the fully specified path of the table and I got this error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000539: Error running expression: CalcDiff(r"C:\Export_Output.dbf", "PIPELENGTH", "DISTANCE") &amp;lt;type 'exceptions.TypeError'&amp;gt;: 'geoprocessing cursor object' object is not iterable&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (CalculateValue).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 02 Apr 2013 15:28:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378877#M12955</guid>
      <dc:creator>StuartBlumberg</dc:creator>
      <dc:date>2013-04-02T15:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378878#M12956</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; ERROR 000539: Error running expression: CalcDiff(r"C:\Export_Output.dbf", "PIPELENGTH", "DISTANCE") &amp;lt;type 'exceptions.TypeError'&amp;gt;: 'geoprocessing cursor object' object is not iterable&amp;nbsp; &lt;BR /&gt;Failed to execute (CalculateValue).&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry about that -- I forgot to use 9.3 syntax for cursors. Need to go look at the &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=849&amp;amp;pid=775&amp;amp;topicname=SearchCursor_method" rel="nofollow" target="_blank"&gt;9.3 help...&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcgisscripting gp = arcgisscripting.create(9.3) def CalcDiff(tbl,valField,diffField): &amp;nbsp; procFields = valField + ";" + diffField &amp;nbsp; sortField = valField &amp;nbsp; Rows = gp.UpdateCursor(tbl, "", "", procFields, sortField) &amp;nbsp; lastVal = 0 &amp;nbsp; Row = Rows.next() &amp;nbsp; while Row: &amp;nbsp;&amp;nbsp;&amp;nbsp; val = float(Row.getValue(valField)) &amp;nbsp;&amp;nbsp;&amp;nbsp; # calc the difference between the last value and this one &amp;nbsp;&amp;nbsp;&amp;nbsp; diff =&amp;nbsp; val - lastVal &amp;nbsp;&amp;nbsp;&amp;nbsp; Row.setValue(diffField,diff) &amp;nbsp;&amp;nbsp;&amp;nbsp; Rows.updateRow(Row) &amp;nbsp;&amp;nbsp;&amp;nbsp; lastVal = val &amp;nbsp;&amp;nbsp;&amp;nbsp; Row = Rows.next() &amp;nbsp; del Row, Rows &amp;nbsp; return tbl&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 02 Apr 2013 15:50:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378878#M12956</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2013-04-02T15:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378879#M12957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That worked!!&amp;nbsp; Thank you very much for your help on this!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Apr 2013 18:01:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378879#M12957</guid>
      <dc:creator>StuartBlumberg</dc:creator>
      <dc:date>2013-04-03T18:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378880#M12958</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Curt,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You're probably going to think I'm crazy but now I need to do this in 10.1.&amp;nbsp; What is the syntax I should be using?&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Apr 2013 19:26:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378880#M12958</guid>
      <dc:creator>StuartBlumberg</dc:creator>
      <dc:date>2013-04-15T19:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Subtract Value From Previous Value</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378881#M12959</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&amp;nbsp;&amp;nbsp; What is the syntax I should be using?&amp;nbsp; Thanks.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fortunately, scripts written for 9.3 (using arcgisscripting) should work fine at 10.x. This includes scripts dropped into the Calculate Value tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's it is set up for arcpy, anyway, just so you can see the slight difference.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
def CalcDiff(tbl,valField,diffField):
&amp;nbsp; procFields = valField + ";" + diffField
&amp;nbsp; sortField = valField
&amp;nbsp; Rows = gp.UpdateCursor(tbl, "", "", procFields, sortField)
&amp;nbsp; lastVal = 0
&amp;nbsp; while Row:
&amp;nbsp;&amp;nbsp;&amp;nbsp; val = float(Row.getValue(valField))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # calc the difference between the last value and this one
&amp;nbsp;&amp;nbsp;&amp;nbsp; diff =&amp;nbsp; val - lastVal
&amp;nbsp;&amp;nbsp;&amp;nbsp; Row.setValue(diffField,diff)
&amp;nbsp;&amp;nbsp;&amp;nbsp; Rows.updateRow(Row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; lastVal = val
&amp;nbsp; del Row, Rows
&amp;nbsp; return tbl
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ArcGIS 10.1 has a new cursor that is faster (arcpy.da.SearchCursor) if speed is an issue.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:29:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-subtract-value-from-previous/m-p/378881#M12959</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T17:29:58Z</dc:date>
    </item>
  </channel>
</rss>

