Select to view content in your preferred language

Arcade: Calculate a Field

502
2
03-22-2023 11:27 AM
arc_ticodex
Regular Contributor

Hi everyone!
I'm relatively new to Arcade and i'm trying to calculate a calculate Field in FeatureSet.

Basicly I have a String Field with domains. I want to create a new field that turns those string domains into numbers so I can have a custom order in my serial chart in Dashboards.

For exemple, I have the values: Very bad; Bad; Good; Very Good;
I need to create a new field that turns those values into numbers: Very bad = 0; Bad = 1; Good = 2; Very Good = 3;

Any idea on how to start?
Thanks!!!!!

 

0 Kudos
2 Replies
jcarlson
MVP Esteemed Contributor

Check out the Decode function, which is actually being used to "encode" your data in this case.

var string_field = $feature['your_field']

var coded_field = Decode(
    string_field,
    'Very bad', 0,
    'Bad', 1,
    'Good', 2,
    'Very Good', 3,
    -1 // the function wants a default value when no conditions are met, so we'll use -1
)

return coded_field
- Josh Carlson
Kendall County GIS
JohannesLindner
MVP Frequent Contributor
var codes = {
    "Very bad": 0,
    "Bad": 1,
    "Good": 2,
    "Very good": 3,
}

if(HasKey(codes, $feature.StringField)) {
    return codes[$feature.StringField]
}
return 999 // default value if you forgot a code

Have a great day!
Johannes
0 Kudos