Unique IDs and pad a string

gray yarn ball

Are you looking to create a unique ID in Power Automate for your Microsoft List items or files created in a SharePoint Document Library, an invoice number, file name, or sequential ID? Let me show you how to sequentially increment your unique ID based on the last File or List Item and pad or pre-fix your unique ID with leading zero’s.

List of Invoices

Above is an example Microsoft (SharePoint) List with 3 invoices and a sequential InvoiceID. The key requirement here is that previous invoice ID’s, list items or filenames, will not be deleted as we will use the last known ID as a lookup in the next action. Please also ensure that you DO NOT turn on trigger concurrency, as we must assign the ID during each unique flow run when calling get items.

Using Get Items, we are able to specify that the items are ordered by InvoiceID in descending order, we also require that only the top 1 item is returned.

This will enable us to retrieve the top, last known Invoice ID.

Then using two compose actions, we then construct the new Invoice ID. First by adding 1 to the last known ID and then by concatenating a string of 0’s to pad or pre-fix the next sequential number.

Let’s have a look at those formulas in more detail:


Get Items will always return an array and Power Automate will attempt to put you into an apply to each. Using the First() expression, we are able to return the first object. We know that we will only have one result as we have specified top 1 in the action. Once we have returned the first object of the body/value array, we specify the InvoiceId, convert that into an int() which will in turn remove any existing padding of 0’s and then add() 1 to the number. This allows us to get the next sequential number for the unique ID.


Padding with 0’s

In my example we want the unique ID to be 5 characters in length. Using the Length() expression we are able to ascertain the length of the new InvoiceID above. Note that we must convert the integer value into a string(). Then by subtracting sub() the length from 5, we know how many 0’s we must pad the string with. The length of 257 is 3 and 5-3 = 2. Using substring() of five 0’s i.e. 00000 and specifying the string to start at position 1, and using the example to 257, we will return two 0’s 00. Finally, with the concat() expression, we bring 00 together with the InvoiceID 257.

concat(substring(‘00000’, 1, sub(5, length(string(outputs(‘InvoiceID’))))),outputs(‘InvoiceID’))

Creating the Item with Unique InvoiceID

Now that we have done the hard work, we are able to pass the InvoiceID to the Create Item action and append it to the Title too. Each time the flow is run, the InvoiceID is incremented by 1 and padded with the appropriate number of 0’s.

That’s all there is to it. No need to store the InvoiceID in another table or list and as long as you don’t delete existing Invoice ID’s, Power Automate will self-manage your sequential, unique, ID.

The exact same principle applies to filenames. You can use the get files (properties only). Your unique ID must be at the start of the filename in order for this to work as it relies on ordering the files by filename and returning the top 1 in order to ascertain the last known unique sequential number.