Get Inspire insights from former attendees in our AMA discussion thread on Inspire Buzz. ACEs and other community members are on call all week to answer!

Alteryx Designer Desktop Discussions

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

convert single or double digit number to 2 digit month

jordanscraw
6 - Meteoroid

I have months listed in a column as single digits representing the month of the year as shown here below.  To be consistent with other files I am joining this data to, I need to combine these with a year column and add a leading zero only when it is a single digit month as sown in the update (i.e. "07" from "7" for July).  Can anyone help me to do this?  I've tried several methods unsuccessfully to even produce the month numbers with a leading zero, without the leading zero also on the 10-12th months (010,011,012) let alone combining the year and month fields as shown her as "Period".  Any help would be greatly appreciated.

 

CurrentUpdate
YearmonthmonthPeriod
20191012019-01
20192022019-02
20193032019-03
20194042019-04
20195052019-05
20196062019-06
20197072019-07
20198082019-08
20199092019-09
201910102019-10
201911112019-11
201912122019-12

 

2 REPLIES 2
patrick_digan
17 - Castor
17 - Castor

padleft(tostring([month]),2,"0") should add the leading 0 when necessary. then [Year]+"-" + [Month] would get your period field. If the data types aren't correct, you can use tostring and tonumber to convert between strings and numbers..

jordanscraw
6 - Meteoroid

Patrick,

 

I was finally able to come up with a solution that worked before I saw your response, but yours is definitely much cleaner and efficient.  Here is what I did that worked. 

 

IF          [month] = "10" THEN [year] + "-" = [month]

ELSEIF [month] = "11" THEN [year] + "-" = [month]

ELSEIF [month] = "12" THEN [year] + "-" = [month]

ELSE    [year] + "-0" + [month]

ENDIF

 

I tested your solution and combined it into a single formula and it worked perfectly for me.  I replaced my formula with yours.  Thank you!

 

[Year]+ "-" + padleft(tostring([month]),2,"0")

Labels