Free Trial

Alteryx Designer Desktop Discussions

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

Excel Text Function

tarichar
6 - Meteoroid

What formula could I use to convert a string to a text field with 4 characters? In Excel, I use a simple Text() formula: =text(A1,"0000") where the following values would convert if...

  • A1 equals 123 and the output adds a leading '0' to '0123'
  • A1 equals 001234 and the output removes the leading '0' to '1234'
6 REPLIES 6
cplewis90
13 - Pulsar
13 - Pulsar

Hello @tarichar,

 

You would have to do an if statement to check the length and run different formulas depending on what is there. It would look something like the below:

 

If length([field])>4

then right([field], 4)

else padleft([field],4,"0")

endif

 

 

tarichar
6 - Meteoroid

Thanks for the feedback.

 

What if i also have values that are greater than 4 characters that I need to stay the same? Example, 123456 needs to remain 123456, not convert to '3456' (using the above if statement). 

cplewis90
13 - Pulsar
13 - Pulsar

The if statement would just need to change slightly

 

If length([field])>4

then trimleft([field],"0")

else padleft([field],4,"0")

endif

tarichar
6 - Meteoroid

Getting closer, but with a starting value of 000123, the output value would be 123 instead of 0123.

 

Sorry for all the scenarios, I just keep running into different issues during each data run. 

cplewis90
13 - Pulsar
13 - Pulsar

One additional modification:

 

If length([field])>4

then padleft(trimleft([field],"0"),4,"0")

else padleft([field],4,"0")

endif

 

Let me know if this works. If not I can try something else.

tarichar
6 - Meteoroid

Nailed it, thanks!

Labels
Top Solution Authors