Data Expression to show Value changed each Year

221
0
06-08-2023 08:32 AM
RosemaryHatch2013
New Contributor II

Hello! 

I'm trying to show a line graph in a dashboard of the CHANGE in a value by year. I was able to get an arcade expression to create a featureset that shows the change by year, however I need to add the ability to filter that feature set by the 'UNIT_NAME'. So ideally, there would be a row for each year and unit combination, with the value being the change from the previous year for that unit. 

Any help (I'm thinking either a nested dictionary, or something similar?) would be appreciated!

 

 

// Write an expression that returns a FeatureSet.
// Documentation: https://arcg.is/3c419TD
// Samples: https://arcg.is/38SEWWz

var portal = Portal('https://arcgis.com');

//Feature Set from hosted
var fs = FeatureSetByPortalItem(
  portal,
  'examplelyr',
  0,
  [
    'Year_Collected',
    'UNIT_NAME',
    'LAST_INFESTED_AREA'
  ],
  false
);

//Group by Year Collected (and Ideally Unit Name)
var sumfs = GroupBy(
  fs,
  ['Year_Collected', 'UNIT_NAME'],
  [{
    name:'TotalInfestedArea',
    expression:'LAST_INFESTED_AREA',
    statistic:'SUM'
  }]
);
//Create a new, ordered layer
var sumfsOrder = Orderby(sumfs, 'Year_Collected')

//Create an empty dictionary to hold values output by above groupby function
var dictYearCount = {}

//Create a dictionary to hold the change in values (AcresChanged)
var dictYearChange = {
  'fields': [
    {name: "Year", type: "esriFieldTypeString" },
    {name: "Unit", type: "esriFieldTypeString" },
    {name: "AcresChanged", type: "esriFieldTypeDouble" }
  ],
  geometryType: "",
  features: [],
}

//Add Key/Value pairs to dictYearCount Dictionary
//Key is Year Collected, Value is total infested area
for (var f in sumfsOrder){
  var yearKey = Text(f.Year_Collected)
  var yearValue = f['TotalInfestedArea']
  //Somewhere in here add ability for a second key, or dictionary, or something to keep Unit as a variable
  dictYearCount[yearKey] = yearValue
}

//For each row in the grouping, create a variable for the current year 
// and the previous year value
for (var t in sumfsOrder) {
  if (HasKey(dictYearCount, Text(t.Year_Collected-1)) == true) {
    var previousYearCount = dictYearCount[Text(t.Year_Collected-1)]
    var currentYearCount = dictYearCount[Text(t.Year_Collected)]
    //Subtract the two values
    var Diff = currentYearCount - previousYearCount
    //Add the new differenct to the Change Dictionary
    Push(
      dictYearChange['features'],
      {
        attributes: {
          Year: Text(t.Year_Collected),
          AcresChanged: Diff
          }
      }
    )
  }
  //If no previous value (ie first year) leave value field empty
  else {
    Push(
      dictYearChange['features'],
      {
        attributes: {
          Year: Text(t.Year_Collected),
          AcresChanged: ""
          }
      }
    )
  }
}

Console(dictYearChange)


var YearChange = FeatureSet(Text(dictYearChange))
return YearChange

 

 

0 Kudos
0 Replies