Hello, Just started using Alteryx this week and I have read through some of the suggestions on how to create the "if contains" formula. I'm not sure if I am missing something but it keeps returning a "null" value. My assignment column has several different grade types which I need to group in into one grade. What I am I trying to achieve is if the assignment column says "MKH" or "SHB" etc. then I want my expression to return "SMA" in the output column.
below is how I wrote it out. I maybe complicating it but if any one has any suggestions that would be great.
IF Contains ([Assignment], "HSH") THEN "SMA"
ELSEIF Contains([Assignment], "MKH") THEN "SMA"
ELSEIF Contains([Assignment], "SHB") THEN "SMA"
ELSEIF Contains([Assignment], "SCS") THEN "SMA"
ELSEIF Contains([Assignment], "SYB") THEN "SMA"
ELSEIF Contains([Assignment], "PXB") THEN "SMA"
ELSE Null()
ENDIF
Solved! Go to Solution.
since you are not using case sensitive option , ensure the text matches exactly.
Give this a try:
IF Contains([Assignment],"HSH")==-1 THEN "SMA"
ELSEIF Contains([Assignment],"MKH")==-1 THEN "SMA"
ELSEIF Contains([Assignment],"SHB")==-1 THEN "SMA"
ELSEIF Contains([Assignment],"SCS")==-1 THEN "SMA"
ELSEIF Contains([Assignment],"SYB")==-1 THEN "SMA"
ELSEIF Contains([Assignment],"PXB")==-1 THEN "SMA"
ELSE Null()
ENDIF
A 'true' condition of the Contains( function will return the value 'true' if the field is Boolean, and '-1' in other situations.
EDIT: @Avinash_K had a great suggestion. Here's the updated version:
IF Contains(Uppercase([Assignment]),"HSH")==-1 THEN "SMA"
ELSEIF Contains(Uppercase([Assignment]),"MKH")==-1 THEN "SMA"
ELSEIF Contains(Uppercase([Assignment]),"SHB")==-1 THEN "SMA"
ELSEIF Contains(Uppercase([Assignment]),"SCS")==-1 THEN "SMA"
ELSEIF Contains(Uppercase([Assignment]),"SYB")==-1 THEN "SMA"
ELSEIF Contains(Uppercase([Assignment]),"PXB")==-1 THEN "SMA"
ELSE Null()
ENDIF
Another option if you only want to specify the true output ("SMA") once:
IF (Contains(Uppercase([Assignment]),"HSH")==-1
OR Contains(Uppercase([Assignment]),"MKH")==-1
OR Contains(Uppercase([Assignment]),"SHB")==-1
OR Contains(Uppercase([Assignment]),"SCS")==-1
OR Contains(Uppercase([Assignment]),"SYB")==-1
OR Contains(Uppercase([Assignment]),"PXB")==-1) THEN "SMA"
ELSE Null()
ENDIF
Hi @Akullar
I would suggest using a Text Input (or Excel Input) to store all the words you would like to match in a column and then use a Find and Replace tool. This method is easier to maintain and more scalable. For example, if you would like to add more words to search for and/or new Groups other than "SMA", all you need to do is to update the Text Input rather than rewriting the long formula in your Formula tool.
hey @Jasperich
I ended up using your method for some reason the suggestions above still returned null values, when I have more time I'll dig into it deeper and see why.
Thanks for the help everyone!
Hi @Akullar
The method above will return a null when no matching word is found. Would you like it to return some text if no match word is found? if this is the case, you will need to add a formula tool after the find and replace tool.
Jasper