Arcade Data Expression - Drawdown Chart

428
1
Jump to solution
06-24-2023 08:58 AM
Labels (1)
AshleyHayes2
New Contributor III

I have a dataset that contains repairs and the expected date that those repairs will be completed.  I would like to use an Arcade data expression to help add a drawdown chart to a dashboard.   The chart will basically show the total number of repairs required and then subtract from that total based on expected completion dates.  In other words there may be 500 repairs and 10 of those repairs have a projected completion date of July 1, 2023.  So, the total would be 490 repairs remaining and the chart would show the decline from 500 to 490 on that date.  This would continue, date by date, until the number of repairs remaining is zero.  

My Arcade data expression utilizes the FeatureSetByPortalItem, Filter, and GroupBy functions to gather the relevant features and groups them by completion dates. Then, it iterates over the grouped dates, calculating the running drawdown value.

 

var fs = FeatureSetByPortalItem(Portal('https://******.maps.arcgis.com'), 'deaef816ff7448dc8fae43bca1a23418', 0, ['Projected_Completed_Converted'], false);

var filteredFeatures = Filter(fs, "Projected_Completed_Converted > Timestamp '2020-01-01'");

var completionDates = GroupBy(filteredFeatures, 'Projected_Completed_Converted', { name: 'total', expression: '1', statistic: 'COUNT' });

var totalRowCount = Count(filteredFeatures);

var drawdownFS = {
  fields: [
    { name: 'date', type: 'esriFieldTypeDate' },
    { name: 'drawdown', type: 'esriFieldTypeInteger' }
  ],
  features: []
};

var runningDrawdown = totalRowCount;
var drawdownFeatures = [];

for (var i in completionDates) {
  var completionDate = completionDates[i].key;
  var countValue = completionDates[i].total;

  runningDrawdown -= countValue;

  var feature = {
    attributes: {
      'date': completionDate,
      'drawdown': runningDrawdown
    }
  };

  drawdownFeatures.push(feature);
}

drawdownFS.features = drawdownFeatures;

return FeatureSet(drawdownFS);

 

 

When I run in the Arcade playground I get the following error: "Test execution error: Execution error - Cannot access value using a key of this type. Verify test data."  I have tried multiple edits to this code, but I have not had success.  I am still a beginner to this type of thing, so any suggestions would be very much appreciated!

 

1 Solution

Accepted Solutions
AshleyHayes2
New Contributor III

Special thanks to @Shakthi_Bharathi_Murugesan for her assistance with this data expression at the Esri User Conference!  

Here is the working version:

var fs = FeatureSetByPortalItem(Portal('https://******.maps.arcgis.com'), 'deaef816ff7448dc8fae43bca1a23418', 0, ['Projected_Completed_Converted'], false);

var filteredFeatures = Filter(fs, "Projected_Completed_Converted > Timestamp '2020-01-01'");

var completionDates = GroupBy(filteredFeatures, 'Projected_Completed_Converted', { name: 'total', expression: '1', statistic: 'COUNT' });


var totalRowCount = Count(filteredFeatures);

var drawdownFS = {
  fields: [
    { name: 'date', type: 'esriFieldTypeDate' },
    { name: 'drawdown', type: 'esriFieldTypeInteger' }
  ],
  features: []
};

var runningDrawdown = totalRowCount;
var drawdownFeatures = [];

for (var i in completionDates) {
  var completionDate = i['Projected_Completed_Converted']
  var countValue = i['total']

  runningDrawdown -= countValue;

  var feature = {
    attributes: {
      'date': completionDate,
      'drawdown': runningDrawdown
    }
  };

  Push(drawdownFeatures,feature);
}

drawdownFS.features = drawdownFeatures;

return FeatureSet(drawdownFS);

View solution in original post

0 Kudos
1 Reply
AshleyHayes2
New Contributor III

Special thanks to @Shakthi_Bharathi_Murugesan for her assistance with this data expression at the Esri User Conference!  

Here is the working version:

var fs = FeatureSetByPortalItem(Portal('https://******.maps.arcgis.com'), 'deaef816ff7448dc8fae43bca1a23418', 0, ['Projected_Completed_Converted'], false);

var filteredFeatures = Filter(fs, "Projected_Completed_Converted > Timestamp '2020-01-01'");

var completionDates = GroupBy(filteredFeatures, 'Projected_Completed_Converted', { name: 'total', expression: '1', statistic: 'COUNT' });


var totalRowCount = Count(filteredFeatures);

var drawdownFS = {
  fields: [
    { name: 'date', type: 'esriFieldTypeDate' },
    { name: 'drawdown', type: 'esriFieldTypeInteger' }
  ],
  features: []
};

var runningDrawdown = totalRowCount;
var drawdownFeatures = [];

for (var i in completionDates) {
  var completionDate = i['Projected_Completed_Converted']
  var countValue = i['total']

  runningDrawdown -= countValue;

  var feature = {
    attributes: {
      'date': completionDate,
      'drawdown': runningDrawdown
    }
  };

  Push(drawdownFeatures,feature);
}

drawdownFS.features = drawdownFeatures;

return FeatureSet(drawdownFS);
0 Kudos