Select to view content in your preferred language

Multiple IF Statements from different fields

594
2
Jump to solution
07-05-2022 02:54 PM
Labels (1)
David-Semitekol
Occasional Contributor

Hello, I'm trying to write 1 arcade expression.  In my table I have a field for Sunday; Monday; Tuesday; etc.  The data is either a 1 if a work crew is at that location on that day or a 0 if they are not.

I want my expression to list the days of the week they are at each location but I'm only able to return the first instance with my formulas.

I tried and IF and Else IF statement

if($feature["USER_SUNDAY"] == 1)
{return " Su "}

else if($feature["USER_MONDAY"] == 1)
{return " M "}

else if($feature["USER_TUESDAY"] == 1)
{return " Tu "}

else if($feature["USER_WEDNESDAY"] == 1)
{return " W "}

else if($feature["USER_THURSDAY"] == 1)
{return " Th "}

else if($feature["USER_FRIDAY"] == 1)
{return " F "}

else if($feature["USER_SATURDAY"] == 1)
{return " Sa "}

And also tried a nested statement:

iif($feature["USER_SUNDAY"] == 1,'Su',
iif($feature["USER_MONDAY"] == 1,'M',
iif($feature["USER_TUESDAY"] == 1,'Tu',
iif($feature["USER_WEDNESDAY"] == 1,'W',
iif($feature["USER_THURSDAY"] == 1,'Th',
iif($feature["USER_FRIDAY"] == 1,'F',
iif($feature["USER_SATURDAY"] == 1,'Sa',NONE)))))))

I even tried replacing IIF with When but achieved the same result.  The formulas only return the first day a crew is at a location and won't combine the days if they are there more than once in the PopUp.

Is this possible or do I need to create an expression for each day of the week and then add it to my PopUp?

Thanks!

Tags (1)
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

When you return from a function (or an Arcade expression in this case), the code stops executing there. Everything after the first executed return statement gets skipped. That's why your first example didn't work.

It's also why your second example didn't work, because it can be rewritten like this:

if(condition1){
    return "Su"
} else {
    if(condition2) {
        return "Mo"
    } else {
        if(condition3) {
            return "Tu"
        } else {
...

 

To do what you want:

  • evaluate an if statement for each condition
  • either build up a return string as you go along or store each string and concatenate at the end
// array to store the strings
var open_days = []

// evaluate each day, add open days to the array
if($feature.USER_SUNDAY == 1) { Push(open_days, "Su") }
if($feature.USER_MONDAY == 1) { Push(open_days, "M") }
if($feature.USER_TUESDAY == 1) { Push(open_days, "Tu") }
if($feature.USER_WEDNESDAY == 1) { Push(open_days, "W") }
if($feature.USER_THURSDAY == 1) { Push(open_days, "Th") }
if($feature.USER_FRIDAY == 1) { Push(open_days, "F") }
if($feature.USER_SATURDAY == 1) { Push(open_days, "Sa") }

// concatenate the strings and return
return Concatenate(open_days, ", ")

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

When you return from a function (or an Arcade expression in this case), the code stops executing there. Everything after the first executed return statement gets skipped. That's why your first example didn't work.

It's also why your second example didn't work, because it can be rewritten like this:

if(condition1){
    return "Su"
} else {
    if(condition2) {
        return "Mo"
    } else {
        if(condition3) {
            return "Tu"
        } else {
...

 

To do what you want:

  • evaluate an if statement for each condition
  • either build up a return string as you go along or store each string and concatenate at the end
// array to store the strings
var open_days = []

// evaluate each day, add open days to the array
if($feature.USER_SUNDAY == 1) { Push(open_days, "Su") }
if($feature.USER_MONDAY == 1) { Push(open_days, "M") }
if($feature.USER_TUESDAY == 1) { Push(open_days, "Tu") }
if($feature.USER_WEDNESDAY == 1) { Push(open_days, "W") }
if($feature.USER_THURSDAY == 1) { Push(open_days, "Th") }
if($feature.USER_FRIDAY == 1) { Push(open_days, "F") }
if($feature.USER_SATURDAY == 1) { Push(open_days, "Sa") }

// concatenate the strings and return
return Concatenate(open_days, ", ")

Have a great day!
Johannes
David-Semitekol
Occasional Contributor

Thanks Johannes, your solution code worked perfectly.  Just to make sure I learned and not just copied:

You assign a variable, in this case it is open_days and the = [] allows you to add to it later in the formula?

The Push command takes the output from the If statement and adds it to the variable.

At the end you are Returning the values from the If statements and adding a comma to the output for readability.

This was awesome, thank you again!

0 Kudos