I have a feature layer containing several years of school enrollment data by county. Among the fields are current year enrollment (curr_enr) and previous year enrollment (prev_enr).
Using those fields, I want to calculate percentage change and display that value in a dashboard serial chart. That is, ($feature.curr_enr-$.prev_enr)/$feature.prev_enr
Based on other posts, it seems that my plan -- a dashboard serial chart showing percentage change by year -- requires a data expression.
I've created a variable for my source feature layer. But I'm stuck on how to calculate percentage change from the two enrollment fields and returning that value to show on the serial chart. (Once created, the serial chart would be controlled by a county selector.) Thank you for any pointers.
Something like this should get you started.
var fs = FeatureSetByPortalItem(
Portal("https://www.arcgis.com"),
"<itemID>",
<layerIndex>,
["County", "Year", "curr_enr", "prev_enr"],
false
);
var features = [];
for (var f in fs) {
var county = f["County"];
var year = f["Year"];
var curr = f["curr_enr"];
var prev = f["prev_enr"];
if (IsEmpty(curr) || IsEmpty(prev) || prev == 0) {
continue;
}
var pctChange = ((curr - prev) / prev) * 100;
Push(features, {
attributes: {
County: county,
Year: year,
PercentChange: pctChange
}
});
}
return FeatureSet(Text({
fields: [
{ name: "County", type: "esriFieldTypeString" },
{ name: "Year", type: "esriFieldTypeInteger" },
{ name: "PercentChange", type: "esriFieldTypeDouble" }
],
geometryType: "",
features: features
}));
You can use the GroupBy function to calculate this
var fs = FeatureSetByPortalItem(
Portal("https://www.arcgis.com"),
"yourID",
0,
['County','Year', 'curr_enr','prev_enr'],
false
)
GroupBy(fs, ['County','Year','curr_enr','prev_enr'], {name: 'Change', expression: '(curr_enr - prev_enr)/prev_enr', statistic: 'Max'})