Arcade expression to convert 1,0 to Yes, No within for loop for expression in popup

2215
6
Jump to solution
07-26-2021 11:34 AM
FPGIS_FrancescaRohr
New Contributor III

Hello Community

I would like my arcade expression to return 'Yes' or 'No' instead of '1' or '0' within an if else and for loop clause in my pop-up. My expression is getting data from another feature layer, filtering that feature layer for matching unique ids with the current layer, and returning attribute information when a match is found. The 'mdca' attribute is a 1 or 0 and I would like to return Yes or No. Here is the expression:

var inspections = FeatureSetByName($map,"UCF Tree Inspections - UCF Tree Inspections")
var treeuniqueid = Text($feature["Grant_ID"]+"-"+$feature["Tree_ID"])
var sql = "Tree_UniqueID = @treeuniqueid"
Console(sql)
var inspection = Filter(inspections, sql)
var cnt = Count(inspection)
var history = ""
if (cnt > 0){
history = "inspections: " + cnt
for (var inspection in inspections){
var mdca = Text(inspection.MDCA_Correct)
history = "MCA Correct: " + mdca
}
}else {
history = " "
}
return history

I have tried to use 'Boolean' instead of 'Text' but then I get a return of 'false' for a 1 which is the opposite of what I want. 

I have also unsuccessfully tried to use 'when' in several places.

Any help would be greatly appreciated. Thank you.

Francesca

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You can use IIF to return a Yes or No

history = "MCA Correct: " + IIF(inspection.MDCA_Correct == 1, 'Yes','No'); //if MDCA_Correct is a number
history = "MCA Correct: " + IIF(inspection.MDCA_Correct == '1', 'Yes','No'); //if MDCA_Correct is a string

 

 

View solution in original post

6 Replies
KenBuja
MVP Esteemed Contributor

You can use IIF to return a Yes or No

history = "MCA Correct: " + IIF(inspection.MDCA_Correct == 1, 'Yes','No'); //if MDCA_Correct is a number
history = "MCA Correct: " + IIF(inspection.MDCA_Correct == '1', 'Yes','No'); //if MDCA_Correct is a string

 

 

jcarlson
MVP Esteemed Contributor

I think part of the problem is that you use inspection as a variable twice. If I'm reading your code correctly, the first inspection is the inspections FeatureSet, as filtered by the sql string. The second inspection is the variable you're using in your for-loop.

I would, first and foremost, change the variable in your loop. Even though the loop and the broader expression have different scopes, it's still a good practice to not re-use names like that. Secondly, it seems that it would be more useful for you to iterate over the inspection object, otherwise why filter it in the first place?

Also, some clarification would be helpful. Is the MDCA_Correct field the boolean value? And are there multiple inspections that may occur for a given tree? As written, your expression is only going to return the value from the final loop, as it will overwrite the history string on each iteration.

All that aside, and making some assumptions about your code, try something like the following expression. This is a case where the function Decode is quite useful.

 

// Get features. Appears only two fields are necessary, and no geometry. Specifying those parameters will help performance.
var inspections = FeatureSetByName(
    $map,
    "UCF Tree Inspections - UCF Tree Inspections",
    ['Tree_UniqueID', 'MDCA_Correct'],
    false)

// Set up filter
var treeuniqueid = Text($feature["Grant_ID"]+"-"+$feature["Tree_ID"])
var sql = "Tree_UniqueID = @treeuniqueid"

// Filter inspections
var inspection = Filter(inspections, sql)

var cnt = Count(inspection)
var history = ""

if (cnt > 0){
    history = "inspections: " + cnt
    for (var i in inspection){
        var mdca = i.MDCA_Correct
        var bool_str = Decode(mdca, 1, 'True', 0, 'False', 'N/A')
        history = "MCA Correct: " + bool_str
    }
} else {
    history = " "
}

return history

 

 

Now, supposing that there are more than one inspections and you'd like to see them all, you could do something like:

for(var i in inspection){
var mdca = i.MDCA_Correct
var bool_str = Decode(mdca, 1, 'True', 0, 'False', 'N/A')
history += '\nMCA Correct: " + bool_str
}

 Using "+=" will append the result to the output string rather than overwrite previous values.

- Josh Carlson
Kendall County GIS
MikeSlattery
Occasional Contributor

Thanks for pointing out Decode!

0 Kudos
FPGIS_FrancescaRohr
New Contributor III

Thanks Josh. Currently we only have one inspection per tree but in the near future there will be more than one. I've saved your code to try when I need it.

0 Kudos
jcarlson
MVP Esteemed Contributor

Sure thing! Just note that only the second code block is for a multiple inspections scenario; the first would suffice for your current process.

- Josh Carlson
Kendall County GIS
0 Kudos
KenBuja
MVP Esteemed Contributor

Be aware that you'll never see the line (even if you have only one tree)

history = "inspections: " + cnt

if you don't add in the "+=" here

history += '\nMCA Correct: " + bool_str