cancel
Showing results for
Did you mean:
Level: Powered On

I'm wondering if someone has come across a solution to adding leading zeros to a number.
The idea is that I have an identifying number that is always 5 digits so the output I would like to see is:

• 00001 (original number 1)
• 00022 (original number 22)
• 00333 (original number 333)
• 04444 (original number 4444)

I can add leading zeros no problem with concat so if I add four zeros to the start (0000) the output I get is

• 00001
• 000022
• 0000333
• 00004444

How can I extract the substring from the right to a lenght of 5?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User

Here is a Flow that runs successfully that I believe demonstrates what you are trying to accomplish.

Here is the formula that I am using for varLength:

length(variables('varStart'))

Here is the expression that I'm using for varResult:

substring(variables('varStart'),sub(variables('varLength'),5),5)

This solution should work regardless of how long you make the initial string.

Scott

If you like my response, please give it a Thumbs Up.

Scott
6 REPLIES 6
Highlighted
Super User

Here is a Flow that runs successfully that I believe demonstrates what you are trying to accomplish.

Here is the formula that I am using for varLength:

length(variables('varStart'))

Here is the expression that I'm using for varResult:

substring(variables('varStart'),sub(variables('varLength'),5),5)

This solution should work regardless of how long you make the initial string.

Scott

If you like my response, please give it a Thumbs Up.

Scott
Anonymous
Not applicable

A couple quick thoughts...

Are you actually usings strings? I ask since you are using concat to pad zero's to the left.

One idea would be to only pad on the number of zero's you need?  You could do this by getting the lenght of the current string, then only concat w/ the number of zero's required.  That would negate the need for your question.

However, to answer your actual ask, could you not use the lenght of the string minus 5 for the "start" parameter of substring, and then you would get the last 5?  Too bad there isn't a left() or right() parallel in flow just yet.

Level: Powered On

Thank you for the quick reply. I gave it a go and its working perfectly. Appreciate the help!!

Level: Powered On

@Anonymous

Thanks for the repy! Interesting idea to find the length and then add the appropriate number of zeros. Would have been my backup plan if I couldn't get it done a more traditional way.

If anyone else is reading along I added a Uservoice to see if Left() and Right() can be added to the expressions list.

Have the expressions for Left() and Right()

Anonymous
Not applicable

Hey, in my world, if it aint broke, don't recode it! Thanks for reading it anyway!

I voted your left/right UV up as that would be nice, and surely isn't a huge stretch for them functionally.

Flow On!

Level: Powered On

I agree: either a right(TEXT,LENGTH)-function or the option to make the second parameter to substring() negative would be so much more elegant.

Announcements

#### Incoming: New and improved badges!

Look out for new contribution recognition badges coming SOON!

#### New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

#### Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

#### Microsoft Learn

Learn how to build the business apps that you need.

#### Power Platform World Tour

Find out where you can attend!

#### Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Solution Authors
Top Kudoed Authors
Users online (4,789)