Power Automate LastIndexOf Function Explained

Power Automate LastIndexOf Function

The Power Automate LastIndexOf function returns the index of the last occurrence of a substring, starting from 0, with a case-insensitive search.

In this article, I’ll introduce you to the lastIndexOf function and will provide a step-by-step guide on how to use the function in a Microsoft flow.

Power Automate LastIndexOf Function

Power Automate LastIndexOf expression

The function returns the index value of the last occurrence of a substring, starting from 0. The search is not case-sensitive.

Syntax

lastIndexOf('<text>', '<searchText>')

Input parameters

  • text (mandatory): The string within the search string should be found.
  • searchText (mandatory): The search string that should be found in the given text.

Return value

  • If the searched string is found: Returns the index value for the last occurrence of a given substring, starting from its initial position.
  • If the searched string is NOT found: Returns -1.
  • If the searched string and text are empty: Returns 0.

Power Automate LastIndexOf Expression Example

Let’s take a look at a lastIndexOf usage example. We want to find out, at which index the ‘you’ within ‘Where are you?’ starts.

The example lastIndexOf expression would look like this:

lastIndexOf('Where are you?','You')
=> 10

To get a better understanding, why the result is ten, take a look at the following table. The first line is every character in the text. The second line is the index for each character starting at 0. You see that the ‘y’ of ‘you’ starts at 10.

Whereareyou?
012345678910111213

Since you only appears once within the text, it is not a good example for the lastIndexOf function, since indexOf function would work here too.

The following example describes the difference between both functions.

lastIndexOf('abc abc','abc')
=> 4

indexOf('abc abc','abc')
=> 0
abcabc
0123456

How To Use The Power Automate LastIndexOf Function

Follow the steps to use the Power Automate LastIndexOf function in a flow.

  1. Create a new flow with trigger ‘Manually trigger a flow’

    Power Automate LastIndexOf new flow

    New flow

  2. Add a new step by clicking on ‘+ New step’

    Power Automate LastIndexOf new step

    Add a new step

  3. Search for ‘initialize’ and click on ‘Initialize variable’

    Power Automate LastIndexOf initialize variable

    Add Initialize variable action

  4. Setup Initialize variable: 1) Provide a name, 2) select type String, 3) Set value to ‘Where are you?’

    Power Automate LastIndexOf setup initialize variable

    Setup variable

  5. Add a compose step by searching for ‘compose’ and clicking on ‘Compose’

    Power Automate LastIndexOf add compose

    Add Compose action

  6. Setup compose action: 1) click into Inputs (popup appears), 2) click on ‘Expression’, 3) set expression to ‘lastIndexOf(variables(‘text’),’You’)’ 4) click on ‘OK’

    Power Automate LastIndexOf

    Setup lastIndexOf expression in Power Automate

  7. Save and do a test run

    Power Automate LastIndexOf test run

    Result test run – Power Automate lastIndexOf function example

Leave a Comment

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