Multiple IIF() Statements in Arcade Expression

31051
11
Jump to solution
05-10-2018 06:01 AM
Lake_Worth_BeachAdmin
Occasional Contributor III

I am trying to use multiple IIF() statements inside a single arcade expression.

Before I was creating 10 separate expressions and using them all in my pop-up configs, but I am now facing the challenge with 30+ IIF() statements for a single field and do not want create 30+ arcade expressions and incorporate all 30 inside pop-up config.

my method which I thought would work but its only displaying blank values

IIF($feature.field =='apple','fruit','')

IIF($feature.field=='carrot','ewwwwww','')

IIF($feature.field=='candy','yummy','')

I want the above all inside a single expression and have it display the desired output depending on field's value.

however the above is not displaying any output even if the field value is candy, or carrot or apple.

is multiple IIF() statements on a single field a limitation of arcade?

Tags (2)
1 Solution

Accepted Solutions
PaulBarker
Esri Contributor

First carrots are delicious! 

Take a look at the When() statement for simple cases .  Otherwise you can use the If statement and add in some "else if" conditions as needed.

View solution in original post

11 Replies
PaulBarker
Esri Contributor

First carrots are delicious! 

Take a look at the When() statement for simple cases .  Otherwise you can use the If statement and add in some "else if" conditions as needed.

Lake_Worth_BeachAdmin
Occasional Contributor III

for else if situations is there a "elif" statement as well?

0 Kudos
KenBuja
MVP Esteemed Contributor

Yes, that would be

if ($feature.field =='apple'){
  return 'fruit';
} else if ($feature.field=='carrot') {
  return 'ewwwwww';
} else if ($feature.field=='candy'){
  return 'yummy';
} else {
  return 'other';
}
LoganAshmore1
New Contributor III

iif(<condition>,<true value>,<false value>)

With arcade language, it is possible to create nested iif formulas by adding additional iif stamens inside the <false value> output. They end up being loaded with  parentheses at the end(if you had over 30, your last line will have over 30 parentheses), but are easy to script and do work well as single expressions in popups for ArcOnline.

Your sample would look something like this:

IIF($feature.field == 'apple','fruit',
IIF($feature.field == 'carrot','ewwwwww',
IIF($feature.field == 'candy','yummy','')))

The formula starts with an initial iif statement, nests a second iif in the firsts <false value>, and then nests a third iif in the second <false value>. This can go pretty far, I've tried it 22 deep before. Just remember to put something for the last <false value>  criteria to close everything off. 

Here is another example I used recently for a field calculator to collect 71 numeric categories into 11 numeric groups:

iif($feature.Class_2012 <=9 , 1,
iif($feature.Class_2012 <=19, 2,
iif($feature.Class_2012 <=29, 3,
iif($feature.Class_2012 == 30, 4,
iif($feature.Class_2012 == 33, 5,
iif($feature.Class_2012 <=41, 6,
iif($feature.Class_2012 <=44, 7,
iif($feature.Class_2012 <=46, 8,
iif($feature.Class_2012 == 47, 9,
iif($feature.Class_2012 <=59, 10,
iif($feature.Class_2012 >= 60, 11,0)))))))))))

Lake_Worth_BeachAdmin
Occasional Contributor III

ahhh this is nice, thank you sir I love nested statements

0 Kudos
LoganAshmore1
New Contributor III

For future reference to other people who see this, iif statements can be nested in either the <true value> or <false value>...or even both. It is my preference to structure the statements in a way that it pares down the consecutive false results, simply because it is cleaner to write and easier to follow. 

KristianEkenes
Esri Regular Contributor

I personally find When() and Decode() easier to read in these cases. 

So this: 

IIF($feature.field == 'apple','fruit', 
IIF($feature.field == 'carrot','ewwwwww', 
IIF($feature.field == 'candy','yummy','')))

 becomes...

Decode( $feature.field,
  'apple','fruit',
  'carrot','ewwwwww',
  'candy','yummy',
'none')

and this:

iif($feature.Class_2012 <=9 , 1, 
iif($feature.Class_2012 <=19, 2, 
iif($feature.Class_2012 <=29, 3,
iif($feature.Class_2012 == 30, 4, 
iif($feature.Class_2012 == 33, 5, 
iif($feature.Class_2012 <=41, 6,
iif($feature.Class_2012 <=44, 7,
iif($feature.Class_2012 <=46, 8,
iif($feature.Class_2012 == 47, 9,
iif($feature.Class_2012 <=59, 10,
iif($feature.Class_2012 >= 60, 11,0)))))))))))

becomes:

When( $feature.Class_2012 <=9, 1, 
  $feature.Class_2012 <=19, 2, 
  $feature.Class_2012 <=29, 3,
  $feature.Class_2012 == 30, 4, 
  $feature.Class_2012 == 33, 5, 
  $feature.Class_2012 <=41, 6,
  $feature.Class_2012 <=44, 7,
  $feature.Class_2012 <=46, 8,
  $feature.Class_2012 == 47, 9,
  $feature.Class_2012 <=59, 10,
  $feature.Class_2012 >= 60, 11,
0 )

That way you don't have to count a bunch of closing parentheses.  

Lake_Worth_BeachAdmin
Occasional Contributor III

I like the when statement, much easier to read

0 Kudos
LoganAshmore1
New Contributor III

100% agree, WHEN or DECODE are typically what people are looking for instead of IIF for long statements. I only end up with long nested IIF statements when I don't originally intend on having a long formula...but things sort of keep adding as I play and I know the project is a one-off.

I guess the purpose of my post was to answer the original question "is multiple IIF() statements on a single field a limitation of arcade?". To which the answer was no, multiple IIF stamens can be combined for the given application.