Skip to main content

Notifications

Community site session details

Community site session details

Session Id : CRj3aPaHXklcK7fuACWF1W
Power Automate - Building Flows
Answered

format a string with leading zeros in MS Flow

Like (0) ShareShare
ReportReport
Posted on 1 Feb 2019 10:46:30 by 352

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?

  • Piper Profile Picture
    177 on 13 May 2024 at 16:27:31
    Re: format a string with leading zeros in MS Flow

    This helped me 4 years later. I was less than thrilled with my case statement and this worked perfectly.

    Thanks!

  • hung1993 Profile Picture
    2 on 19 May 2022 at 12:19:14
    Re: format a string with leading zeros in MS Flow

    You can use format number

     

    hung1993_0-1652962725322.pnghung1993_1-1652962741243.png

     

  • Robyn_D Profile Picture
    2 on 28 Apr 2022 at 03:39:37
    Re: format a string with leading zeros in MS Flow

    You can also use the formatNumber function - 

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

     

  • kwilsson Profile Picture
    8 on 13 Oct 2021 at 08:23:11
    Re: format a string with leading zeros in MS Flow

    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

  • Murderbot Profile Picture
    132 on 24 Sep 2021 at 22:50:04
    Re: format a string with leading zeros in MS Flow

    Best value!

  • teylyn1 Profile Picture
    358 on 03 Jun 2020 at 04:54:34
    Re: format a string with leading zeros in MS Flow

    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
  • Community Power Platform Member Profile Picture
    on 26 May 2020 at 10:19:27
    Re: format a string with leading zeros in MS Flow

    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')) ... 
  • Axal Profile Picture
    945 on 13 Nov 2019 at 09:33:25
    Re: format a string with leading zeros in MS Flow

    @Lam01007 This trick is so cool.

     

    GBU ! 😁

  • Lam01007 Profile Picture
    231 on 06 Nov 2019 at 08:44:27
    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?
  • livingstonep Profile Picture
    352 on 14 Feb 2019 at 21:26:57
    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

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,679 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,015 Most Valuable Professional

Leaderboard