Select to view content in your preferred language

ArcGIS Dashboard-Arcade Expressions (Math)

1315
11
Jump to solution
05-09-2023 08:30 AM
MarlaJohnson
New Contributor III

Can you use data expressions in ArcGIS Dashboards to perform math with a pervious record?  The data is stored in ArcGIS Server and pulled into ArcGIS Online .

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Can you give some examples? It's hard to know how the expression is failing without knowing what it's actually doing.

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
11 Replies
jcarlson
MVP Esteemed Contributor

Yes! Provided you can identify what "previous" means with some record sorting, etc., you can easily write an expression that uses the previous record's value to calculate something else.

- Josh Carlson
Kendall County GIS
0 Kudos
MarlaJohnson
New Contributor III
I am trying to subtract the current day number (chemical reading) from the previous day. It is usually the record right before if sorted by date The issue is Arcade expression is not calling the data, which makes me think I am doing it wrong.
0 Kudos
jcarlson
MVP Esteemed Contributor

Can you share the expression?

- Josh Carlson
Kendall County GIS
0 Kudos
MarlaJohnson
New Contributor III
I have tried a lot of different things. I do have an empty field I can place the results in. This is for a serial chart.

var portal = Portal('https://www.arcgis.com');
var fs = FeatureSetByPortalItem(portal, 'fcb8c011c9f44ad09e735683da808b41');

var currentIndex = feature['OBJECTID'];
var previousIndex = currentIndex - 1;

if (previousIndex > 0) {
var previousRecord = FeatureByIndex(fs, previousIndex);

var previousValue = previousRecord['INSPAnswerNumber'];
var currentValue = feature['INSPAnswerNumber'];

if (!IsEmpty(currentValue) && !IsEmpty(previousValue)) {
return currentValue - previousValue;
}
}

return null;

0 Kudos
jcarlson
MVP Esteemed Contributor

I'm not sure how you ended up with that expression. The FeatureSetByPortalItem function requires you to specify the layer index, and it won't get past the second line as its written. Even if it did, it needs to return a FeatureSet object.

The way Data Expressions work is a bit different from other Arcade profiles. If you haven't yet, take a look at some of the expressions here:

https://github.com/Esri/arcade-expressions/tree/master/dashboard_data

In particular, note how you need to first define a dictionary with the fields defined and an empty array for the features. Then you need to loop through every feature in your FeatureSet to push attributes to that dictionary. It will look very different from what you have posted.

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

var fs = FeatureSetByPortalItem(
    portal,
    'fcb8c011c9f44ad09e735683da808b41',
    0,
    ['some_field'],
    false
);

var out_dict = {
    fields: [
        {name: 'some_field', type: 'esriFieldTypeInteger'},
        {name: 'difference_from_last', type: 'esriFieldTypeInteger'}],
    geometryType: '',
    features: []
}

var prev_val = 0

for (var f in fs) {
    Push(
        out_dict['features'],
        {
            attributes: {
                some_field: f['some_field'],
                difference_from_last: f['some_field'] - prev_val
            }
        }
    )
    
    prev_val = f['some_field']
}

return FeatureSet(Text(out_dict))
- Josh Carlson
Kendall County GIS
0 Kudos
MarlaJohnson
New Contributor III
I updated it to this but not exactly getting the correct data back. One day someone puts in data, a number. The second day someone puts in data, a number. I need it to look at the second day and subtract it from the first day. It is easy in python.
var portal = Portal('https://www.arcgis.com');
var fs = FeatureSetByPortalItem(portal, 'fcb8c011c9f44ad09e735683da808b41', 0, ['INSPAnswerNumber'], false);

var out_dict = {
fields: [
{ name: 'INSPAnswerNumber', type: 'esriFieldTypeInteger' },
{ name: 'difference_from_last', type: 'esriFieldTypeInteger' }
],
geometryType: '',
features: []
};

var prev_val = 0;
for (var feature in fs) {
var current_val = feature['INSPAnswerNumber'];
Push(out_dict['features'], {
attributes: {
INSPAnswerNumber: current_val,
difference_from_last: current_val - prev_val
}
});
prev_val = current_val;
}

return FeatureSet(Text(out_dict));
0 Kudos
jcarlson
MVP Esteemed Contributor

Well, that can depend on the data itself. Is the field actually an integer, or does it need to be converted?

Are you records already in the appropriate order? You may need to use Sort if not.

What are you getting back, if anything?

- Josh Carlson
Kendall County GIS
0 Kudos
MarlaJohnson
New Contributor III
The data is an interger I am getting back weird numbers that make no sense.
0 Kudos
jcarlson
MVP Esteemed Contributor

Can you give some examples? It's hard to know how the expression is failing without knowing what it's actually doing.

- Josh Carlson
Kendall County GIS
0 Kudos