<?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: GroupBy() Function Trouble in ArcGIS Online Developers Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357710#M1302</link>
    <description>&lt;P&gt;Showing that other table makes it a little more understandable. In that case, leave off that second GroupBy expression and use your original version.&lt;/P&gt;&lt;P&gt;What is the error you're getting on line 7?&lt;/P&gt;</description>
    <pubDate>Wed, 06 Dec 2023 21:43:58 GMT</pubDate>
    <dc:creator>KenBuja</dc:creator>
    <dc:date>2023-12-06T21:43:58Z</dc:date>
    <item>
      <title>GroupBy() Function Trouble</title>
      <link>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357641#M1299</link>
      <description>&lt;P&gt;I have a data set "sourceTable" with RoadNumbers associated for each entry. Each entry has an Individual_Removal_Score. I want to combine all entries based on RoadNumber and SUM the Individual_Removal_Score values to create one record for each RoadNumber with a Total_Score. I then want to filter "stats" to lookup RoadNumbers that match the ID field of the target layer and return the Total_Score. This is being done as an attribute rule in pro for a field in the target dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;//Reference Layer and relevant fields
var sourceTable = FeatureSetByName($datastore, "TNF_RoadConditionAssessment", ["RoadNumber", "Individual_Removal_Score"],false);
//key field for target layer
var ID = $feature.ID;
//statistic variable target layer
var Miles = $feature.Total_Miles;
//Group reference layer by RoadNumber and SUM Individual_Removal_Scores for each entry
var stats = groupby(sourceTable, 'RoadNumber',
{name: 'Total_Score', expression: 'Individual_Removal_Score', statistic: 'SUM'})
//Filter Summary table(stats) by RoadNumber where value is equal to the key field "ID"
var Lookup = First(Filter(stats, "RoadNumber = &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/354972"&gt;@ID&lt;/a&gt;"));
//return Total_Score/Miles if key field has a match
return iif(Lookup == null, null, ((Lookup.Total_Score)/Miles));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Script verifies but won't save because of an error on line 13 "Attribute value lookup error". I have used this exact script without the GroupBy() function just fine so I'm at a loss. I'm assuming the GroupBy() table isn't being returned properly so there is nothing to reference? Wondering if the field "Total_Miles" needs to be created physically somewhere or if it gets created in memory while its running. The way I understand it is the GroupBy() function creates a new temp FeatureSet. I thought I was creating a FeatureSet with RoadNumber and Total_Miles (being a sum of "Individual_Removal_Score") as the fields but my thinking is that it's breaking down because there are multiple entries with the same roadnumber? (Screen shot example of the table I'm attempting to group below)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 431px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/88208i02BDCEA30BFEB59C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks for the input!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 20:50:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357641#M1299</guid>
      <dc:creator>amrodri01</dc:creator>
      <dc:date>2023-12-06T20:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: GroupBy() Function Trouble</title>
      <link>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357675#M1300</link>
      <description>&lt;P&gt;If you have a field in your table that contains the mileage for each road, you would add that to the GroupBy function. Or you could use the "SHAPE__Length" field. However, since that won't be in miles, you would have to convert that in the Expression. Here, I'm making the conversion from meters to miles&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var stats = groupby(sourceTable, 'RoadNumber',
  [
    {name: 'Total_Score', expression: 'Individual_Removal_Score', statistic: 'SUM'},
    {name: 'Total_Miles', expression: 'SHAPE__Length * 0.000621371', statistic: 'SUM'}
  ]
);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then in the final line, you would use that summarized length&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;return iif(Lookup == null, null, ((Lookup.Total_Score)/Lookup.Total_Miles));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 20:51:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357675#M1300</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-12-06T20:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: GroupBy() Function Trouble</title>
      <link>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357700#M1301</link>
      <description>&lt;P&gt;I do have a field in the sourceTable that has the mileage of the road based on the roadnumber. I guess I'm not sure how it would display properly because the sourceTable is a point layer and the target layer is a line layer. Both layers have their own fields with the total mileage of the road (not shape length). If I were to GroupBy() the ML1_Road_Miles wouldn't that just sum the mileage and not have it a single value? Thats why I had it referencing the line FeatureSet "Total_Miles" as a variable. Maybe an average instead which would give it the correct value?&lt;/P&gt;&lt;P&gt;Below is a snip of the line FeatureSet. This script is writing to the "Storage_Score field&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture2.PNG" style="width: 365px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/88219i878210CB653213D3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture2.PNG" alt="Capture2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Below is a snip of the point FeatureSet "sourceTable" w/ the Road Mileage&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture3.PNG" style="width: 397px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/88221i096B37142943C49D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture3.PNG" alt="Capture3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even still the filter function displays an error on line 7. Below is how I have it now, but it doesn't seem right.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var sourceTable = FeatureSetByName($datastore, "TNF_RoadConditionAssessment", ["RoadNumber", "Individual_Removal_Score", "ML1_Road_Miles"],false);
var ID = $feature.ID;
var stats = groupby(sourceTable, 'RoadNumber', 
[{name: 'Total_Score', expression: 'Individual_Removal_Score', statistic: 'SUM'},
{name: 'Total_Miles', expression: 'ML1_Road_Miles', statistic: 'SUM'}])

var Lookup = First(Filter(stats, "RoadNumber = &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/354972"&gt;@ID&lt;/a&gt;"));

return iif(Lookup == null, null, ((Lookup.Total_Score)/Lookup.Total_Miles));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 21:22:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357700#M1301</guid>
      <dc:creator>amrodri01</dc:creator>
      <dc:date>2023-12-06T21:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: GroupBy() Function Trouble</title>
      <link>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357710#M1302</link>
      <description>&lt;P&gt;Showing that other table makes it a little more understandable. In that case, leave off that second GroupBy expression and use your original version.&lt;/P&gt;&lt;P&gt;What is the error you're getting on line 7?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 21:43:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357710#M1302</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-12-06T21:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: GroupBy() Function Trouble</title>
      <link>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357762#M1303</link>
      <description>&lt;P&gt;Gotcha, okay with the original script it was referencing line 11. Just kind of weird since it only shows in the attribute rules pane and not in the expression entry window.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture4.PNG" style="width: 614px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/88236iDE1AFC3EE6777975/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture4.PNG" alt="Capture4.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 23:05:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1357762#M1303</guid>
      <dc:creator>amrodri01</dc:creator>
      <dc:date>2023-12-06T23:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: GroupBy() Function Trouble</title>
      <link>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1358291#M1304</link>
      <description>&lt;P&gt;Figured out the solution, needed to filter the multiple RoadNumber records into a table with just a single matching ID (Line 8 ) then the rest of the script functioned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;//Reference Layer and relevant fields
var sourceTable = FeatureSetByName($datastore, "TNF_RoadConditionAssessment", ["RoadNumber", "Individual_Removal_Score"],false);
//key field for target layer
var ID = $feature.ID;
//statistic variable target layer
var Miles = $feature.Total_Miles;
//Filter all features that equal road ID
var Lookupall = Filter(sourceTable, "RoadNumber = &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/354972"&gt;@ID&lt;/a&gt;")
//Group reference layer by RoadNumber and SUM Individual_Removal_Scores for each entry
var stats = groupby(Lookupall,'RoadNumber',
{name: 'Total_Score', expression: 'Individual_Removal_Score', statistic: 'SUM'})
//Filter Summary table(stats) by RoadNumber where value is equal to the key field "ID"
var Lookup = first(Filter(stats, "RoadNumber = &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/354972"&gt;@ID&lt;/a&gt;"))
//return Total_Score/Miles if key field has a match
return iif(Lookup == null, 0, (Lookup.Total_Score/Miles));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2023 17:05:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-developers-questions/groupby-function-trouble/m-p/1358291#M1304</guid>
      <dc:creator>amrodri01</dc:creator>
      <dc:date>2023-12-08T17:05:42Z</dc:date>
    </item>
  </channel>
</rss>

