cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Kudo Commander
Kudo Commander

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
Highlighted
Kudo Kingpin
Kudo Kingpin

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.

Set some variablesSet some variablesUse Do Until loopUse Do Until loopCOmpose and update values of your Variable to update stringCOmpose and update values of your Variable to update stringLast Compose give you final valueLast Compose give you final valueoutput Starting Value is 1output Starting Value is 1OutPut  End value is 00001OutPut End value is 00001

   

If you find this Helpful Please Accept it as Solution.

 

Thanks,

Harry_G

View solution in original post

8 REPLIES 8
Highlighted
Kudo Kingpin
Kudo Kingpin

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.

Set some variablesSet some variablesUse Do Until loopUse Do Until loopCOmpose and update values of your Variable to update stringCOmpose and update values of your Variable to update stringLast Compose give you final valueLast Compose give you final valueoutput Starting Value is 1output Starting Value is 1OutPut  End value is 00001OutPut End value is 00001

   

If you find this Helpful Please Accept it as Solution.

 

Thanks,

Harry_G

View solution in original post

Highlighted

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

 

 

Highlighted

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

Highlighted

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

Highlighted
Advocate II
Advocate II

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?
Highlighted

@Lam01007 This trick is so cool.

 

GBU ! 😁

Highlighted
Frequent Visitor

Just to clarify... 
First, I need to Initialize variable with name: FormID, string value: 1
...then Set variable name: FormID, ID (for me its the SP ID)
...then when I Update item, in the my running number field I use 

concat(substring('0000', 0, sub(4,length(variables('FormID')))), variables('FormID')) ... 
Highlighted

This can also be done with a simple formatNumber() function. Use the expression

 

formatNumber(variables('myVariable'),'000000')
Setup in the editorSetup in the editorresult in the run windowresult in the run window

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

Top Solution Authors
Top Kudoed Authors
Users online (11,578)