<?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: Return the count of a string in multiple fields using data expression in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1387574#M9152</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I haven't worked with it but would a combination of the&amp;nbsp;&lt;A href="https://developers.arcgis.com/arcade/function-reference/featureset_functions/#filter" target="_self"&gt;Filter&lt;/A&gt; and &lt;A href="https://developers.arcgis.com/arcade/function-reference/featureset_functions/#groupby" target="_self"&gt;GroupBy&lt;/A&gt; functions be helpful?&lt;/P&gt;&lt;P&gt;This sample has an example of the kind of output you're looking for - &lt;A href="https://github.com/Esri/arcade-expressions/blob/master/dashboard_data/AggregateByDOW(SerialChart).md" target="_self"&gt;Aggregate by Day of Week&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Feb 2024 17:13:35 GMT</pubDate>
    <dc:creator>JillianStanford</dc:creator>
    <dc:date>2024-02-27T17:13:35Z</dc:date>
    <item>
      <title>Return the count of a string in multiple fields using data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1387510#M9149</link>
      <description>&lt;P&gt;I am trying to build a bar chart illustrating the number of times a string is recorded across multiple (19) fields. I would like to get the total number of times Planning, Organization, Equipment, Training, Exercise, and Unknown appear in POETE area (poete_area) through POETE area 19 (poete_area_19).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dwold_0-1709051300882.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/96007iDE165ABD49283845/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dwold_0-1709051300882.png" alt="dwold_0-1709051300882.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is what I have for my data expression so far but getting tripped up on it:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var portal = Portal('https://www.arcgis.com/');
var fs = FeatureSetByPortalItem(
    portal,
    '384158f2ebe34f8ba8b0b39dfe5f5af5',
    0,
    [
        'Jurisdiction',
        'core_capability',
        'target',
        'poete_area',
        'poete_area_2',
        'poete_area_3',
        'poete_area_4',
        'poete_area_5',
        'poete_area_6',
        'poete_area_7',
        'poete_area_8',
        'poete_area_9',
        'poete_area_10',
        'poete_area_11',
        'poete_area_12',
        'poete_area_13',
        'poete_area_14',
        'poete_area_15',
        'poete_area_16',
        'poete_area_17',
        'poete_area_18',
        'poete_area_19'
    ],
    false
);

function poeteCount(pa, type){
  Return IIf(pa==type, 1, 0)
}

var poeteType = ["Planning","Organization","Equipment","Training", "Exercise", "Unknown"];
var arrayLength = poeteType .length;
for (var i = 0; i &amp;lt; arrayLength; i++) {
console.log(poeteType [i]); 
}

for (var t in fs) {
// Console(IIf(t["poete_area"]=="Planning", 1, 0))

//Console(poeteCount(t["poete_area"],"Planning"))

var arrayLength = (poeteCount(t["poete_area"],"Planning")+poeteCount(t["poete_area_2"],"Planning"))
//Console(cntPlanning)
}

var poeteDict = {  
 fields: [
  { name: 'Jurisdiction', type: 'esriFieldTypeString' },  
  { name: 'core_capability',type: 'esriFieldTypeString'},
  { name: 'target',type: 'esriFieldTypeString'},  
  { name: 'poete_area',type: 'esriFieldTypeString'},
  { name: 'poete_area_2',type: 'esriFieldTypeString'},
  { name: 'poete_area_3',type: 'esriFieldTypeString'},
  { name: 'poete_area_4',type: 'esriFieldTypeString'},
  { name: 'poete_area_5',type: 'esriFieldTypeString'},
  { name: 'poete_area_6',type: 'esriFieldTypeString'},
  { name: 'poete_area_7',type: 'esriFieldTypeString'},
  { name: 'poete_area_8',type: 'esriFieldTypeString'},
  { name: 'poete_area_9',type: 'esriFieldTypeString'},
  { name: 'poete_area_10',type: 'esriFieldTypeString'},
  { name: 'poete_area_11',type: 'esriFieldTypeString'},
  { name: 'poete_area_12',type: 'esriFieldTypeString'},
  { name: 'poete_area_13',type: 'esriFieldTypeString'},
  { name: 'poete_area_14',type: 'esriFieldTypeString'},
  { name: 'poete_area_15',type: 'esriFieldTypeString'},
  { name: 'poete_area_16',type: 'esriFieldTypeString'},
  { name: 'poete_area_17',type: 'esriFieldTypeString'},
  { name: 'poete_area_18',type: 'esriFieldTypeString'},
  { name: 'poete_area_19',type: 'esriFieldTypeString'}
 ],  
 'geometryType': '',   
 'features': []
 };

 return poeteDict&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any tips would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Feb 2024 17:26:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1387510#M9149</guid>
      <dc:creator>dwold</dc:creator>
      <dc:date>2024-02-27T17:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: Return the count of a string in multiple fields using data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1387574#M9152</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I haven't worked with it but would a combination of the&amp;nbsp;&lt;A href="https://developers.arcgis.com/arcade/function-reference/featureset_functions/#filter" target="_self"&gt;Filter&lt;/A&gt; and &lt;A href="https://developers.arcgis.com/arcade/function-reference/featureset_functions/#groupby" target="_self"&gt;GroupBy&lt;/A&gt; functions be helpful?&lt;/P&gt;&lt;P&gt;This sample has an example of the kind of output you're looking for - &lt;A href="https://github.com/Esri/arcade-expressions/blob/master/dashboard_data/AggregateByDOW(SerialChart).md" target="_self"&gt;Aggregate by Day of Week&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Feb 2024 17:13:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1387574#M9152</guid>
      <dc:creator>JillianStanford</dc:creator>
      <dc:date>2024-02-27T17:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: Return the count of a string in multiple fields using data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1387763#M9157</link>
      <description>&lt;P&gt;I bet we could get this with a single GroupBy. We'll use a couple other tricks to keep the expression from getting too long, though.&lt;/P&gt;&lt;P&gt;The crux of it is using a SQL expression to count across fields, like this:&lt;/P&gt;&lt;PRE&gt;CASE WHEN poete_area = 'Planning' THEN 1 ELSE 0 END +&lt;BR /&gt;CASE WHEN poete_area2 = 'Planning' THEN 1 ELSE 0 END +&lt;BR /&gt;...&lt;BR /&gt;CASE WHEN poete_aera19 = 'Planning' THEN 1 ELSE 0 END&lt;/PRE&gt;&lt;P&gt;It's a long expression, and you'd have to do it for every single option you want counted up, so the arrays and functions can take care of that for us.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// list of areas; we'll splice it into the field list later
var areas = [
  "poete_area",
  "poete_area_2",
  "poete_area_3",
  "poete_area_4",
  "poete_area_5",
  "poete_area_6",
  "poete_area_7",
  "poete_area_8",
  "poete_area_9",
  "poete_area_10",
  "poete_area_11",
  "poete_area_12",
  "poete_area_13",
  "poete_area_14",
  "poete_area_15",
  "poete_area_16",
  "poete_area_17",
  "poete_area_18",
  "poete_area_19"
];

var fs = FeatureSetByPortalItem(
  Portal("https://www.arcgis.com/"),
  "384158f2ebe34f8ba8b0b39dfe5f5af5",
  0,
  Splice(["Jurisdiction", "core_capability", "target"], areas),
  false
);

// values to sum
var values = [
  'Planning',
  'Organization',
  'Equipment',
  'Training',
  'Exercise',
  'Unknown'
]

// function for building SQL string
function poeteSQL(p) {
  
  // build array of SQL strings per area field
  var area_strings = [];

  for (var a in areas) {
    Push(
      area_strings,
      `CASE WHEN ${areas[a]} = '${p}' THEN 1 ELSE 0 END`
    )
  }

  // give back a single string that sums the areas
  return Concatenate(area_strings, ' + ')
}

// array of statistics
var stats = []

for (var v in values) {
  Push(
    stats,
    {
      name: `${values[v]}_count`,
      expression: poeteSQL(values[v]),
      statistic: 'SUM'
    }
  )
}

// group results into single row and sum
return GroupBy(
  fs,
  {name: 'id', expression: '1'},
  stats
)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 27 Feb 2024 22:26:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1387763#M9157</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2024-02-27T22:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: Return the count of a string in multiple fields using data expression</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1388212#M9163</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;&amp;nbsp;live saver! Thank you, thank you, thank you!!&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2024 18:39:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/return-the-count-of-a-string-in-multiple-fields/m-p/1388212#M9163</guid>
      <dc:creator>dwold</dc:creator>
      <dc:date>2024-02-28T18:39:13Z</dc:date>
    </item>
  </channel>
</rss>

