Select to view content in your preferred language

Help with Arcade expression

1026
5
Jump to solution
02-10-2022 06:26 AM
JoshNock1
Occasional Contributor

Hi,

I am looking to create an arcade expression that compares values from multiple fields and returns the highest values.
However, some fields have identical values.

Hopefully my example below better explains the issue

The data layer might contain the following 5 colour fields. With corresponding values.

Blue   Green   Yellow   Orange   Red
   6         8            2              8            8

And what I would like returned is Green, Orange, Red

Any help would be greatly appreciated, thanks

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

If I am understanding this correctly, you want the expression to return all colours whose value is equal to the maximum value. This could be a single colour, or multiple, if there are ties.

Finding the max value is easy enough:

var colour_values = [
    $feature.blue,
    $feature.green,
    $feature.yellow,
    $feature.orange,
    $feature.red
]

var max_val = Max(colour_values)

But then once you get the value, what do you do with it?

A simple approach is to just get the index positions of the maximum values, then decode those back to the names. Matching values can be pushed to an array, and we can use Concatenate to format the array as a nice string.

var top_colours = []

for (var v in colour_values){
    if (colour_values[v] == max_val){
        var colour_name = Decode(
            v,
            0, 'Blue',
            1, 'Green',
            2, 'Yellow',
            3, 'Orange',
            4, 'Red',
            ''
        )
        
        Push(top_colours, colour_name)
    }
}

return Concatenate(top_colours, ', ')

Here's the output from manually entering your example values:

jcarlson_1-1644506718929.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

5 Replies
jcarlson
MVP Esteemed Contributor

If I am understanding this correctly, you want the expression to return all colours whose value is equal to the maximum value. This could be a single colour, or multiple, if there are ties.

Finding the max value is easy enough:

var colour_values = [
    $feature.blue,
    $feature.green,
    $feature.yellow,
    $feature.orange,
    $feature.red
]

var max_val = Max(colour_values)

But then once you get the value, what do you do with it?

A simple approach is to just get the index positions of the maximum values, then decode those back to the names. Matching values can be pushed to an array, and we can use Concatenate to format the array as a nice string.

var top_colours = []

for (var v in colour_values){
    if (colour_values[v] == max_val){
        var colour_name = Decode(
            v,
            0, 'Blue',
            1, 'Green',
            2, 'Yellow',
            3, 'Orange',
            4, 'Red',
            ''
        )
        
        Push(top_colours, colour_name)
    }
}

return Concatenate(top_colours, ', ')

Here's the output from manually entering your example values:

jcarlson_1-1644506718929.png

 

- Josh Carlson
Kendall County GIS
JoshNock1
Occasional Contributor

That worked wonders, thank you Josh

KimGarbade
Frequent Contributor

I had written code that worked, but I used an array formatted like this:

var myArray = ["Blue:"+$feature.Blue,"Yellow:"+$feature.Yellow...]

So the array looked like this example: ["Blue:2","Yellow:5","Green:9","Orange:9","Red:8"]

Using this method I had to use Left, and Right, Find, and Count functions to parse the array values, Yuck. 

That Decode function!  Wow what a game changer!  Not sure how I missed knowing about it, but that is very cool.  Thanks for that.

0 Kudos
jcarlson
MVP Esteemed Contributor

Decode is a great function to work into your expressions. Feels like every month or so there's an Arcade function that somehow I never learned about, and I realize I've been doing things inefficiently!

I'd thought about a slightly different way of approaching this issue using an array of dictionaries, actually, and your expression sort of looked like it.

var colours = [
{colour:'Blue', value:3},
{colour:'Green', value:5},
...
]

And then using array-specific functions to sort and filter.

- Josh Carlson
Kendall County GIS
Robert_LeClair
Esri Notable Contributor

If you use the Max expression for numeric fields, the label will return the max value.  For example, in my cities feature class I have 2 numeric fields, Xcoord and Ycoord. If I type Max($feature.Xcoord,$feature.Ycoord) for the expression, the label drawn on the map returns the highest value.  See this Esri Community response for more details.

0 Kudos