Need help with IF statement

91
7
Jump to solution
06-22-2021 02:50 PM
JeremyMoore2
New Contributor II

I have a nested If statement that checks a field then does a pull data on different csv files. It is working as intended however I need to add to it. I need a new if statement to check two fields for certain values then add in a fixed value if those two fields in a specific combination show up. 

For Example in plain text: If Field "Measurement Type" = "Primary Facilities" and Field "Hot_Spot_temp" is greather than 150 set the priority field to 10. 

in Survey123 it should look something like this however I have butchered the syntax and I'm having a hard time figuring out where it's failing:

if($Thermal_Measurement_Type} = "Primary Facilities' and number(${Hot_Spot_Temp}) > 150, '10', 0)

 

The entire nested if statement looks like this:

 

 

 

if(${Thermal_Measurement_Type} = 'Primary Facilities', pulldata('Primary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), 
if(${Thermal_Measurement_Type} = 'Secondary Facilities', pulldata('Secondary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), 
if(${Thermal_Measurement_Type} = 'Indirect Measurements', pulldata('Indirect_Measurements_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), 
if($Thermal_Measurement_Type} = "Primary Facilities' and number(${Hot_Spot_Temp}) > 150, '10'), 0)))

 

 

Edited the code to make it a little easier to read

 

0 Kudos
1 Solution

Accepted Solutions
DougBrowning
MVP Frequent Contributor

I think the one in bold below is an extra, remove it.  But you prob want '0' also since they are all strings.

if(${Thermal_Measurement_Type} = 'Primary Facilities', pulldata('Primary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if(${Thermal_Measurement_Type} = 'Secondary Facilities', pulldata('Secondary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if(${Thermal_Measurement_Type} = 'Indirect Measurements', pulldata('Indirect_Measurements_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if($Thermal_Measurement_Type} = "Primary Facilities' and number(${Hot_Spot_Temp}) > 150, '10'), 0)))

View solution in original post

0 Kudos
7 Replies
DougBrowning
MVP Frequent Contributor

I think the one in bold below is an extra, remove it.  But you prob want '0' also since they are all strings.

if(${Thermal_Measurement_Type} = 'Primary Facilities', pulldata('Primary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if(${Thermal_Measurement_Type} = 'Secondary Facilities', pulldata('Secondary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if(${Thermal_Measurement_Type} = 'Indirect Measurements', pulldata('Indirect_Measurements_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if($Thermal_Measurement_Type} = "Primary Facilities' and number(${Hot_Spot_Temp}) > 150, '10'), 0)))

View solution in original post

0 Kudos
JeremyMoore2
New Contributor II

I got it to work. I had to walk away, take a break, go out to the garden and cut a head of cabbage then rewrite the logic. End the end it looks like this

if(${Thermal_Measurement_Type} = 'Primary Facilities' and number(${Hot_Spot_Temp}) > 150, '10', 0)

The entire nested If statement looks like this:

 

if(${Thermal_Measurement_Type} = 'Primary Facilities', pulldata('Primary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if(${Thermal_Measurement_Type} = 'Secondary Facilities', pulldata('Secondary_Facilities_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if(${Thermal_Measurement_Type} = 'Indirect Measurements', pulldata('Indirect_Measurements_Priority', 'Priority', 'Rise', ${Hot_Spot_Above_Reference}), if(${Thermal_Measurement_Type} = 'Primary Facilities' and number(${Hot_Spot_Temp}) > 150, '10',  0))))

 

 

I love code and hate it at the same time. Thank you for your help

0 Kudos
DougBrowning
MVP Frequent Contributor

Yep that was the one I bolded.  You are prob going to want '0' though.

0 Kudos
JeremyMoore2
New Contributor II

You're right again. Ty Much

0 Kudos
Helen_Liang
New Contributor

Hi everyone, 

I have a similar problem with the if statements in Survey 123 Connect but I can't seem to figure out the error by myself and I'm hoping if I can ask for any assistance from you all.

I'm trying to develop a scoring system where if the person selected answer A from the previous question then a score of 10 is displayed, if answered B then a score of 1, and a score of 0 for selected unknown from the previous question. 

I have my if statement written as:

if(selected(${Question1},'Yes),'10',if(selected(${Question1},'No'),'1','0'))

I don't have any syntax errors but no matter what answer I chose from the previous selection, it still displays 0 for me. 

Any idea/help would be appreciated!! Thanks in advance! 

 

0 Kudos
DougBrowning
MVP Frequent Contributor

Looks ok to me.  My guess is that the built in lists are Yes No in the label but yes no for the name field.  Data always stores the name - It is case sensitive which is why you get 0. 

Why in the world the default list is diff cases I have no idea.  I strongly encourage you to make both the name and label the same or it will drive you nuts for years.

Hope that is it.

Helen_Liang
New Contributor

OMG thank you so much!! That was the problem!! 

I will remember to keep them the same in the future so I don't run into the same problem again!!