community
cancel
Showing results for 
Search instead for 
Did you mean: 

Alteryx designer Discussions

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

Concatenate fields with if else statement

Asteroid

Hi,

 

I have a list of names that I want to concatenate them with a seperator (comma) (please see below), constrained to following criteria

 

1- Remove all dots spaces from middle initial

2-If there is no middle initial, it should look like Adam, Thomas  not Adam, Thomas, ---> No comma at the end

3-Same procedure applies to first Name . If there is no FIrst Name, just include last name with no comma

 

Expect outcome should look like below. Not sure If i can do it with formula function by using if else (or something else) statement. Any help is appreciated 

 

First NameLast NameMiddle InitialOutput 
TomJackD.RTom,Jack,DR
SkuraDaniel Skura,Daniel
TimTreakFTim,Treak
TimHoward Tim,Howard
 Simmons Simmons

Probably the simplest way is to use the Data Cleansing tool to remove the spaces and . etc as initial clean up step.

You can then use a formula tool to make the concatenated string:

 

[Last Name]
+ IIF(IsNull([First Name]) || [First Name] = '', '', ', ' + [First Name])
+ IIF(IsNull([Middle Initial]) || [Middle Initial] = '', '', ', ' + [Middle Initial])

 

 

You can do this in one step using the REGEX_Replace function to clean up:

REGEX_Replace([Last Name], '[^A-Za-z]', '')
+ IIF(IsNull([First Name]) || REGEX_Replace([First Name], '[^A-Za-z]', '') = '', '', ', ' + REGEX_Replace([First Name], '[^A-Za-z]', ''))
+ IIF(IsNull([Middle Initial]) || REGEX_Replace([Middle Initial], '[^A-Za-z]', '') = '', '', ', ' + REGEX_Replace([Middle Initial], '[^A-Za-z]', ''))

Sample attached

 

 

 

Thanks for sharing the + SIGN tip for concatenation.  I also appreciate the REGEX_WITH REPLACE. The second item is very useful and nicely explained.  

Parse Error at char(153): Malformed IF statement (Expression #2)

 

 

[LN_ID] + "-" + [GL_MATRIX] + "-" +
IF [HfsFlag]="Y"
then "HFS"
elseif [HfsFlag]="N"
then "HFI" else ""
+
IF [FvoFlag] = "Y" THEN "-FVO"
ELSE ""
ENDIF

 

 

 

Abhay,

This works and thanks for your help.   I removed the pipes ( || ) from my Teradata SQL and used the +.

David 

Labels