Let’s talk Alteryx Copilot. Join the live AMA event to connect with the Alteryx team, ask questions, and hear how others are exploring what Copilot can do. Have Copilot questions? Ask here!
Start Free Trial

Alteryx Designer Desktop Discussions

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

Regex expression to derive/calculate Month and Year from column 'months_yr'

c_raviram
7 - Meteor

Hi Experts, can you pls let me know a regex expression to derive month and year for below column "months_yr"

months_yr is month number and the years are differentiated with "_2".

Values with "_2" is for year 2022 and without "_2" is for year 2021

 

Sample example below:

I have column "months_yr" in my dataset and I am looking for a regex expression or formula to derive columns "Month" and "Year"

months_yrMonthYear
1012021
1_2012022
10_2102022
11112021
11_2112022
12122021
12_2122022

 

Regards, Ravi

4 REPLIES 4
Luke_C
17 - Castor
17 - Castor

Hi @c_raviram 

 

Here's an example using regex_replace & regex_match, although you can definitely accomplish the same results without regex. 

 

Luke_C_0-1636466754994.png

 

 

Jonathan-Sherman
15 - Aurora
15 - Aurora

Hi @c_raviram,

 

I would use a regex formula to calculate the month and a standard if condition to calculate the year:

 

Month:

PadLeft(REGEX_Replace([months_yr], "^(\d+)_.*", "$1"),2,"0")

 

Year:

IF EndsWith([months_yr], "_2")
THEN "2022"
ELSE "2021"
ENDIF

 

 

JonathanSherman_0-1636466768384.png

 

I've attached my workflow for you to download if needed!

 

Kind regards,

Jonathan

 

HomesickSurfer
12 - Quasar

Hi @c_raviram 

 

Here's my non-Regex solution.

Workflow attached.

 

Capture.PNG

MarqueeCrew
20 - Arcturus
20 - Arcturus

@c_raviram ,

 

I want to help you AVOID RegEx for this solution.  The ability to parse the data without RegEx is a reason not to use RegEx.  Here is a formula that creates a new variable that is easily parsed with a text to columns tool.  You can also alter the formula to create 2 variables in a formula if you so choose.

 

 

IF Contains([months_yr], "_") THEN padleft(left([months_yr],FindString([months_yr], "_")),2,"0")+"|202"+right([months_yr],1)
ELSE
padleft([months_yr],2,"0")+"|"+"2021"
ENDIF

 

 

Using the "FINDSTRING" function, you can get what you need out of the data.  Here is my browse output:

 

months_yrMonthYearMM|YYYY
101202101|2021
1_201202201|2022
10_210202210|2022
1111202111|2021
11_211202211|2022
1212202112|2021
12_212202212|2022

 

Cheers,

 

Mark

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
Labels
Top Solution Authors