<?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 with Python or SQL in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072991#M25640</link>
    <description>&lt;P&gt;Questions along these lines get asked fairly regularly on the Esri Community, probably every 4-6 months.&amp;nbsp; Although it is possible to answer your question using Field Calculator, it is quite a clunky approach in my mind.&amp;nbsp; This kind of question is best suited for ArcPy cursors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lyr = # name of layer or path to data set
flds = ["ID", "AdjRiverLe", "River"]
sql = "ORDER BY ID, AdjRiverLe DESC"

with arcpy.da.UpdateCursor(lyr, flds, sql_clause=(None, sql)) as cur:
    rid, adj_river, river = next(cur)
    river = "YES"
    cur.updateRow([rid, adj_river, river])
    prev_rid = rid
    
    for rid, adj_river, _ in cur:
        if rid == prev_rid:
            river = "NO"
        else:
            river = "YES"
        cur.updateRow([rid, adj_river, river])
        prev_rid = rid&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 27 Jun 2021 16:25:44 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2021-06-27T16:25:44Z</dc:date>
    <item>
      <title>Field calculator with Python or SQL</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072969#M25637</link>
      <description>&lt;P&gt;Hi mates,&lt;/P&gt;&lt;P&gt;I would like to calculate/fill the fields- 'River' with the values either 'Yes' or 'No' depending on the maximum value in the field named 'AdjRiverLe' and the field named 'ID'. See the image here.&lt;/P&gt;&lt;P&gt;The rule will be:&lt;/P&gt;&lt;P&gt;Yes= The maximum value in the field 'AdjRiverLe' group by the field 'ID'&lt;/P&gt;&lt;P&gt;No= the rest of the values in the field 'AdjRiverLe'&lt;/P&gt;&lt;P&gt;For example, the ID 1 has three individual rows with three different values in the field- 'AdjRiverLe '.&amp;nbsp; I would like to select the row which belongs to the maximum values in the field - 'AdjRiverLe' and fill the field 'River' with 'Yes'.&lt;/P&gt;&lt;P&gt;I reckon the code block option would work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 03:25:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072969#M25637</guid>
      <dc:creator>MDBaky</dc:creator>
      <dc:date>2021-06-27T03:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator with Python or SQL</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072976#M25638</link>
      <description>&lt;P&gt;Yes, the code block would work for your situation.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 09:56:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072976#M25638</guid>
      <dc:creator>Waffle_House</dc:creator>
      <dc:date>2021-06-27T09:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator with Python or SQL</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072979#M25639</link>
      <description>&lt;P&gt;Thanks Waffle for you reply. But would you please help me to figure the code block writing out?&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 11:39:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072979#M25639</guid>
      <dc:creator>MDBaky</dc:creator>
      <dc:date>2021-06-27T11:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator with Python or SQL</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072991#M25640</link>
      <description>&lt;P&gt;Questions along these lines get asked fairly regularly on the Esri Community, probably every 4-6 months.&amp;nbsp; Although it is possible to answer your question using Field Calculator, it is quite a clunky approach in my mind.&amp;nbsp; This kind of question is best suited for ArcPy cursors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lyr = # name of layer or path to data set
flds = ["ID", "AdjRiverLe", "River"]
sql = "ORDER BY ID, AdjRiverLe DESC"

with arcpy.da.UpdateCursor(lyr, flds, sql_clause=(None, sql)) as cur:
    rid, adj_river, river = next(cur)
    river = "YES"
    cur.updateRow([rid, adj_river, river])
    prev_rid = rid
    
    for rid, adj_river, _ in cur:
        if rid == prev_rid:
            river = "NO"
        else:
            river = "YES"
        cur.updateRow([rid, adj_river, river])
        prev_rid = rid&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Jun 2021 16:25:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1072991#M25640</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-06-27T16:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator with Python or SQL</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1073171#M25641</link>
      <description>&lt;P&gt;Since you mention SQL, is the layer in question in an Enterprise GeoDatabase, or is it a feature service? There are a couple of ways you might approach this, but it may depend on where the features are coming from.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jun 2021 13:44:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1073171#M25641</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-06-28T13:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Field calculator with Python or SQL</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1073173#M25642</link>
      <description>&lt;P&gt;Also, though you didn't mention Arcade, It's worth pointing out that this can be done simply enough in Arcade.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;// Get features w/ matching ID
var id = $feature.ID
var id_feats = Filter(FeatureSetByName($datastore, 'polygon-layer-name'), 'ID = &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/354972"&gt;@ID&lt;/a&gt;')

// Get maximum value of AdjRiverLe field
var max_le = Max(id_feats, 'AdjRiverLe')

// Assign 'Yes' to max value, 'No' to others
if($feature.AdjRiverLe == max_le){
    return 'Yes'
} else {
    return 'No'
}&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 28 Jun 2021 13:52:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/field-calculator-with-python-or-sql/m-p/1073173#M25642</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-06-28T13:52:24Z</dc:date>
    </item>
  </channel>
</rss>

