<?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 to return count of values across 5 different columns using GroupBy function in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292267#M52391</link>
    <description>&lt;P&gt;It is possible to do it all in a single GroupBy if you write the right expression. Look at the following SQL expression:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;CASE WHEN obs1 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs2 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs3 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs4 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs5 = 'Tree Obstruction' THEN 1 ELSE 0 END&lt;/LI-CODE&gt;&lt;P&gt;If your point had 4 of the 5 fields marked as "Tree Obstruction", this will return a 4. And if you use this in GroupBy with the statistic &lt;STRONG&gt;SUM&lt;/STRONG&gt;, you'll end up with the total of all Tree Obstructions across 5 fields for the whole layer.&lt;/P&gt;&lt;P&gt;The trouble is that you'd need the same 5-line expression for every item in your domain, even though only the domain value is changing. Not that that's a bad thing, but it makes the expression lengthy and annoying.&lt;/P&gt;&lt;P&gt;Now consider a custom function:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function GroupingExpression(domain_value) {
    return `
    CASE WHEN obs1 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs2 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs3 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs4 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs5 = '${domain_value}' THEN 1 ELSE 0 END
    `
}&lt;/LI-CODE&gt;&lt;P&gt;Now we can just call GroupingExpression('Tree Obstruction') to get the same thing as before. And if we use it to build our GroupBy function, it could look like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;GroupBy(
    fs,
    {name: 'the_row', expression: 1},
    [
        {name: 'tree_obstruction_count', expression: GroupingExpression('Tree Obstruction'), statistic: 'SUM'},
        {name: 'always_off_count', expression: GroupingExpression('Always Off'), statistic: 'SUM'},
        // and so on
    ]
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;The result of this should be a single-row FeatureSet with the totals in their own columns, which you can then use to generate a chart.&lt;/P&gt;</description>
    <pubDate>Tue, 23 May 2023 19:29:27 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2023-05-23T19:29:27Z</dc:date>
    <item>
      <title>Arcade data expression to return count of values across 5 different columns using GroupBy function</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292213#M52386</link>
      <description>&lt;P&gt;Hi, I'm working on a data expression for a dashboard bar chart. The dashboard is showing inspections for streetlights in a survey area.&lt;/P&gt;&lt;P&gt;On the point feature being referenced by the dashboard are 5 'observation' columns - 'obs1', 'obs2', etc.&lt;/P&gt;&lt;P&gt;The columns all use the same domain list. The possible values relate to the light status such as 'Always Off' or 'Tree Obstruction'. There are 10 possible values in the domain list. Some points may only have 1, or none of these columns populated. Others may use all 5 of the columns, just depends on the inspection results and how many issues need to be addressed.&lt;/P&gt;&lt;P&gt;What I'm trying to return to the chart is the count of all the unique domain list values, grouped not by the 5 columns, but by the observation values themselves. So that I end up with a bar chart showing, say, 10 counts (or points) of 'Always Off', 15 for 'Tree Obstruction', etc. I could just have 5 separate charts for each observation column but that would obviously not be a very good solution.&lt;/P&gt;&lt;P&gt;I'm not sure how to pass this list to the groupby function, or if I need to approach this in some other way. Some of the examples&amp;nbsp;&lt;A href="https://www.esri.com/arcgis-blog/products/ops-dashboard/announcements/introducing-data-expressions-in-arcgis-dashboards/" target="_self"&gt;in this blog post&lt;/A&gt;&amp;nbsp;are close but not exactly what I need unless I'm missing something.&lt;/P&gt;&lt;P&gt;Thank You&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2023 18:09:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292213#M52386</guid>
      <dc:creator>AaronManuel2</dc:creator>
      <dc:date>2023-05-23T18:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade data expression to return count of values across 5 different columns using GroupBy function</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292256#M52389</link>
      <description>&lt;LI-CODE lang="javascript"&gt;var value_counts = {
    "Always Off": 0,
    "Tree Obstruction": 0,
}
var fields = ["Status1", "Status2", "Status3"]


var p = Portal(...)
var fs = FeaturesetByPortalItem(p, id, layer, fields, false)

for(var f in fs) {
    for(var i in fields) {
        var value = f[fields[i]]
        if(IsEmpty(value)) { continue }
        if(!HasKey(value_count, value)) { value_counts[value] = 0 }
        value_counts[value] = value_counts[value] + 1
    }
}

var out_fs = {
    geometryType: "",
    fields: [
        {name: "Status", type: "esriFieldTypeString"},
        {name: "Count", type: "esriFieldTypeInteger"},
    ],
    features: []
}
for(var value in value_counts) {
    var c = value_counts[value]
    var f = {attributes: {Status: value, Count: c}}
    Push(out_fs.features, f)
}
return Featureset(Text(out_fs))&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 23 May 2023 19:10:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292256#M52389</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-05-23T19:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade data expression to return count of values across 5 different columns using GroupBy function</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292267#M52391</link>
      <description>&lt;P&gt;It is possible to do it all in a single GroupBy if you write the right expression. Look at the following SQL expression:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;CASE WHEN obs1 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs2 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs3 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs4 = 'Tree Obstruction' THEN 1 ELSE 0 END +
CASE WHEN obs5 = 'Tree Obstruction' THEN 1 ELSE 0 END&lt;/LI-CODE&gt;&lt;P&gt;If your point had 4 of the 5 fields marked as "Tree Obstruction", this will return a 4. And if you use this in GroupBy with the statistic &lt;STRONG&gt;SUM&lt;/STRONG&gt;, you'll end up with the total of all Tree Obstructions across 5 fields for the whole layer.&lt;/P&gt;&lt;P&gt;The trouble is that you'd need the same 5-line expression for every item in your domain, even though only the domain value is changing. Not that that's a bad thing, but it makes the expression lengthy and annoying.&lt;/P&gt;&lt;P&gt;Now consider a custom function:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function GroupingExpression(domain_value) {
    return `
    CASE WHEN obs1 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs2 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs3 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs4 = '${domain_value}' THEN 1 ELSE 0 END +
    CASE WHEN obs5 = '${domain_value}' THEN 1 ELSE 0 END
    `
}&lt;/LI-CODE&gt;&lt;P&gt;Now we can just call GroupingExpression('Tree Obstruction') to get the same thing as before. And if we use it to build our GroupBy function, it could look like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;GroupBy(
    fs,
    {name: 'the_row', expression: 1},
    [
        {name: 'tree_obstruction_count', expression: GroupingExpression('Tree Obstruction'), statistic: 'SUM'},
        {name: 'always_off_count', expression: GroupingExpression('Always Off'), statistic: 'SUM'},
        // and so on
    ]
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;The result of this should be a single-row FeatureSet with the totals in their own columns, which you can then use to generate a chart.&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2023 19:29:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292267#M52391</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2023-05-23T19:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade data expression to return count of values across 5 different columns using GroupBy function</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292269#M52392</link>
      <description>&lt;P&gt;Thanks Johannes, this worked perfectly!&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2023 19:35:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292269#M52392</guid>
      <dc:creator>AaronManuel2</dc:creator>
      <dc:date>2023-05-23T19:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade data expression to return count of values across 5 different columns using GroupBy function</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292320#M52401</link>
      <description>&lt;P&gt;Thank you! This is what I was originally looking to do.&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2023 21:27:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-data-expression-to-return-count-of-values/m-p/1292320#M52401</guid>
      <dc:creator>AaronManuel2</dc:creator>
      <dc:date>2023-05-23T21:27:45Z</dc:date>
    </item>
  </channel>
</rss>

