Alteryx Designer Desktop Discussions

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

Regex Replace

girisri
7 - Meteor

HI,

 

How can Regex Replace be used to achieve the below thru Multi-Row formula tool- Problem and Expected result as below:

Problem
NameValueID
N1Annual, Annual Annual1
N1 2
N1 3
N2Annual, Quarterly, Monthly1
N2 2
N2 3
   
Expected Result
NameValueID
N1Annual1
N1Annual2
N1Annual3
N2Annual1
N2Quarterly2
N2Monthly3
5 REPLIES 5
DavidP
17 - Castor
17 - Castor

Her're an alternative to regex for this problem.

 

alternative to regex.png

Kenda
16 - Nebula
16 - Nebula

Hey @girisri

 

If you're really wanting to use Reg_Ex and your data is always in the format shown in your example (with three rows per name and all of the values listed in the first row of the value field separated by commas), you could do it like this:

 

Add a Multi-Row Formula tool to edit the Value field with this expression:

trim(iif([ID]=2,regex_replace([Row-1:Value],"(.*)\,(.*)\,(.*)","$2"),iif([ID]=3,regex_replace([Row-2:Value],"(.*)\,(.*)\,(.*)","$3"),[Value])))

Capture.PNG

 

 

Then add a normal Formula tool to edit the Value field again with this expression:

trim(IIF([ID]=1, REGEX_Replace([Value], "(.*)\,(.*)\,(.*)","$1"), [Value]))

 

Note I had to add a comma for the N1 Value field as your sample data did not have that. As I said, this will work so long as your real data has the same form as your sample. 

 

If you have a dynamic number of rows, however, I would recommend approaching this like @DavidP suggested with using the Text To Columns tool first, Filtering out the empty values then modifying the ID field. If you want to still use the Multi-Row Formula tool here instead of the Tiles, you could group by the Name field and modify the ID field with this expression:

[Row-1:ID]+1

 

Hope this helps!

MarqueeCrew
20 - Arcturus
20 - Arcturus

@girisri,

 

While @Kenda has used a regular expression, I wouldn't use one for this requirement.  The pattern that I see is that the ID indicates which word to use.  For that reason I would use the GetWord() function instead.  GetWord finds the zero-based word number.  In other words, Word 1 is Word 0.

 

Picture1.png

 

My approach was to first find the MAX (Value and ID) then create the required number of rows.  Then I calculate the word for the value and rename fields.

 

Cheers,


Mark

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
girisri
7 - Meteor

@MarqueeCrew 

Thanks! Nice way to solve it!

girisri
7 - Meteor

@DavidP

Thanks, This works too!

Labels