Power Apps: StartsWith Function

Power Apps: StartsWith Function

by: Manuel ⏱️ ✏️ Updated: 📖 4 min read 💬 0

In Power Apps, we deal with text all the time, and quite often we need to know whether a piece of text begins with something specific. Think of a search box that filters a gallery as someone types, or a quick check to see if a product code starts with a given prefix. The StartsWith function does exactly that.

It takes your text and the text you're looking for, then returns a simple true or false. That makes it a great fit for conditions, filters, and validations in your app.

Let's take a look.

Usage

It follows a simple pattern.

  1. Text: the text string you want to check.
  2. Start Text: the text string you're looking for at the start.

Here's the most basic example.

StartsWith("Hello","H")

// Returns: true

It's simple, but you should know some things so as not to get some nasty surprises.

Edge Cases

The comparison is case insensitive

StartsWith treats uppercase and lowercase as the same, so a lowercase match still returns true.

StartsWith("Hello","h")

// Returns: true
Case insensitivity

Sometimes you would want to consider "Hello" and "hello" as different strings, but this function will always treat them as the same.

Trailing spaces are not ignored

It considers the strings as is, so you should be careful with trailing spaces.

StartsWith("Hello"," Hello")

// Returns: false

To properly test, you should remove the trailing spaces. To do that you need to use the Trim function.

// You need to use the Trim function to remove the spaces
StartsWith("Hello",Trim(" Hello"))

// Returns: true

It only checks the start

Don't forget that you're only checking if the string starts with another text string. If the string is in the middle or end of the text string, then it always returns false.

StartsWith("Hello","Oh Hello There")

// Returns: false

Comparing with an empty string returns true

You need to be aware of how it behaves when compared with an empty string. If you're comparing with a variable, this issue will be hidden, but you may have invalid results.

StartsWith("Hello","")

// Returns: true

One could argue that this should not be the case, but that's the way Microsoft defined it, so you should be aware of it.

Limitations

There are a few things to keep in mind.

Delegation only works with some data sources

This function is only delegable if you're using text strings, and only in Dynamics 365, Common Data Services, and SQL Server. If you don't know what this is, please check my article on delegation and my reference table of the data sources where Filter will be delegable.

Delegation warning

When the function isn't delegable for your data source, Power Apps only evaluates the first 500 records (up to 2000 if you raise the limit). On larger data sets you'll get incomplete results with no error.

Localization

Formulas may have localization-based differences. With some regional settings the arguments are separated by a comma, but if you set your regional settings to Portugal, you should use a semicolon instead.

// Comma-based regional settings
StartsWith("Hello","H")

// Semicolon-based regional settings (for example, Portugal)
StartsWith("Hello";"H")

Recommendations

Here are some things to keep in mind.

Be careful with case

Since the comparison is case insensitive, be careful when using this function. Sometimes you would want to consider "Hello" and "hello" different strings but, in this function, they will be the same.

Trim your input

If you're using a variable or any other data source to compare, use the Trim function to remove trailing spaces.

Check the type when using variables

Always check the type of the comparison, in case you're using variables; otherwise, you may end up with errors in your Power App.

Don't nest the result

Don't try to nest because you'll get the error in the formulas since the StartsWith will return a true/false, and that's not a valid string.

Final Thoughts

The StartsWith function is a simple way to check the beginning of a text string, but a few quirks can catch you out: the comparison ignores case, trailing spaces matter, and an empty match text always returns true. Keep those in mind and it becomes a reliable little helper in your apps.

Sources

Microsoft's StartsWith function reference

Back to the Power Apps Function Reference.

Photo by Gia Oris on Unsplash

Comments

💬

No comments yet

Be the first to share your thoughts on this article!

Leave a Comment

All comments are reviewed for spam before being displayed 5000 left
Replying to