<?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 Data Expression to show Value changed each Year in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-show-value-changed-each-year/m-p/1297182#M7924</link>
    <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help (I'm thinking either a nested dictionary, or something similar?) would be appreciated!&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;// 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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 08 Jun 2023 15:32:45 GMT</pubDate>
    <dc:creator>RosemaryHatch2013</dc:creator>
    <dc:date>2023-06-08T15:32:45Z</dc:date>
    <item>
      <title>Data Expression to show Value changed each Year</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-show-value-changed-each-year/m-p/1297182#M7924</link>
      <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help (I'm thinking either a nested dictionary, or something similar?) would be appreciated!&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;// 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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2023 15:32:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/data-expression-to-show-value-changed-each-year/m-p/1297182#M7924</guid>
      <dc:creator>RosemaryHatch2013</dc:creator>
      <dc:date>2023-06-08T15:32:45Z</dc:date>
    </item>
  </channel>
</rss>

