Arrays are part of the building blocks of Power Automate , and we can get arrays from many actions, like the SharePoint “Get Items” action, for example. But if we only want the last element of the array, then we have two choices. We could check the length of the array, use the “sub” function to reduce one element to the size since the indexes start at zero, and access the element of that array, ensuring that the array has data. Or we could use the “last” function that does all of this for us. So today, let’s look at how the “last” function works.

Where to find it?

You can find the function in every action where a formula is supported. For example, let’s look at a “Compose” action:

As you can see, we can auto-complete by using the “tab” key. Let’s look at how to use it.

If you have a variable with an array, for example, here’s how to use it:

Now that we know where it is, let’s check how to use it.

Usage

It follows a simple pattern.

  1. Array to parse

Let’s start with a simple example:

createArray('Manuel','T','Gomes')
last(<array>)

will return 

'Gomes'

How about if the array is empty? To do that, let’s use a variable.

The function will not break, and it will simply return an empty result:

Finally, what will happen if we provide it with a string and not an array:

We will get the following:

last('Manuel T Gomes')

will return

's'

Consider the string an array of characters, so the “last” function will return the last element of that array or the last character. If you need to find the last character of a string, then the last function will do that for you. Notice that if the last character is a space, it will return the space and not the previous character like this:

last('Manuel T Gomes ')

will return

' '

If you want the last character without the spaces, you need to use the “trim” function first to remove the spaces and then the “last” function.

Limitations

Depending on the size of your string, your expression may return an error, even if it’s correct. Please note that the expressions have a max size of 8,192 characters. If you have an expression that is even bigger than 1000, I would strongly advise that you break it into smaller, manageable formulas.

Recommendations:

Here are some things to keep in mind.

Don’t use it to parse CSV or any other delimiter strings

Please don’t use this to break comma-separated strings. The split function is better at doing this. You can also check my “How to parse a CSV file” article and template for more details.

Don’t nest

There’s no real reason to do it, but if you find yourself in a situation where you have nested last functions in a formula, you should review it and make everything more straightforward. You end up in the same place since the last function will return only one character, and the second will return the same character since it’s the last one of the new string. If this happens, either remove all the not functions or check if one should not be there.

Always add a comment

Adding a comment will also help avoid mistakes. Please indicate why you are trying to find the element and what it means. It may look obvious initially but will not be in a few months or years. It’s essential to enable faster debugging when something goes wrong.

Sources:

Microsoft’s last Function Reference

Back to the Power Automate Function Reference.

Photo by Mike Von on Unsplash

 

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published. Required fields are marked *