Select to view content in your preferred language

Data expression that takes two numeric fields and calculates percentage change

144
5
a week ago
Labels (1)
JimMiller3
Occasional Contributor

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.

0 Kudos
5 Replies
Neal_t_k
Frequent Contributor

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
}));
0 Kudos
KenBuja
MVP Esteemed Contributor

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'})
Neal_t_k
Frequent Contributor

@KenBuja I need to learn groupby better, much shorter, and all at once.

0 Kudos
JimMiller3
Occasional Contributor

Thank you @Neal_t_k and @KenBuja for the above responses. Both suggestions work perfectly if I select any of California’s 58 counties.

Problems occur when the county selector is set to “None.” In that case, three serial charts update: 1) cumulative enrollment by year for all of California's counties; 2) cumulative enrollment change by year for all of California's counties and 3) cumulative enrollment percentage change by year for all of California's counties.

The percentage change values are incorrect.

For example, the image below shows 2023-24 to 2024-25 percentage change on the serial chart and in an indicator using the percentage change calculated value. The percentage change value in the indicator card - -0.54% -- is correct. The percentage change value in the serial chart – 2.97% -- is incorrect.

JimMiller3_0-1763703214566.png

Below is how things appear in the serial chart setup. I suspect the problem stems from the Statistic selection. It currently shows “Maximum” but any option I choose – Average, Minimum, etc. -- yields incorrect values. Is there a way to show percentage change value as is, without applying a statistic type to it? Or is there another approach to take. Thanks.

JimMiller3_1-1763703326555.png

0 Kudos
Neal_t_k
Frequent Contributor

@JimMiller3   Did you try "Sum" percent change? How do you have it calculated in the indicator? 

0 Kudos