570
2
12-08-2023 07:32 AM
by
MVP Regular Contributor

Based on the sample data how can I create a new column/variableA&B, based on when certain conditions are met from two columns using Arcade and then assign colors to the new column's categorical values accordingly?

I am using Pro 3.1.

Sample data:

``````ID A       B       A&B
1  Present Present A Present & B Present
2  Absent  Present A Absent  & B Present
3  Present Absent  A Present & B Absent
4  Absent  Absent  A Absent  & B Absent
5  Present Present A Present & B Present
6  Absent  Present A Absent  & B Present
7  Present Absent  A Present & B Absent
8  Absent  Absent  A Absent  & B Absent``````

Color conditions:

• If A Present & B Present then color red.
• If A Absent & B Present or A Present & B Absent then color orange.
• If A Absent & B Absent then color yellow.
Question | Analyze | Visualize
Tags (3)
1 Solution

Accepted Solutions
by
MVP Esteemed Contributor

You don't necessarily need to calculate a whole new field for this, but you could. Whether you calculate the field or use this expression directly in the symbology settings, it is the same.

This is simple enough in Arcade. Note, however, that the expression won't directly set the color. Rather, it will return a value that your symbology settings will use for categories. You can call them 'red', 'orange', and 'yellow', but they may as well be 'a', 'b', and 'c' for all the different it makes. Colors are set separately from the expression.

``````return when(
\$feature.A == 'Present' && \$feature.B == 'Present', 'red',
(\$feature.A == 'Absent' && \$feature.B == 'Present') || (\$feature.A == 'Present' && \$feature.B == 'Absent'), 'orange',
\$feature.A == 'Absent' && \$feature.B == 'Absent', 'yellow',
'other'
)``````

However, you don't really need all this. Since it doesn't matter which value is absent/present, just the count, we can simplify matters.

``````// this will create a 2-item array of either True or False
var present = [
\$feature.A == 'Present',
\$feature.B == 'Present'
]

return when(
All(present), 'red',    // matches when both are 'Present'
Any(present), 'orange', // matches when either are 'Present', but only evaluates if the first check is false; in other words, when only 1 is 'Present'
'yellow' // logically anything else will be when both are 'Absent', so we don't actually need any conditions
)``````

- Josh Carlson
Kendall County GIS
2 Replies
by
MVP Regular Contributor

@KenBuja happy Friday, hope all is well, when you have the time, can you please have a look into this? Cheers!

Question | Analyze | Visualize
by
MVP Esteemed Contributor

You don't necessarily need to calculate a whole new field for this, but you could. Whether you calculate the field or use this expression directly in the symbology settings, it is the same.

This is simple enough in Arcade. Note, however, that the expression won't directly set the color. Rather, it will return a value that your symbology settings will use for categories. You can call them 'red', 'orange', and 'yellow', but they may as well be 'a', 'b', and 'c' for all the different it makes. Colors are set separately from the expression.

``````return when(
\$feature.A == 'Present' && \$feature.B == 'Present', 'red',
(\$feature.A == 'Absent' && \$feature.B == 'Present') || (\$feature.A == 'Present' && \$feature.B == 'Absent'), 'orange',
\$feature.A == 'Absent' && \$feature.B == 'Absent', 'yellow',
'other'
)``````

However, you don't really need all this. Since it doesn't matter which value is absent/present, just the count, we can simplify matters.

``````// this will create a 2-item array of either True or False
var present = [
\$feature.A == 'Present',
\$feature.B == 'Present'
]

return when(
All(present), 'red',    // matches when both are 'Present'
Any(present), 'orange', // matches when either are 'Present', but only evaluates if the first check is false; in other words, when only 1 is 'Present'
'yellow' // logically anything else will be when both are 'Absent', so we don't actually need any conditions
)``````

- Josh Carlson
Kendall County GIS