<?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: Arcade Data Expression - Return Predominant Value while Grouping By Another Field in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1158619#M6067</link>
    <description>&lt;P&gt;Maybe no one is reading this one anymore, but I revisited it.&lt;/P&gt;&lt;P&gt;I couldn't stop thinking about this one so I gave it a go in a Dashboard and it turned out to be much harder than I thought it would be.&amp;nbsp; I couldn't figure out how to do it just using GroupBy and OrderBy when in the Dashboard environment (long story but I couldn't get my Total Incidents, Predominant Incident Type, and Incident Name all in the same FeatureSet using this method... not saying its not possible.&amp;nbsp; I just couldn't figure it out).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I found this great sample on github:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Esri/arcade-expressions/blob/master/dashboard_data/CombineMultipleLayers(SerialChart).md" target="_blank"&gt;arcade-expressions/CombineMultipleLayers(SerialChart).md at master · Esri/arcade-expressions · GitHub&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Wow... Very cool!&amp;nbsp; It shows you how to return your own custom FeatureSet from a dictionary.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using these two fields in my sample data:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_0-1648505435390.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37516i1CF16F78E3B644A9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_0-1648505435390.png" alt="KimGarbade_0-1648505435390.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And this code to define the data source to use for my list:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Write an expression that returns a FeatureSet.
// Documentation: https://arcg.is/3c419TD
// Samples: https://arcg.is/38SEWWz

//----------------------------------------------------------
//Portal feature set
//----------------------------------------------------------
var IncidentsByCounty = GroupBy(FeatureSetByPortalItem(Portal('https://garbadehome.maps.arcgis.com'),'baa95b02c52147bf8962379e85831fe8',0,['County','Incident'], True), 
  [ //fields/expressions to group statistics by
    {name: 'County', expression: 'County'},
    {name: 'Incident', expression: 'Incident'}
  ],
  [ // statistics to return for each unique categroy
    {name: 'CountOfInc', expression: 'Incident', statistic: 'COUNT'}
  ]
)

var cntyNoIncident = GroupBy(IncidentsByCounty, 
  [ //fields/expressions to group statistics by
    {name: 'County', expression: 'County'}
  ],
  [ // statistics to return for each unique categroy
    {name: 'Predom', expression: 'CountOfInc', statistic: 'MAX'},
    {name: 'TotalInc', expression: 'CountOfInc', statistic: 'SUM'}
  ]
)

// Create empty array for features, feat object to populate array
var features = [];
var feat;

for(var cnty in cntyNoIncident){
    var countyname = cnty.County
    var predomCount = cnty.Predom
    var incidentTotal = cnty.TotalInc
    var predomIncType = filter(IncidentsByCounty,"County = @countyname AND CountOfInc = @predomCount")
    var x = []
    for (var topIncident in predomIncType){
      Push (x, topIncident.Incident)
    }
    feat = {  
        attributes: {
            County: countyname,
            PredomIncidentType: Concatenate(x,'/'),
            PredomIncidentTotal: predomCount,
            TotalIncidents: incidentTotal,
        },
    }
    Push(features, feat);
}

var outputDict = {
    fields: [
        {name: 'County', type: 'esriFieldTypeString'},
        {name: 'PredomIncidentType', type: 'esriFieldTypeString'},
        {name: 'PredomIncidentTotal', type: 'esriFieldTypeInteger'},
        {name: 'TotalIncidents', type: 'esriFieldTypeInteger'},
    ],
    geometryType: '',
    features: features,
}

return FeatureSet(Text(outputDict));
//return predomIncType
&lt;/LI-CODE&gt;&lt;P&gt;I generated this List:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_1-1648505612306.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37517iC59A4C47DFC614C9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_1-1648505612306.png" alt="KimGarbade_1-1648505612306.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I learned a bunch so thank you for the question.&lt;/P&gt;&lt;P&gt;K&lt;/P&gt;</description>
    <pubDate>Mon, 28 Mar 2022 22:14:23 GMT</pubDate>
    <dc:creator>KimberlyGarbade</dc:creator>
    <dc:date>2022-03-28T22:14:23Z</dc:date>
    <item>
      <title>Arcade Data Expression - Return Predominant Value while Grouping By Another Field</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1156474#M6009</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am working on a data expression in Dashboards (ArcGIS Online) that returns the Count of features by Location.&amp;nbsp; I have that part figured out, but I'd like to also return the predominant value from another field ('categories').&amp;nbsp; How would I go about doing that.?&amp;nbsp; Below is my expression that returns the features grouped by location.&lt;/P&gt;&lt;P&gt;Ideally, I want a &lt;STRONG&gt;List&lt;/STRONG&gt; that shows: "Country, Total Incidents, Predominant Incident Categories is ___."&lt;/P&gt;&lt;P&gt;Any advice is much appreciated, thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// return fs that is grouped by 'Location' and gets 'COUNT' of features for that location&lt;BR /&gt;var fs = FeatureSetByPortalItem(Portal('&lt;A href="https://myorg.maps.arcgis.com/" target="_blank" rel="noopener"&gt;https://myorg.maps.arcgis.com/&lt;/A&gt;'), 'myItemID', 0, ['Location'], false);&lt;/P&gt;&lt;P&gt;return GroupBy(fs, ['Location'],&lt;BR /&gt;[{name: 'total_incidents', expression: 'Location', statistic: 'COUNT' }&lt;BR /&gt;]);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Mar 2022 23:38:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1156474#M6009</guid>
      <dc:creator>cwlee27</dc:creator>
      <dc:date>2022-03-22T23:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Data Expression - Return Predominant Value while Grouping By Another Field</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1156572#M6011</link>
      <description>&lt;P&gt;I hope I understand what you mean by "&lt;SPAN&gt;Predominant Incident".&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I defined it to mean if you had 10 car crashes (for example 5 Multi Car, 3 Single Car, 1 Bicycle, 1 Pedestrian) the predominant type would be "Multi Car"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Another thing I will say is that I'm testing this on my home ArcGIS Pro single use install, so I didn't write the Arcade for a dashboard pulling from a portal, but the logic should be pretty similar.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I had test data laying around so I used&amp;nbsp; that and wrote an attribute rule to populate a new field named "Predominent" (spelled wrong) with the predominant value from the TextTst field (in the image below I updated the IntTest field from 6 to 7 to fire the Arcade to populate the Predominent field)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_0-1648032869813.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37076i6979476744691AEA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_0-1648032869813.png" alt="KimGarbade_0-1648032869813.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The expression I used uses OrderBy (DESCending) to order the feature set returned by the GroupBy function in descending order from most to least occurrences of COUNT.&amp;nbsp; The First function just takes the first record from this ordered feature set.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only problem is if you have a tie its only going to take the first one.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_1-1648033093387.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37077iC7A228AD4CD408A1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_1-1648033093387.png" alt="KimGarbade_1-1648033093387.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here it is in plain text:&lt;/P&gt;&lt;P&gt;return First(OrderBy(GroupBy((FeatureSetByName($datastore, 'TestPoints',['TxtTest'],false)), 'TxtTest',{name: 'NumberTest', expression: '1', statistic: 'COUNT'}),'NumberTest DESC')).TxtTest&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;P&gt;K&lt;/P&gt;</description>
      <pubDate>Wed, 23 Mar 2022 11:08:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1156572#M6011</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2022-03-23T11:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Data Expression - Return Predominant Value while Grouping By Another Field</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1158619#M6067</link>
      <description>&lt;P&gt;Maybe no one is reading this one anymore, but I revisited it.&lt;/P&gt;&lt;P&gt;I couldn't stop thinking about this one so I gave it a go in a Dashboard and it turned out to be much harder than I thought it would be.&amp;nbsp; I couldn't figure out how to do it just using GroupBy and OrderBy when in the Dashboard environment (long story but I couldn't get my Total Incidents, Predominant Incident Type, and Incident Name all in the same FeatureSet using this method... not saying its not possible.&amp;nbsp; I just couldn't figure it out).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I found this great sample on github:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Esri/arcade-expressions/blob/master/dashboard_data/CombineMultipleLayers(SerialChart).md" target="_blank"&gt;arcade-expressions/CombineMultipleLayers(SerialChart).md at master · Esri/arcade-expressions · GitHub&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Wow... Very cool!&amp;nbsp; It shows you how to return your own custom FeatureSet from a dictionary.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using these two fields in my sample data:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_0-1648505435390.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37516i1CF16F78E3B644A9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_0-1648505435390.png" alt="KimGarbade_0-1648505435390.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And this code to define the data source to use for my list:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Write an expression that returns a FeatureSet.
// Documentation: https://arcg.is/3c419TD
// Samples: https://arcg.is/38SEWWz

//----------------------------------------------------------
//Portal feature set
//----------------------------------------------------------
var IncidentsByCounty = GroupBy(FeatureSetByPortalItem(Portal('https://garbadehome.maps.arcgis.com'),'baa95b02c52147bf8962379e85831fe8',0,['County','Incident'], True), 
  [ //fields/expressions to group statistics by
    {name: 'County', expression: 'County'},
    {name: 'Incident', expression: 'Incident'}
  ],
  [ // statistics to return for each unique categroy
    {name: 'CountOfInc', expression: 'Incident', statistic: 'COUNT'}
  ]
)

var cntyNoIncident = GroupBy(IncidentsByCounty, 
  [ //fields/expressions to group statistics by
    {name: 'County', expression: 'County'}
  ],
  [ // statistics to return for each unique categroy
    {name: 'Predom', expression: 'CountOfInc', statistic: 'MAX'},
    {name: 'TotalInc', expression: 'CountOfInc', statistic: 'SUM'}
  ]
)

// Create empty array for features, feat object to populate array
var features = [];
var feat;

for(var cnty in cntyNoIncident){
    var countyname = cnty.County
    var predomCount = cnty.Predom
    var incidentTotal = cnty.TotalInc
    var predomIncType = filter(IncidentsByCounty,"County = @countyname AND CountOfInc = @predomCount")
    var x = []
    for (var topIncident in predomIncType){
      Push (x, topIncident.Incident)
    }
    feat = {  
        attributes: {
            County: countyname,
            PredomIncidentType: Concatenate(x,'/'),
            PredomIncidentTotal: predomCount,
            TotalIncidents: incidentTotal,
        },
    }
    Push(features, feat);
}

var outputDict = {
    fields: [
        {name: 'County', type: 'esriFieldTypeString'},
        {name: 'PredomIncidentType', type: 'esriFieldTypeString'},
        {name: 'PredomIncidentTotal', type: 'esriFieldTypeInteger'},
        {name: 'TotalIncidents', type: 'esriFieldTypeInteger'},
    ],
    geometryType: '',
    features: features,
}

return FeatureSet(Text(outputDict));
//return predomIncType
&lt;/LI-CODE&gt;&lt;P&gt;I generated this List:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_1-1648505612306.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37517iC59A4C47DFC614C9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_1-1648505612306.png" alt="KimGarbade_1-1648505612306.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I learned a bunch so thank you for the question.&lt;/P&gt;&lt;P&gt;K&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 22:14:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1158619#M6067</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2022-03-28T22:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Data Expression - Return Predominant Value while Grouping By Another Field</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1158782#M6070</link>
      <description>&lt;P&gt;Kim - thank you so much, this is great! I'm going to try to implement this on my end, I'll let you know how it goes!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 12:27:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-return-predominant-value/m-p/1158782#M6070</guid>
      <dc:creator>cwlee27</dc:creator>
      <dc:date>2022-03-29T12:27:51Z</dc:date>
    </item>
  </channel>
</rss>

