2

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

we also tried:

substring(concat('000000', variables('ItemID'))
, sub(5, length(variables('BoxNo')))
, add(6, length(variables('ItemID'))))

but with no luck.

The BoxNo value can go from 1 to 99,999 so we need to pad up to 4 leading zeros

The ItemID can go from 1 to 999,999 so we need to pad up to 5 leading zeros

How can we format those fields like that?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148

2 Answers2

4

I found another method to do this. There is the formatNumber function. Given an integer or decimal number it will format your number. If you use the '0' formatter it will replace blank spaces with zeroes.

Here is a link to the documentation: Format Number documentation

Also here is a link to custom number formats: custom-numeric-format-strings

So for example to pad with zeroes use:

formatNumber([your field],'0000000000')`

Here is an example from my power automate flow:

formatNumber(items('Apply_to_each')?['AttorneyID'],'0000000000')
Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
glennsep
  • 164
  • 3
  • 10
2

You will need to utilize a couple Expressions to handle this.

  1. Declare a String variable to store the input (e.g. named InputVar). You will need to continue using the concat() Expression here with the input value to add the maximum possible zeros required for each item (e.g. 4 for BoxNo and 5 for ItemID).

    This is obviously inefficient, but there's no better way to insert a dynamic number of characters in Flow, to my knowledge.

  2. Declare a second Integer variable to determine the length (e.g. named InputVarLength), with the following custom Expression as the value:

     length(variables('InputVar'))
    
  3. Finally, declare a third variable that will calculate a substring result (e.g. named InputVarResult). Use this custom substring Expression as the value:

     substring(variables('InputVar'),sub(variables('InputVarLength'),5),5)
    

For the ItemID or other results, you'd replace the 5s in the substring Expression with the appropriate startIndex and length to return the size you would like. For reference, the substring format is:

substring(text, startIndex, length)

Which includes the String value you want to find a substring of, the position within that string that you want to start from, and how many characters from that starting position you want to include in your substring result.

The nested subtract format is:

sub(minuend, subtrahend)

Which includes the Integer value to be subtracted from (minuend) and the Integer value that you want to subtract (subtrahend). E.g. sub(10, 2) would return an Integer value of 8.

Community
  • 1
  • 1
TylerH
  • 20,799
  • 66
  • 75
  • 101