web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / format a string with l...
Power Automate
Unanswered

format a string with leading zeros in MS Flow

(0) ShareShare
ReportReport
Posted on by 358

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?

Categories:
I have the same question (1)
  • Verified answer
    Harry_G Profile Picture
    367 on at

    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

  • livingstonep Profile Picture
    358 on at

    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 Profile Picture
    2 on at

    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 Profile Picture
    358 on at

    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 Profile Picture
    231 on at

    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 Profile Picture
    945 on at

    @Lam01007 This trick is so cool.

     

    GBU ! 😁

  • Community Power Platform Member Profile Picture
    on at

    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')) ... 
  • teylyn1 Profile Picture
    362 on at

    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
  • Murderbot Profile Picture
    132 on at

    Best value!

  • kwilsson Profile Picture
    8 on at

    You can just use the "Format number" function this will return 0001 if the number 1 is inserted.
    Skärmbild 2021-10-13 102042.png

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard