November 6, 2024

Power Automate’s endsWith function is part of a set of functions that deal with strings. In this case we’re checking if a string ends with “something” or not. The endsWith function will always return with true or false.

So let’s see how to use it.

Usage

It follows a simple pattern.

  1. Text
  2. Text to search

Let’s start with a simple example:

endsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - 'Gomes'

will return 

true

It makes sense since it’s precisely the same string. But let’s do some variations to see what we get. First, the comparison string in a different case:

endsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - 'gomes'

will return 

true

So we can already see that the comparison is case-insensitive. To prove the point.

endsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - 'gOmEs'

will return 

true

Now let’s add spaces:

endsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes' - compare with - ' Gomes '

will return 

false

Spaces are considered as a character, so the string is not the same. Be careful with this. The trim function can help but always understand what you’re trimming.

Let’s add some numbers:

endsWith(variables('TEXT'),variables('TEXT_TO_COMPARE'))
'Manuel T. Gomes1' - compare with - '1'

will return 

true

In this case, we’re still comparing strings, but how about if we have a variable with a number?

endsWith(variables('TEXT'),variables('NUMERIC_VARIABLE'))

will return an error

Power Automate will consider the expression as correct, but it will return the following error when it runs:

Unable to process template language expressions in action 'Test_with_number' inputs at line '1' and column '4721': 'The template language function 'endsWith' expects its second parameter to be of type string. The provided value is of type 'Integer'. Please see https://aka.ms/logicexpressions#endswith for usage details.'.

The definition is clear, so be aware that there’s no conversion of the datatypes. To “solve” this, you can use the string function that converts the number into the correct datatype. Here’s an example:

endsWith(variables('TEXT'), string(variables('NUMERIC_VARIABLE')))

'Manuel T. Gomes1' - compare with - 1

will return 

true
Converting datatypes is dangerous so please be careful. I recommend that you always use the defined type in the function and don’t convert it to avoid the unforeseen result.

That’s it. The function is quite simple and super useful.

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 breaking it into smaller, manageable formulas.

As mentioned before, please don’t forget that the function requires two strings. It would be best if you did the conversions before calling the function. Power Automate won’t return an error when you’re building the Flow, but it will when it’s running.

Recommendations:

Here are some things to keep in mind.

Use “debug” compose actions.

Since the comparison will return true or false, sometimes it’s tricky to understand how the calculation is done, depending on how complex the expression is. So I recommend using Compose actions to have the values that go “in” the function. This way, if the value doesn’t make sense you can understand, based on the parameters, why it was calculated that way.

Don’t nest

There’s no real reason to do it. Furthermore, if you try to nest “endsWith” function, you’ll receive an error since boolean values are not comparable. So be very careful to avoid breaking Flows.

Sources:

Microsoft’s endsWith Function Reference

Back to the Power Automate Function Reference.

Photo by Annie Spratt 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 *

Mastodon