cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
livingstonep
Level 8

format a string with leading zeros in MS Flow

we have this formula in our MS Flow - Update Item task...

 

concat(items('Apply_to_each')['T_x002d_Code']
,'-'
,items('Apply_to_each')['BoxNo']
,'-'
,
items('Apply_to_each')['ID'])

but we need to add leading zeros to the BoxNo and ID so it ends up like this:

A02-00001-000235

 

How can we format those fields like that please?

1 ACCEPTED SOLUTION

Accepted Solutions
Harry_G
Level 8

Re: format a string with leading zeros in MS Flow

Hi @livingstonep,

I have Produce 5 digits leading zero base on your box number length for your box number you can flow these steps to produced other leading zero numbers.

1.JPGSet some variables2.JPGUse Do Until loop3.JPGCOmpose and update values of your Variable to update string4.JPGLast Compose give you final value5.JPGoutput Starting Value is 16.JPGOutPut End value is 00001

   

If you find this Helpful Please Accept it as Solution.

 

Thanks,

Harry_G

View solution in original post

6 REPLIES 6
Harry_G
Level 8

Re: format a string with leading zeros in MS Flow

Hi @livingstonep,

I have Produce 5 digits leading zero base on your box number length for your box number you can flow these steps to produced other leading zero numbers.

1.JPGSet some variables2.JPGUse Do Until loop3.JPGCOmpose and update values of your Variable to update string4.JPGLast Compose give you final value5.JPGoutput Starting Value is 16.JPGOutPut End value is 00001

   

If you find this Helpful Please Accept it as Solution.

 

Thanks,

Harry_G

View solution in original post

Highlighted
livingstonep
Level 8

Re: format a string with leading zeros in MS Flow

Hi,

 

thanks so much for your truly excellent and very detailed answer

I ended up doing it in a similar way, but I used Switch Case in the Apply to each, then concatenated zeros that way using pre-defined padding variables

 

 

Hirsty2000
Level: Power Up

Re: format a string with leading zeros in MS Flow

Hi livingstoneP.

 

Would you mind sharing your solution with us.  I'm really struggling with getting this working at the moment.

 

Thank you in advance

livingstonep
Level 8

Re: format a string with leading zeros in MS Flow

Hi, we found an even better solution, and that is to use Substring and concat...

 

so we create the values we need in variables using substring and concat:

 

  1. set a variable with the maximum leading zeros: variables('PaddingForBoxNo') = '00000'
  2. PackedBoxNo = concat(variables('PaddingForBoxNo'),variables('BoxNo'))
  3. get length of PackaedBoxNo: length(variables('packedBoxNo'))
  4. set cut point for PackedBoxNo: sub(variables('lenPackedBoxNo'),variables('lenPaddingForBoxNo'))
  5. generate formatted (packed) variable: substring(variables('packedBoxNo'),variables('startCutPackedBoxNo'),variables('lenPaddingForBoxNo'))

so basically we simulate the Right function using LEN and SUBSTRING in several steps...

 

and se end up with '00024' ...

 

the only alternative is to use a Switch case, or follow the answer above...

 

HTH

Philip

Lam01007
Level: Powered On

Re: format a string with leading zeros in MS Flow

I got one more simple solution, only use 1 step:

Just put the Expression in the value of "Set Variable" step:

 

concat(substring('00000', 0, sub(5,length(variables('FormID')))), variables('FormID'))
 
Where the '00000' is pad left zero template, the length is the maximum number of digit.
And  '5' is the length of the above template
And replace 'FormID' to your variable name
Easy?
Axal
Level: Powered On

Re: format a string with leading zeros in MS Flow

@Lam01007 This trick is so cool.

 

GBU ! 😁

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

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

firstImage

Incoming: New and improved badges!

We've given our badges an overhaul and also added some brand new ones!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 135 members 5,259 guests
Please welcome our newest community members: