Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Writing an If statement with multiple OR

soumya
7 - Meteor

How can I write an if then statement which has multiple conditions in it? I tried using || instead of "or" but it didn't work. Is there any way to write this logic? 

 

For eg: 

 

if site name = "Yahoo" OR "Google" 

then "X"

Elseif site name = "CNN" OR "CBS"

then "Y"

ELSEIF "Z" endif 

8 REPLIES 8
DylanB
Alteryx Alumni (Retired)
if [site name] = "Yahoo" OR [site name] = "Google" then "X"
ELSEIF [site name] = "CNN" OR [site name] = "CBS" then "Y"
ELSE "Z" 
ENDIF

Think of the "OR" as an operator on two logical statements, so the code before and after should each evaluate to TRUE or FALSE

JohnJPS
15 - Aurora

I'm not at my computer, but you can certainly do something like

 

IF [siteName] == "Google" OR [siteName] == "Yahoo" THEN

 

... you could also check for IN... e.g.

 

IF [siteName] IN ("Google","Yahoo") THEN

 

jdunkerley79
ACE Emeritus
ACE Emeritus

Worth considering the SWITCH statement as well:

 

SWITCH([site name],"Z",
    ."Yahoo","X"
    ,"Google","X",
    ,"CNN","Y",
    ,"CBS","Y"
)
jdunkerley79
ACE Emeritus
ACE Emeritus

Too many commas in my formula. Fixed version:

 

SWITCH([site name],"Z"
    ,"Yahoo","X"
    ,"Google","X"
    ,"CNN","Y"
    ,"CBS","Y"
)

(Shouldn't have written it on a mobile!)

ignas
8 - Asteroid

In this case and in a lot of other cases Switch formula is better and it is shorter.

ejohnson
5 - Atom

I want to do something similar to this, but use the contain formula as the field may have other information as well. So I tried the following, but it does not bring out the results I'm expecting...

 

if contains("Yahoo",[site name]) OR contains("Google",[site name]) then "X"
ELSE "Z" 
ENDIF

 

nsessa
7 - Meteor

I am having the same issue as well. Does anyone have a resolution? 

JohnJPS
15 - Aurora

@ejohnson , @nsessa ,

I think the issue in your example is just the order of the field vs what is contained in the field: list the field name first:

if contains([site name],"Yahoo") OR contains([site name],"Google") then "X"
ELSE "Z" 
ENDIF

Hope that helps!

John

Labels