I am trying to return the sum of integer fields in a layer, however, am getting a NaN result. I have tried with Count and the correct number appears, so why does Sum not work? What am I missing here?
I am using this in a data expression to use in Dashboards list widget. Any pointers are greatly appreciated!!
var features = FeatureSetByPortalItem(
Portal('https://adcgis.maps.arcgis.com'),
'91feaa24f7d34f9299f5b99cbd7328ed',
1,
["C18_Indst_URAdr_AgriForestFish"],
false
);
Sum(features);
Result Value
Result NaN
Type Number
When you use the Sum function with a single parameter, it expects an array of summable values.
When you're using a FeatureSet, you need to supply a second parameter with the field name you want summed, as there could be any number of fields in a FeatureSet. Even when you only specify a single field, it will likely still have an ObjectID field, so it needs to be told which field you want.
Try Sum(features, 'C18_Indst_URAdr_AgriForestFish')
Hi @jcarlson, thanks for explaining!
The expression now results in the correct number:
var features = FeatureSetByPortalItem(
Portal('https://adcgis.maps.arcgis.com'),
'91feaa24f7d34f9299f5b99cbd7328ed',
1,
["C18_Indst_URAdr_AgriForestFish"],
false
);
return(Sum(features, 'C18_Indst_URAdr_AgriForestFish'))
However in dashboards it says the Arcade expression is unable to be executed? How do I enable it to be used in my widgets?
Oh, I'm sorry! I thought this was an Advanced Formatting thing, I missed the part about it being a Data Expression. A Data Expression needs a FeatureSet as its output.
You could write this up as a Data Expression easily enough, but can I ask how you're using the data in your dashboard? If all you need is the number, you could get that into something like an Indicator widget without needing a separate expression.
In any case, the easiest way to get an output FeatureSet is to use GroupBy, which can handle the aggregation for you, too.
var features = FeatureSetByPortalItem(
Portal('https://adcgis.maps.arcgis.com'),
'91feaa24f7d34f9299f5b99cbd7328ed',
1,
["C18_Indst_URAdr_AgriForestFish"],
false
);
return GroupBy(
features,
{name: 'id', expression: '1'},
{name: 'the_sum', expression: 'C18_Indst_URAdr_AgriForestFish', statistic: 'SUM'}
)