Select to view content in your preferred language

Arcade Loop for Cummulative Sum in Dashboard Chart

100
1
3 weeks ago
DaveVanGelder
New Contributor

I am trying to run a cumulative sum expression on a series chart in my Dashboard using @jcarlson's excellent community post . I'm 90% of the way there, but the output accumulates in the wrong direction (i.e starts at the highest year and works backward, instead of starting at the lowest year and working foward. 

If anyone has any tips to accumulate starting at the lowest year, that'd be hugely helpful!

The output: 

DaveVanGelder_0-1737584452660.png

 

The script:

var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com/'),'f0367a64169a4ba6bf3206c47d65147a',0,['MaturationYear','Hectares'], false);
//Count(fs);
var PYear = Filter (fs, "MaturationYear > '1977-01-01'AND Hectares > 0");
var maturity = GroupBy(PYear,'MaturationYear',{name: 'total', expression: 'Hectares', statistic: 'SUM'});
var fs_dict = {
  fields: [
    {name:'MaturationYear', type:'esriFieldTypeDate'},
    {name:'running_sum', type:'esriFieldTypeDouble'}],
  geometryType: '',
  features:[]
}
var i = 0
for(var s in maturity){
  var filt_date = s['MaturationYear']
  var running_sum_fs =Filter(maturity,"MaturationYear >= @filt_date")
  fs_dict.features[i] ={
    attributes: {
      'MaturationYear': Date(Text(filt_date)),
      'running_sum': Sum(running_sum_fs, 'total')
    }
  }
  console(filt_date)
  console(sum(running_sum_fs,'Hectares'))
  i ++
}
featureset(text(fs_dict))
 
0 Kudos
1 Reply
NicoleJohnson
Regular Contributor

I used the USA Current Wildfires data on Living Atlas to try this in a different way. Bear in my mind I'm no jcarlson, so this could probably be improved. I also just realized my code is lacking comments--feel free to ask questions. 

Output:

NicoleJohnson_1-1737730544842.png

Code:

 

var wildfires = FeatureSetByPortalItem(Portal('https://www.arcgis.com/'),'d957997ccee7408287a963600a77f61f', 1, ['CreateDate', 'GISAcres'], false);

var wildfireAcres = GroupBy(wildfires, 'CreateDate', {name: 'wildfireAcres', expression: 'GISAcres', statistic: 'SUM'});

var sumDict = {
  fields: [
    {name: 'Create_Date', type: 'esriFieldTypeDate'},
    {name: 'GIS_Acres', type: 'esriFieldTypeDouble'},
    {name: 'Sum_Acres', type: 'esriFieldTypeDouble'}
  ],
  geometryType: '',
  features: []
}

var firstWildfire = First(OrderBy(wildfireAcres, 'CreateDate ASC'))

for (var i in wildfireAcres){
  
  var thisDate = i.CreateDate
  var lastWildfires = Filter(wildfireAcres, 'CreateDate <= @thisDate')
  
  var cumulativeAcres

  if (i.CreateDate == firstWildfire['CreateDate']){
    cumulativeAcres = i.wildfireAcres
  } else {
    cumulativeAcres = Sum(lastWildfires, 'wildfireAcres') + i.wildfireAcres
  }

  Push(sumDict['features'], {attributes: {Create_Date: i.CreateDate, GIS_Acres: i.wildfireAcres, Sum_Acres: cumulativeAcres}})
}

return featureset(text(sumDict))

 

 

0 Kudos