Power Automate IndexOf() Function | Power Automate lastIndexOf()

Do you want to know how to get a character position or string position in Power Automate? In this tutorial, I have explained how to do this by using the Power Automate indexof function.

I will show you the syntax of IndexOf() function in Power Automate, Power Automate LastindexOf Function with various examples.

Power Automate IndexOf() Function

Power Automate indexOf() function returns the position of the first occurrence of a string, from a given string. IndexOf() function returns the first true match of a given string. This function also includes spaces as a count position.

This function is essential to find the position of a string or a character from the provided string value. The indexOf() function consists of two components such as the lastIndexOf() and nthIndexOf().

IndexOf() Function Syntax

This is the syntax for the indexOf() function that can be used inside a Power Automate flow.

indexOf(['text'],'Search text ')

Note: The first character on the position of ‘0’ not starts with ‘1’.

Example: indexOf([‘Joni Sherman’], ‘Sherman’) => Index position for ‘Sherman’ is ‘5’. If the search text is ‘Joni’, then the index position is ‘0’.

The return value is an integer where the search text will appear for the first time in a given text.

Note: If the serach text not found in the given text, then it will return the output as [-1].

IndexOf() Function Examples in Power Automate

Let’s dive into the indexOf() function by implementing it in a Power Automate flow.

So, now, I will explain how to implement the indexOf() function in a Power Automate ‘Instant cloud flow’ that will trigger manually.

Follow the below-mentioned steps to create a flow that will work with the indexOf() function:

Example:-1

1. Navigate to make.powerautomate.com in the browser. In the Power Automate Home page, click on “+Create” -> select “Instant cloud flow” -> set Flow name -> choose “Manually trigger a flow” -> click on Create.

  • In the trigger add a Text input box to provide the text manually.
power automate indexof

2. Then, add an ‘Initialize variable’ flow action to provide the Name, Type, and Value for the variable.

power automate indexof function

3. Next, add the ‘Compose’ data operation to return the output of the indexOf() function inside a flow.

  • Inputs: Add the below-provided expression and replace the text value.
indexOf(variables('Name'),'Sherman')
power automate get index of string

4. Now, it is time to save and test the flow. Click on Save and select Test. Test it Manually.

indexOf() function in Power Automate

5. Finally, when the flow runs successfully, the outputs of the compose flow action will display returns of the indexOf() function for the given string as shown below.

  • In the Run flow box, I have given the text as ‘Joni Sherman’.
power automate find position in string

This is how to use the indexOf() string function in a Power Automate flow.

Example:-2

Now, I will discuss another example of a Power Automate string function i.e., indexOf() function, by using a SharePoint list.

Recently, When I was working on a SharePoint list, I got a requirement from one of our clients to find a character index or a string position of a text column value.

For that, I have used a SharePoint list named ‘HR Policies’ with the columns as presented in the table below.

Column NameDatatype
Employee NameIt is a single line of text – Title column
Policy AppliedSingle line of text
Started DateA single line of text
power automate find position in string

In the SharePoint list mentioned above, we need to find out the character index or string position based on the list column i.e., Policy Applied.

To implement it, let’s create a Power Automate ‘Automated cloud flow’ that will trigger automatically.

Follow the below steps to create the flow:

1. Open a Power Automate home page, click on +Create -> Select “Automated cloud flow” -> Provide flow name, choose a trigger ”When an item is created” -> Click on Create. Set details like Site Address and List Name.

power automate indexof example

2. After that, add an ‘Initialize variable’ flow action to store the values of a text column. Give the details for a variable.

  • Name: Give a name for an initialized variable.
  • Type: Select datatype as ‘String’ from drop-down.
  • Value: Add the list column from the dynamic content.
power automate find index of string

3. To see the results of the indexOf() function, add the ‘Compose’ flow action and provide the below-given expression in the ‘Inputs’ section.

indexOf(variables('Applied Policy'),'Policy')
indexOf() in Power Automate flow

4. At this point, save and test the flow manually. Click on Save and Test.

How to use the Power Automate indexOf() function

5. Then, the outputs of the compose data operation will show the index number for a ‘Policy’ in the provided string, as shown in the figure below:

Power Automate indexOf() function

This is how to use the indexOf() string function in a Power Automate flow to get index of a string.

Power Automate LastindexOf Function

Power Automate LastindexOf Function will return the index of the last occurrence of a string from a given string. This function also includes spaces as a count position. And the output of the lastindexOf() will always return as ‘Number’.

This is one of the Power Automate string functions included under the indexOf() function. It will be useful to find out the index of those characters or string that comes repeatedly.

The index of a character will be started at '0'. If the character or a text is not found, then it will return the output as '-1'.

LastindexOf() Function Syntax

The syntax of the lastindexOf() function in Power Automate is:

lastindexOf(['text'],'Search text ')

Example: lastIndexOf(Power Platform has four major components Power BI, Power Apps, Power Automate and Power Virtual Agents),’Power’) => It will return the last index of power, i.e. (82).

lastindexOf(Henrietta Muller),’e’) => Return the output as ’14’ because in the given text i.e., ‘Henrietta Muller’ lastindexOf() function finds out the last repeated character position of ‘e’.

lastIndexof Power Automate Examples

Here, I will explain how to use the lastindexOf() function in Power Automate by implementing it in a manual trigger flow of Power Automate.

Follow the below-mentioned steps to create a flow that will work with the lastindexOf() function:

Example:-1

1. Navigate to make.powerautomate.com in the browser. In the Power Automate Home page, click on “+Create” -> select “Instant cloud flow” ->In the next window, set Flow name -> choose “Manually trigger a flow” -> click on Create.

  • Add a text input box in the trigger to provide the text manually.
Power Automate lastindexOf() function

2. Then, add an ‘Initialize variable’ flow action to store the value of a text during the flow. Set Name, Type, and Value for a variable.

PowerAutomate functions lastindexOf() function

3. In the next step, add a ‘Compose’ data operation to give the expression of lastindexOf() function. Add the details:

  • Inputs: Provide the below given expression here.
lastIndexOf(variables('Name'),'e')
Power Automate flow lastindexOf() function

4. Now, the flow has been created. Click on Save and Test. Test the flow Manually.

lastindexOf() string function in Power Automate

5. In the Run flow window, I was given the text input (Henrietta Muller) -> Click on the ‘Run flow’ button as shown below.

Power Automate functions-lastindexOf() function explained

5. Now, the outputs of a compose data operation will return the last index of a character or a text from the provided text.

lastindexOf() function in Power Automate

This is how to use the lastindexOf() string function in a Power Automate flow.

Example:-2

Here, I will explain one more example of the Power Automate lastindexOf() function based on a SharePoint list.

Recently, when I was working with a SharePoint list, I got a task to find the last index of a text or character based on a SharePoint list column using Power Automate flow.

To do that, I have created a SharePoint list named ‘HR Policies’ with different columns, as represented in the table below.

Column NameDatatype
Employee NameIt is a Single line of text – Title column
Policy AppliedSingle line of text column
Policy DescriptionSingle line of text column
What is lastindexOf() function in Power Automate

From the above given SharePoint list, I need to find out the last index of a particular string from the given text based on the list column i.e., Policy Description.

To achieve this, let’s build an ‘Automated cloud flow’ in Power Automate that will trigger when an item is created in a SharePoint list.

Follow the below steps:

1. For creating an ‘Automated cloud flow’, follow: Browse https://make.powerautomate.com -> In the Power Automate Home page, click +Create -> Select ‘Automated cloud flow’.

In the next window, set the Flow name, choose ‘When an item is created’ trigger -> Click Create. Provide the below details inside the trigger.

  • Site Address: Select a SharePoint site from the drop-down.
  • List Name: Choose a specific SharePoint list from the drop-down.
How to use the Power Automate lastindexOf() function

2. Under the trigger, add an ‘Initialize variable’ flow action to store the values of a list column during the flow. Set the details for a variable as below:

  • Name: Give the name manually.
  • Type: Select datatype from the drop-down.
  • Value: Take value from the dynamic content.
PowerAutomate expressions lastindexOf() function

3. Then, add a ‘Compose’ data operation to include the lastindexOf() string expression along with the variable and search text.

  • Inputs: In the inputs, add the below given expression placed in the code.
lastIndexOf(variables('Description'),'Policy')
Using lastindexOf() function in Power Automate

4. It is the time for the flow to save and test it. So, click on Save and Test the flow -> Test the flow Manually.

Power Automate string functions lastindexOf()

5. When the flow runs, add an item to the SharePoint list. If there are no errors, the flow runs successfully.

Open the compose flow action to display the last index of a search text from the given text like the screenshot below:

String Function lastindexOf() in Power Automate

This is how to use a Power Automate lastindexOf() function, to find out the last index of a character, text, or string based on a SharePoint list column.

Conclusion

When finding a character index or a string position from a text string, a user can use the indexOf() function in Power Automate. I hope from this tutorial, you understood how to use the indexOf() function in Power Automate. I have also shown you two Power Automate indexof examples.

You can also check: