Select to view content in your preferred language

Dashboard Indicator that sums different layers

791
2
09-27-2023 12:13 PM
aprhyde11
New Contributor III

Is it possible to create an Indicator that combines an attribute from different layers? I have a cost attribute for points, lines, and polygons layers and I want to combine them for an Overall Cost indicator. 

I haven't been able to find a direct answer from my searching

Please let me know if there are further questions!

0 Kudos
2 Replies
marksm_macomb
Occasional Contributor

You will need to write a data expression to combine features from multiple layers into one FeatureSet.

Some other resources:

Solved: Dashboard Data Expression to Combine Multiple Feat... - Esri Community

arcade-expressions/dashboard_data/CombineMultipleLayers(SerialChart).md at master · Esri/arcade-expr...

At the most basic, your data expression may look like this:

var portal = Portal('Portal URL')

//Create a feature set for each point, line, and polygon layer and include the cost feild
var point_fs = FeatureSetByPortalItem(portal, 'ItemID', layerID, ['COST'], false)
var polygon_fs = FeatureSetByPortalItem(portal, 'ItemID', layerID, ['COST'], false)
var line_fs = FeatureSetByPortalItem(portal, 'ItemID', LayerID, ['COST'], false)

//Create an empty array and variable for the new FeatureSet that you will push/combine your features into
var features = [];
var feat;

//Push each feature from each layer into the empty array
for (var f in point_fs) {
    feat = {
        'attributes': {
            'COST': f['COST']
        }
    };
    Push(features, feat)
}

for (var f in polygon_fs) {
    feat = {
        'attributes': {
            'COST': f['COST']
        }
    };
    Push(features, feat)
}

for (var f in line_fs) {
    feat = {
        'attributes': {
            'COST': f['COST']
        }
    };
    Push(features, feat)
}

//Create final FeatureSet from combined features
var combinedDict = {
    'fields': [
        { 'name': 'COST', 'type': 'esriFieldTypeInteger' },
    ],
    'geometryType': '',
    'features': features,
};

return FeatureSet(Text(combinedDict));

 

But you can do more with grouping, summing, etc. if you have different categories that you want to sum by. 

jcarlson
MVP Esteemed Contributor

@marksm_macomb  has the right answer, but I'd like to mention that the basic "loop through features" approach can get very long if your layers have a lot of features. Using aggregate functions like Sum will evaluate a lot faster, and if all you need is the total cost summed, you can do it this way:

var portal = Portal('Portal URL')

var fsets = [
  FeatureSetByPortalItem(portal, 'ItemID', layerID, ['COST'], false),
  FeatureSetByPortalItem(portal, 'ItemID', layerID, ['COST'], false),
  FeatureSetByPortalItem(portal, 'ItemID', layerID, ['COST'], false)
]

var running_sum = 0

for (var set in fsets) {
  running_sum += Sum(set, 'COST')
}

return FeatureSet({
  fields: {name: 'total_cost', type: 'esriFieldTypeInteger'}, // or whatever field type it is
  geometryType: '',
  features: [{attributes: {total_cost: running_sum}}]
})

Your output will be a single feature with a single field: the total.

If you need the output to be dynamic, responding to filters, etc, then you will need to do it the long way, looping through everything. And don't forget that if you do want to filter these in some way, all of those fields you're filtering by will need to be in the output as well.

- Josh Carlson
Kendall County GIS