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

nthIndexOf Function in Power Automate: 10 Real-time Examples

No comments

Loading

In this “nthIndexOf Function in Power Automate” article, we will learn about what an nthIndexOf Function is in Power Automate and how to use it with real-time examples. If you are new to this index of functionality, you may refer to the previous articles: IndexOf Function in Power Automate: 10 Real-time Examples with SharePoint Online and LastIndexOf Function in Power Automate With 10 Real-Time Examples.

Power Automate, a powerful workflow automation tool by Microsoft, offers a multitude of functions to empower users in creating intricate workflows. One such indispensable function is nthIndexOf, which allows users to find the position of the nth occurrence of a specified value within a string or an array. In this article, we will dive into the nthIndexOf function and provide 10 real-time examples demonstrating its usage and versatility.

Understanding the nthIndexOf Function in Power Automate

The nthIndexOf function helps in finding the position of the nth occurrence of a specified value within a string or an array. Its syntax is straightforward and efficient.

Syntax:

Syntax of nthIndexOf Function in Power Automate
Syntax of nthIndexOf Function in Power Automate
nthIndexOf(expression, value, nth)
  • Expression: The input string or array where the search will be performed.
  • Value: The value or substring to search for within the expression.
  • Nth: The desired occurrence of the value within the expression.

Sample Example:

nthIndexOf Function in Power Automate: Real-time Examples

We will explore various scenarios to showcase the nthIndexOf function in action. Each example follows the format: first, the syntax of the function expression, then the actual example with passing values, and finally the output of the expression.

Example: Finding the Position of the 2nd Occurrence of a Substring

Let’s find the second position of this word ‘power’ from this sentence: ‘Power Automate is powerful and flexible.’. To achieve that, use the below expression:

Expression: nthIndexOf('Power Automate is powerful and flexible.', 'power', 2)

nthIndexOf Function in Power Automate - 2nd Occurrence of a Substring

nthIndexOf Function in Power Automate – 2nd Occurrence of a SubstringOutput:

The output is: 18

nthIndexOf Function in Power Automate - 2nd Occurrence of a Substring - Output
nthIndexOf Function in Power Automate – 2nd Occurrence of a Substring – Output

Example: Locating the 3rd Occurrence of a Character

To locate the third occurrence of this character ‘o’ from this sentence ‘Hello, world! How are you?’, use the below expression:

nthIndexOf('Hello, world! How are you?', 'o', 3)
nthIndexOf Function in Power Automate - 3rd Occurrence of a Character
nthIndexOf Function in Power Automate – 3rd Occurrence of a Character

Output:

The output is: 5

nthIndexOf Function in Power Automate - 3rd Occurrence of a Character - Output
nthIndexOf Function in Power Automate – 3rd Occurrence of a Character – Output

Example: Identifying the 2nd Index of a SharePoint List Item

To identify the second index of a SharePoint List Item or a document from the Document Library Item, use the below expression, which uses the “apply to each” loop:

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

Note:

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

To check the 4th specific text in SharePoint list item, use the below expression, which uses the “apply to each” loop:

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

Example: Locating the 1st Occurrence of a Character in a File Name

To locate the first occurrence of the character dot (.) in the file name, use the below expression, which uses the “apply to each” loop:

nthIndexOf(items('Apply_to_each')?['{Name}'],'.',1)

Example: Finding the Position of the 2nd Occurrence of a Word in a Sentence

To find the position of the second occurrence of a word in a sentence, use the below expression:

nthIndexOf(‘The quick brown fox jumps over the lazy dog.’, ‘fox’, 2)

Example: Identifying the 1st Index of a Specific Object in an Array

To identify 1st index of a specific object in an array, use the below expression:

Expression: nthIndexOf(variables('objectArray'), 'targetValue', 1)

Example: Searching for a Value in a CSV String

To search for a specific value in a CSV string, use the below expression:

Expression: nthIndexOf('value1,value2,value3,value4', 'value3', 1)

Example: Locating the 2nd Occurrence of a Value in an Array

To locate the second occurrence of a value in an array, use the below expression:

Expression: nthIndexOf(variables('stringArray'), 'searchValue', 2)

These examples clearly explain the power and versatility of the nthIndexOf function in Power Automate. By using this function effectively, you can pinpoint specific occurrences within strings or arrays, facilitating more efficient Power Automate flow designs.

Example: Extract the specific position of the forward slash (“/”) from an URL

Let’s say you have the below URL:

 https://abcde.sharepoint.com/Lists/EmployeeList/AllItems.aspx

If, from this URL, you want to extract the position of the fourth position of the forward slash (“/“), then you can use the below expression:

nthIndexOf('https://abcde.sharepoint.com/Lists/EmployeeList/AllItems.aspx','/',4)

Output:

This will give an output of 34. You can use this index number for further logic in your flow while dealing with dynamic URLs to extract substrings from a 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 nthIndexOf 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.

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

Best Practices for Utilizing the Power Automate nthIndexOf Function

When using the nthIndexOf function in Power Automate expressions, it’s essential to follow best practices to ensure efficient and effective workflow development. Here are some best practices for using the nthIndexOf function:

Understand the Function and Its Purpose:

Gain a clear understanding of what the nthIndexOf function does and when to use it. It’s crucial to comprehend that it finds the position of the nth occurrence of a specified value within a string or array.

Validate Input Parameters:

Ensure that the input expression, value, and nth occurrence are of the expected data types and meet the requirements of the nthIndexOf function.

Handle Not Found Scenarios:

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

Parameterization for Reusability:

Parameterize the value being searched for and the nth occurrence to allow for flexibility and reusability of the Power Automate flow.

Error Handling:

Implement proper error handling to manage scenarios where the value is not found or when there are unexpected errors during the search process.

Consistent and Meaningful Variable Names:

Use descriptive and consistent variable names for the expression, value, and nth occurrence to enhance code readability and understanding.

Commenting and Documentation:

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

Testing and Debugging:

  • Thoroughly test the nthIndexOf 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 nthIndexOf.

Performance Optimization:

Be mindful of the performance implications, especially when working with large strings or arrays. Optimize the usage of nthIndexOf 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 nthIndexOf function.
  • Implement regular backups of your workflows, especially when making significant changes involving the nthIndexOf function.

Use of Variables:

Do not pass the string value directly inside the nthIndexOf function; rather, create a variable, assign the value there, and use it in your nthIndexof 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 nthIndexOf function in Power Automate expressions, ensuring the reliability and maintainability of your workflows.

Summary: Power Automate nthIndexOf function Expressions

Thus, in this “Power Automate nthIndexOf function” article, we have learned about the nthIndexOf 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 nthIndexOf 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