<?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: Data Expression for Pie Chart in Dashboard Fails in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096050#M5035</link>
    <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous User&amp;nbsp;,&lt;/P&gt;&lt;P&gt;One of the issues in your code is that you using "fs" as if it contains the installation date. In this case "fs" is a featureset and you will loop through the features, read out the installation date and and fill the output "Dict" with the values calculated. Have a look at the code below:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var p = 'https://www.arcgis.com'; 
var itemId = 'xxx'; 
var layerId = 0; 
var fs = Filter(FeatureSetByPortalItem(Portal(p), itemID, layerID,['Installation']),'Installation &amp;gt; 0'); 
var ageyrs = Year(Now())-fs;
var agechoice = iif(ageyrs&amp;gt;20,'&amp;gt;20 Years Old',(iif(ageyrs&amp;lt;10,'&amp;lt;10 Years Old','11-20 Years Old')));
var Dict = {
    'fields':[ 
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'}, 
        {'name': 'agecat', 'type':'esriFieldTypeString'}], 
    'geometryType':'', 
    'features': []};

var index = 0;
for (var f in fs) {
    var instalationdate = f['Installation'];
    var ageyrs = DateDiff(Now(), instalationdate, 'years');
    var agechoice = iif(ageyrs&amp;gt;20,'&amp;gt;20 Years Old',
                       (iif(ageyrs&amp;lt;10,'&amp;lt;10 Years Old',
                       '11-20 Years Old')));

    Dict.features[index] = {   
            'attributes': {   
                'sign_age': ageyrs,
                'agecat': agechoice
            }}   
    index++; 
};  

return FeatureSet(Text(Dict));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you just want to return a count per age class you could also create an intermediate dictionary for each class value and update the count.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Sep 2021 17:56:59 GMT</pubDate>
    <dc:creator>XanderBakker</dc:creator>
    <dc:date>2021-09-07T17:56:59Z</dc:date>
    <item>
      <title>Data Expression for Pie Chart in Dashboard Fails</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096039#M5033</link>
      <description>&lt;P&gt;I'm creating&amp;nbsp; a dashboard for road signs.&amp;nbsp; Each sign has the installation year (4 digit integer).&amp;nbsp; (The field name is "Installation"). I want to show a pie chart that shows if signs are &amp;lt;10 years old, 11-20 years old, or &amp;gt;20 years old.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my first time trying to do this in a data expression, and I can't get the correct results.&amp;nbsp; It says all the signs are 11-20 years old.&amp;nbsp; I also realized the "ageyrs" variable says it's Nan.&amp;nbsp; So something somewhere isn't calculating right.&amp;nbsp; Can anyone spot what I need to fix in my code?&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;var p = 'https://www.arcgis.com';
var itemId = 'xxx';
var layerId = 0;

var fs = Filter(FeatureSetByPortalItem(Portal(p),itemID,layerID,['Installation']),'Installation &amp;gt; 0');
var ageyrs = Year(Now())-fs
var agechoice = iif(ageyrs&amp;gt;20,'&amp;gt;20 Years Old',(iif(ageyrs&amp;lt;10,'&amp;lt;10 Years Old','11-20 Years Old')))

var Dict = {
    'fields':[
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'},
        {'name': 'agecat', 'type':'esriFieldTypeString'}],
        'geometryType':'',
        'features':
        [{'attributes':
            {'sign_age':ageyrs,
             'agecat':agechoice
        }}]};

return FeatureSet(Text(Dict))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1108"&gt;@XanderBakker&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 17:32:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096039#M5033</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-09-07T17:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Data Expression for Pie Chart in Dashboard Fails</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096050#M5035</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous User&amp;nbsp;,&lt;/P&gt;&lt;P&gt;One of the issues in your code is that you using "fs" as if it contains the installation date. In this case "fs" is a featureset and you will loop through the features, read out the installation date and and fill the output "Dict" with the values calculated. Have a look at the code below:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var p = 'https://www.arcgis.com'; 
var itemId = 'xxx'; 
var layerId = 0; 
var fs = Filter(FeatureSetByPortalItem(Portal(p), itemID, layerID,['Installation']),'Installation &amp;gt; 0'); 
var ageyrs = Year(Now())-fs;
var agechoice = iif(ageyrs&amp;gt;20,'&amp;gt;20 Years Old',(iif(ageyrs&amp;lt;10,'&amp;lt;10 Years Old','11-20 Years Old')));
var Dict = {
    'fields':[ 
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'}, 
        {'name': 'agecat', 'type':'esriFieldTypeString'}], 
    'geometryType':'', 
    'features': []};

var index = 0;
for (var f in fs) {
    var instalationdate = f['Installation'];
    var ageyrs = DateDiff(Now(), instalationdate, 'years');
    var agechoice = iif(ageyrs&amp;gt;20,'&amp;gt;20 Years Old',
                       (iif(ageyrs&amp;lt;10,'&amp;lt;10 Years Old',
                       '11-20 Years Old')));

    Dict.features[index] = {   
            'attributes': {   
                'sign_age': ageyrs,
                'agecat': agechoice
            }}   
    index++; 
};  

return FeatureSet(Text(Dict));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you just want to return a count per age class you could also create an intermediate dictionary for each class value and update the count.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 17:56:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096050#M5035</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-09-07T17:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Data Expression for Pie Chart in Dashboard Fails</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096289#M5041</link>
      <description>&lt;P&gt;That definitely helps and makes more sense.&amp;nbsp; However it still isn't calculating the Sign Age correctly.&amp;nbsp; Therefore all the signs show up as 11-20yrs old which isn't correct.&amp;nbsp; Does it matter that the the Installation field is an integer field?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 10:51:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096289#M5041</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-09-08T10:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Data Expression for Pie Chart in Dashboard Fails</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096290#M5042</link>
      <description>&lt;P&gt;I made&amp;nbsp; a few minor tweaks and got it to work! Thank you so much for you help! I was searching for a quite some time yesterday trying to figure this out.&amp;nbsp; Below is the updated code.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var p = 'https://www.arcgis.com'; 
var itemId = 'xxx'; 
var layerId = 0; 
var fs = Filter(FeatureSetByPortalItem(Portal(p), itemID, layerID,['Installation']),'Installation &amp;gt; 0'); 

var Dict = {
    'fields':[ 
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'}, 
        {'name': 'agecat', 'type':'esriFieldTypeString'}], 
    'geometryType':'', 
    'features': []};

var index = 0;
for (var f in fs) {
    var insdate = f['Installation'];
    var ageyrs = Year(Now())-insdate;
    var agechoice = iif(ageyrs&amp;gt;20,'&amp;gt;20 Years Old',
                       (iif(ageyrs&amp;lt;10,'&amp;lt;10 Years Old',
                       '11-20 Years Old')));

    Dict.features[index] = {   
            'attributes': {   
                'sign_age': ageyrs,
                'agecat': agechoice
            }}   
    index++; 
};  

return FeatureSet(Text(Dict));&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 08 Sep 2021 10:56:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096290#M5042</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-09-08T10:56:07Z</dc:date>
    </item>
    <item>
      <title>Re: Data Expression for Pie Chart in Dashboard Fails</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096343#M5045</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous User&amp;nbsp;,&lt;/P&gt;&lt;P&gt;I'm glad it works and as you noticed there is a big difference between an integer and a date. I changed the calculation using DateDiff, but this requires two dates to compare. In your case using an integer your calculation does the job.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 13:17:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096343#M5045</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-09-08T13:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: Data Expression for Pie Chart in Dashboard Fails</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096345#M5047</link>
      <description>&lt;P&gt;Thank you so much for your help!!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 13:19:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-for-pie-chart-in-dashboard-fails/m-p/1096345#M5047</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-09-08T13:19:09Z</dc:date>
    </item>
  </channel>
</rss>

