lastIndexOf Function in Power Automate with Bunch of Real-Time Examples

LastIndexOf Function in Power Automate With 10 Real-Time Examples

No comments

Loading

In this “LastIndexOf Function in Power Automate” article, we will learn about what the lastIndexOf function is in Power Automate and how to work with the lastIndexOf function in Power Automate with real-time examples. In the previous article, we learned about the indexOf function in Power Automate. You may read this article before you get into this last indexOf function article.

Power Automate, formerly known as Microsoft Flow, is a versatile automation tool that enables users to create workflows and automate a variety of tasks. One important function within Power Automate is the lastIndexOf function, which helps in finding the position of the last occurrence of a specified value within a string or an array. In this article, we will delve into the lastIndexOf function and provide 10 real-time examples of how to utilize this function using SharePoint Online as the data source.

Understanding the lastIndexOf Function

The lastIndexOf function is utilized to find the position of the last occurrence of a specified value within a string or an array. It returns the index of the last occurrence of the value within the given data.

Syntax:

lastIndexOf(expression, value)
  • Expression: The input string or array where the search will be performed.
  • Value: The value or substring to search for within the expression.
Power Automate lastIndexOf Function Syntax
Power Automate lastIndexOf Function Syntax

Sample Example

Let’s say we have this string  ‘This is a sample string; this sample is used for the last index of the demo.’ where we want to find the last occurrence of the word ‘sample’, then we can write the lasIndexOf function as below:

lastIndexOf('This is a sample string; this sample is used for the last index of the demo.','sample')

Output:

The output is 30

LastIndexOf Function in Power Automate: Examples with SharePoint Online

We will demonstrate various scenarios where the lastIndexOf function can be used with SharePoint Online as the data source.

Example: Finding the Last Position of a Substring in a Text Field

To find the last position of the word ‘important‘ within a ‘Remark‘ field in a SharePoint list or Document Library:

lastIndexOf(items('Apply_to_each')?['Remark'],'important')

Output:

This will give the position of the last occurrence of the ‘important‘ word in the ‘Remark‘ column.

 

Example: Checking for the Last Specific Text in a SharePoint List Item

To check for the last occurrence of a specific keyword in the ‘Title‘ field of a SharePoint list item:

Expression: lastIndexOf(items('Apply_to_each')['Title'], 'keyword')

Output:

This will give the position of the last occurrence of the ‘keyword‘ word in the ‘Title‘ column. The example uses the “apply to each” loop of Power Automate.

Example: Checking if a String Contains a Substring

To verify if a string contains a specific substring and find the last position of it:

Expression: lastIndexOf('This is a sample string', 'sample')

Output:

The above expression will find the last position of  the ‘sample‘ word found in the given string.

Example: Locating the Last Occurrence of a Character

To find the position of the last occurrence of a character in a string, use the below expression:

Expression: lastIndexOf('Hello, World!', ',')

Example: Searching for a Value in an Array

To find the last position of a specific value within an array of strings, use the below expression:

Expression: lastIndexOf(variables('stringArray'), 'searchValue')

Example: Identifying the Last Index of a SharePoint List Item

To get the index of the last occurrence of a SharePoint list item being processed in a loop, use the below expression:

Expression: lastIndexOf(outputs('Compose'), items('Apply_to_each')['ID'])

Example: Identifying the Last Index of a forward slash from URL

The lastIndexOf or indexOf function plays a vital role while you work with SharePoint URLs or any URLs; it will help to extract certain text from the URL after a specific position, and this position will be input in your substring function. Let’s look at the below example:

Expression: lastIndexOf('https://csmcs.sharepoint.com/Lists/EmployeeList/AllItems.aspx','/')

Output:

The expression will return output as 47; this checks the last occurrence of the forward slash ‘/’ in the given URL.

Notes:

  • In all the above examples, if the expression does not find the searching value from the given input text, it will return -1.
  • The lastIndexOf of the expression is case-insensitive, so it does not matter if it is upper or lower case in the string; it will consider it.

Best Practices for Utilizing the Power Automate LastIndexOf Function

When utilizing the lastIndexOf function in Power Automate expressions, it’s important to follow best practices to ensure efficient, reliable, and maintainable workflows. Here are some best practices for using the lastIndexOf function:

Clear Understanding of Inputs and Outputs:

  • Input Expression: Ensure a clear understanding of the input expression and the value to be searched for using lastIndexOf.
  • Output: Understand that the output of lastIndexOf is the position of the last occurrence of the specified value within the given expression.

Input Validation and Sanitization:

  • Validate the input expression and value to ensure they are of the correct data types (string or array) that lastIndexOf expects.
  • Sanitize inputs to handle edge cases, such as empty or null values.

Error Handling:

Implement error handling to manage scenarios where the value is not found (e.g., -1 for not found) or when there are unexpected errors during the search process.

Handle Not Found Scenarios:

Understand that lastIndexOf returns -1 when the value is not found in the expression. Plan your workflow to handle this scenario appropriately.

Consistent and Meaningful Variable Names:

Use descriptive and consistent variable names for the expression and the value being searched for to enhance code readability and understanding.

Commenting and Documentation:

Add comments to explain the purpose and usage of the lastIndexOf function for future reference and collaboration.
Document the expected behavior and possible outputs of the lastIndexOf function in the specific context it is used.

Parameterization for Reusability:

Parameterize the value being searched for to allow for flexibility and reusability of the workflow.

Testing and Debugging:

  • Thoroughly test the lastIndexOf function with various scenarios, including edge cases, to ensure it behaves as expected.
  • Utilize debugging tools to troubleshoot and debug any issues related to the usage of lastIndexOf.

Performance Optimization:

Be mindful of the performance implications, especially when working with large strings or arrays. Optimize the usage of lastIndexOf to minimize unnecessary operations.

Version Control and Backup:

  • Use version control practices to keep track of changes made to the Power Automate flow involving the lastIndexOf function.
  • Implement regular backups of your Power Automate flows, especially when making significant changes involving the lastIndexOf function.

Use of Variables:

Do not pass the string value directly inside the lastIndexOf function; rather, create a variable, assign the value there, and use it in your lastIndexof function. For example, if you want to directly pass the string value inside the index of the function, you need to single quote your string, like ‘My test string’, but if you use it using a variable, then it is not needed.

Keep the string short:

Try to keep the string value as short as possible. In your complex logic, try to make the string as short as possible; otherwise, it will break or fail.

By adhering to these best practices, you can effectively use the lastIndexOf function in Power Automate expressions, ensuring the reliability and maintainability of your workflows.

The lastindexOf function in Power Automate handles strings in caseinsensitive ways, which means ‘MY TEST STRING’ and “my test string” are both valid.

Summary: Power Automate lastIndexOf function Expressions

Thus, in this “Power Automate lastIndexOf function”article, we have learned about the lastIndexOf function in Power Automate along with some real-time examples with SharePoint Online data, and we have also learned about the best practices we should follow while working with the lastIndexOf function in Power Automate.

See Also: Power Platform Articles

You may also visit the Power Platform article hub, where you will see a bunch of articles focusing on Power Platform, like Power Automate, Power Apps, etc. All the articles are written with real-time project scenarios and troubleshooting techniques.

About Post Author

Do you have a better solution or question on this topic? Please leave a comment