Select to view content in your preferred language

Empty results from data expression in Dashboard

4092
15
Jump to solution
05-04-2022 04:03 PM
ElinorBrehmer_Österhult
Occasional Contributor

Hello!

I am new to data expressions in dashboard. I want to combine two columns from one layer, by dividing them with each other, but my code gives me no results. I also get no error message, it is just empty.

 

If anyone has an idea what is wrong with my code I am grateful to hear. Thanks in advance.

 

/Elinor

 


var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'),
'1db5c1db1d2d44a985b2a2144f6c663f', 0,
["M3SK_HA", "M3SK_HA_Lov"], false)


var percentLov = {
'fields': [{'name':'Lovpercent', 'type':'esriFieldTypeDouble'},
],
'geometryType': '',
'features':
[{'attributes':
{'Lovpercent': 'M3SK_HA_Lov'/'M3SK_HA'
}}]};


return FeatureSet(Text(percentLov));

15 Replies
ElinorBrehmer_Österhult
Occasional Contributor

I am experimenting now, and I found someone wrote in this forum that the expression must return a FeatureSet. Can one add something like this to your code:?

var Dict = {
'fields': [{'name':'Lovpercent', 'type':'esriFieldTypeDouble'},
{'name':'M3SK_HA_Lov', 'type':'esriFieldTypeDouble'},
{'name':'M3SK_HA', 'type':'esriFieldTypeDouble'}],
'geometryType': '',
'features':
[{'attributes':
{'Percent': ('M3SK_HA_Lov'/ 'M3SK_HA'),
'M3SK_HA_Lov': 'M3SK_HA_Lov',
'M3SK_HA': 'M3SK_HA'

}}]};

return FeatureSet(Text(Dict));

 

0 Kudos
jcarlson
MVP Esteemed Contributor

If you check out the function reference pages, the GroupBy function returns a FeatureSet, which is why it's a great workaround.

The thing with Data Expressions is that, yes, they need to use a FeatureSet, but when you define them yourself, you need to add every feature one by one. Defining the FeatureSet's features array as you have in your expression would leave you with a single feature.

Think of it like this:

 

features: [
    // feature 1
    { attributes: {
         attribute1: value,
         attribute2: value
    }},
    // feature 2
    { attributes: {
         attribute1: value,
         attribute2: value
    }},
    ... // and so on, for every feature in the layer
]

 

 

Manually defining each feature is not realistic, so most Data Expressions will use a for loop to iterate over the input features. If you wanted to go that route, you've got a bit more you'll need to add.

 

var fs = FeatureSetByPortalItem(
    Portal('https://www.arcgis.com'),
    '1db5c1db1d2d44a985b2a2144f6c663f', 0,
    ["M3SK_HA", "M3SK_HA_Lov"],
    false
)

var out_dict = {
    fields: [
        {name:'Lovpercent', type:'esriFieldTypeDouble'},
        {name:'M3SK_HA_Lov', type:'esriFieldTypeDouble'},
        {name:'M3SK_HA', type:'esriFieldTypeDouble'}
    ],
    geometryType: '',
    features: []
};

// Iterate over the fs FeatureSet
for (var f in fs){
    
    // calculate percentage
    var ha   = f['M3SK_HA']
    var ha_l = f['M3SK_HA_Lov']
    var p    = ha_l / ha
    
    // push values into feature array
    Push(
        out_dict['features'],
        {
            attributes: {
                Lovpercent: p,
                M3SK_HA_Lov: ha_l,
                M3SK_HA: ha
            }
        }
    )
}

return FeatureSet(Text(out_dict));

 

 

- Josh Carlson
Kendall County GIS
ElinorBrehmer_Österhult
Occasional Contributor

Hi Josh!

Big thanks for the explanation how GroupBy works and iterations. The code you wrote was working very good and gave the desired output, I am very grateful for all help! 😄 

0 Kudos
jcarlson
MVP Esteemed Contributor

You're welcome! I'm glad it's working for you!

- Josh Carlson
Kendall County GIS
jcarlson
MVP Esteemed Contributor

PS - This GitHub repo is probably the best collection of good, working examples of Data Expressions: https://github.com/Esri/arcade-expressions/tree/master/dashboard_data 

- Josh Carlson
Kendall County GIS
ElinorBrehmer_Österhult
Occasional Contributor

Thanks for the link, this page was very good to see real working examples also, even if I wouldn't have been able to do the code you wrote for me based on these examples. I will keep on learning.

0 Kudos