<?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: Calculate the percentage of total based on GroupBy() results in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1127993#M5523</link>
    <description>&lt;P&gt;You should keep in mind that if you have a FeatureSet, the chart itself can handle the GroupBy function.&lt;/P&gt;&lt;P&gt;I can have a Data Expression that returns a FeatureSet:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_0-1640094862261.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30136iD99D288C7CF41EF0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_0-1640094862261.png" alt="jcarlson_0-1640094862261.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And then let the chart handle the grouping for me by setting it to&lt;STRONG&gt; Grouped&lt;/STRONG&gt; &lt;STRONG&gt;Values&lt;/STRONG&gt; and choose various statistics.&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_2-1640094902965.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30139i83AFE291DF8A93F1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_2-1640094902965.png" alt="jcarlson_2-1640094902965.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;But to more directly address your issue, you can add another field in your &lt;STRONG&gt;GroupBy&lt;/STRONG&gt; to get the percentage, by earlier calculating the total number of responses. Try this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fv = FeatureSetByPortalItem(
    Portal(p),
    itemID,
    layerID,
    ['future_support'], 
    false
);
    
var fv_f = Filter(fv,'future_support IS NOT NULL');

// Get the total number of records in the filtered fs
var fv_tot = Count(fv_v);

var features = [];
var feat;

for (var feature in fv_f){
    var split_fv_f = Split(feature['future_support'],',') 
    var count_arr = Count(split_fv_f) 
    for(var i = 0; i &amp;lt; count_arr; i++ ){ 
        feat = { 
            'attributes': { 
                'split_choices': decode(Number(Trim(split_fv_f[i])),
            1,'Pruning &amp;amp; Rejuvenation',
            2,'Composting',
            3,'Integrated Pest &amp;amp; Disease Management',
            4,'Weeding ',
            5,'Nutrition',
            6,'Shade',
            7,'Mulching',
            8,'Erosion control',
            9,'Coffee planting',
            10,'Harvesting',
            11,'Sustainability',
            12,'Business Skills',
            99,'None',
            'other') 
            }}

        Push(features, feat);
}}

var dict = {
  fields: [
    { name: 'split_choices', type: 'esriFieldTypeString' }
  ],
  geometryType: '',
  features: features
};

var fs_dict = FeatureSet(Text(dict));

// Use total from above to get the percentage values
return GroupBy(fs_dict, ['split_choices'], 
        [{
            name: 'split_count',
            expression: 'split_choices',
            statistic: 'COUNT'
         },
         {
            name: 'split_percentage',
            expression: '1 / @fv_tot',
            statistic: 'SUM'
          ]); &lt;/LI-CODE&gt;&lt;P&gt;By using the expression &lt;STRONG&gt;1 / @ fv_t&lt;/STRONG&gt;, your GroupBy function will calculate the per-response percentage, then sum them. For example, each single response out of 2208 responses is worth about 0.000453. If 370 of my responses fall into a given category, then the total percentage of responses that chose that category would be 16.76%.&lt;/P&gt;</description>
    <pubDate>Tue, 21 Dec 2021 14:10:02 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2021-12-21T14:10:02Z</dc:date>
    <item>
      <title>Arcade data expression: Calculate the percentage of total based on GroupBy() results</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1127965#M5521</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;today I got stuck writing a data expression that calculates the percentage of several categories based on the frequency of their mentioning in a survey.&lt;/P&gt;&lt;P&gt;To illustrate, I have a survey that allows the user to give multiple topics he would like to know more about in future trainings. These are split by the data expression into seperate rows in order to count them.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fv = FeatureSetByPortalItem(Portal(p),itemID,layerID,
    ['future_support'], 
    false );
    
var fv_f = Filter(fv,'future_support IS NOT NULL')

var dict = {
  fields: [
    { name: 'split_choices',        type: 'esriFieldTypeString' },
  ],
  geometryType: '',
  features: [],
};

var index = 0; 

for (var feature in fv_f){
    var split_fv_f = Split(feature['future_support'],',') 
    var count_arr = Count(split_fv_f) 
    for(var i = 0; i &amp;lt; count_arr; i++ ){ 
        dict.features[index++] = { 
            'attributes': { 
                'split_choices': decode(Number(Trim(split_fv_f[i])),
            1,'Pruning &amp;amp; Rejuvenation',
            2,'Composting',
            3,'Integrated Pest &amp;amp; Disease Management',
            4,'Weeding ',
            5,'Nutrition',
            6,'Shade',
            7,'Mulching',
            8,'Erosion control',
            9,'Coffee planting',
            10,'Harvesting',
            11,'Sustainability',
            12,'Business Skills',
            99,'None',
            'other') 
            }}    
}}

var fs_dict = FeatureSet(Text(dict));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is all fine, in the next step I would like to group and count per given answer. Easy task, just use the GroupBy() function:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;return GroupBy(fs_dict, ['split_choices'], 
       [{ name: 'split_count', 
       expression: 'split_choices', 
       statistic: 'COUNT' }]); &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which gives me the following:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Merlin_1-1640084073043.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30133i1D82B43C330DF246/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Merlin_1-1640084073043.png" alt="Merlin_1-1640084073043.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Based on this, I can create a nice graph showing these counts, but I cannot tell the percentage of total per question. The total is easly calcuated as variable by counting the filtered result of fv_f. So I would basically like to perform per each row in the above picture an additional calculation to get the percentage.&lt;/P&gt;&lt;P&gt;I tried to use the GroupBy() FS I get in another for-loop but that didn't seem to work, as I am not able to reference the fields correctly, namely split_count.&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;All the best,&lt;BR /&gt;merlin&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 11:00:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1127965#M5521</guid>
      <dc:creator>Merlin</dc:creator>
      <dc:date>2021-12-21T11:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade data expression: Calculate the percentage of total based on GroupBy() results</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1127993#M5523</link>
      <description>&lt;P&gt;You should keep in mind that if you have a FeatureSet, the chart itself can handle the GroupBy function.&lt;/P&gt;&lt;P&gt;I can have a Data Expression that returns a FeatureSet:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_0-1640094862261.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30136iD99D288C7CF41EF0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_0-1640094862261.png" alt="jcarlson_0-1640094862261.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And then let the chart handle the grouping for me by setting it to&lt;STRONG&gt; Grouped&lt;/STRONG&gt; &lt;STRONG&gt;Values&lt;/STRONG&gt; and choose various statistics.&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_2-1640094902965.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30139i83AFE291DF8A93F1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_2-1640094902965.png" alt="jcarlson_2-1640094902965.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;But to more directly address your issue, you can add another field in your &lt;STRONG&gt;GroupBy&lt;/STRONG&gt; to get the percentage, by earlier calculating the total number of responses. Try this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fv = FeatureSetByPortalItem(
    Portal(p),
    itemID,
    layerID,
    ['future_support'], 
    false
);
    
var fv_f = Filter(fv,'future_support IS NOT NULL');

// Get the total number of records in the filtered fs
var fv_tot = Count(fv_v);

var features = [];
var feat;

for (var feature in fv_f){
    var split_fv_f = Split(feature['future_support'],',') 
    var count_arr = Count(split_fv_f) 
    for(var i = 0; i &amp;lt; count_arr; i++ ){ 
        feat = { 
            'attributes': { 
                'split_choices': decode(Number(Trim(split_fv_f[i])),
            1,'Pruning &amp;amp; Rejuvenation',
            2,'Composting',
            3,'Integrated Pest &amp;amp; Disease Management',
            4,'Weeding ',
            5,'Nutrition',
            6,'Shade',
            7,'Mulching',
            8,'Erosion control',
            9,'Coffee planting',
            10,'Harvesting',
            11,'Sustainability',
            12,'Business Skills',
            99,'None',
            'other') 
            }}

        Push(features, feat);
}}

var dict = {
  fields: [
    { name: 'split_choices', type: 'esriFieldTypeString' }
  ],
  geometryType: '',
  features: features
};

var fs_dict = FeatureSet(Text(dict));

// Use total from above to get the percentage values
return GroupBy(fs_dict, ['split_choices'], 
        [{
            name: 'split_count',
            expression: 'split_choices',
            statistic: 'COUNT'
         },
         {
            name: 'split_percentage',
            expression: '1 / @fv_tot',
            statistic: 'SUM'
          ]); &lt;/LI-CODE&gt;&lt;P&gt;By using the expression &lt;STRONG&gt;1 / @ fv_t&lt;/STRONG&gt;, your GroupBy function will calculate the per-response percentage, then sum them. For example, each single response out of 2208 responses is worth about 0.000453. If 370 of my responses fall into a given category, then the total percentage of responses that chose that category would be 16.76%.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 14:10:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1127993#M5523</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-12-21T14:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade data expression: Calculate the percentage of total based on GroupBy() results</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1128886#M5565</link>
      <description>&lt;P&gt;Excellent, thanky you very much, works like a charm!&lt;BR /&gt;&lt;BR /&gt;I have one minor change/improvement:&lt;BR /&gt;I just used &lt;STRONG&gt;100 / @ fv_t&lt;/STRONG&gt;, to get the percentage I want to display right away.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Merlin_0-1640596475903.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30361i79A3480895B55025/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Merlin_0-1640596475903.png" alt="Merlin_0-1640596475903.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Merlin_1-1640596507530.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/30362i36DBF58B3D01E687/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Merlin_1-1640596507530.png" alt="Merlin_1-1640596507530.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 09:14:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1128886#M5565</guid>
      <dc:creator>Merlin</dc:creator>
      <dc:date>2021-12-27T09:14:41Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade data expression: Calculate the percentage of total based on GroupBy() results</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1436000#M9578</link>
      <description>&lt;P&gt;I am trying to achieve something same result on my end but can't seem to get it right. I am trying to calculate percentage for categories in a field on a single choice question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am definately missing something. Please help. see what I did below&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;var&lt;/SPAN&gt; &lt;SPAN&gt;portals&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;Portal&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'&lt;A href="https://clevernestgis.maps.arcgis.com/" target="_blank" rel="noopener"&gt;00&lt;/A&gt;'&lt;/SPAN&gt;&lt;SPAN&gt;);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;var&lt;/SPAN&gt; &lt;SPAN&gt;fs&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;FeatureSetByPortalItem&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;portals&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;'99'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/S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;&lt;SPAN&gt;,[&lt;/SPAN&gt;&lt;SPAN&gt;'Formal_level'&lt;/SPAN&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; [{ &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;name&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;SPAN&gt;'name'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;expression&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;SPAN&gt;'100 / @countf_fs'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;statistic&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;SPAN&gt;'SUM'&lt;/SPAN&gt;&lt;SPAN&gt;}]);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 May 2024 01:24:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/arcade-data-expression-calculate-the-percentage-of/m-p/1436000#M9578</guid>
      <dc:creator>EdmundEkanem1</dc:creator>
      <dc:date>2024-05-11T01:24:14Z</dc:date>
    </item>
  </channel>
</rss>

