Power Automate Slice Function Explained

Power Automate Slice Function

The Power Automate Slice function lets you extract a substring of a given string by defining a start and end index within a given string.

The slice function is very similar to the substring function, which does basically the same by providing a start index and a length of the needed substring.

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

Power Automate Slice Function

power automate slice

Extract a portion of a string by specifying the starting and ending point or value.

Syntax

slice('<text>', <startIndex>, <endIndex>?)

Input parameters

  • text (mandatory): The input string that holds the substring to be searched for.
  • startIndex (mandatory): The input is an integer that represents the starting point for searching for the substring. If the provided index is greater than the length of the string, an empty string will be returned. If the index is negative, the search will begin at the calculated position by adding the length of the string with the provided negative index.
  • endIndex (optional):: The input is an integer that represents the ending point for searching for the substring. The character at this index will not be included in the search. If the provided index is not specified or greater than the length of the string, the search will go up to the end of the string. If the index is negative, the search will end at the calculated position by adding the length of the string with the provided negative index.

Return value

  • Retrieve a new string containing the identified substring.

Power Automate Slice Expression Example

Let’s imagine we have the following string, and we want to slice ‘John’ out of the string.

"I am John Doe."

The index starts at 0 the J is at position 5 and the endIndex is the first character after the searched string, so it must be the 9.

IamJohnDoe.
012345678910111213

The Power Automate slice expression must look like this, to retrieve the “John” slice.

slice('I am John Doe.',5,9)
=> "John"

How To Use The Power Automate Slice Function

Now that we have a basic understanding of the function. Let’s take a look at the slice function in flow.

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

    power automate slice new flow

    New flow

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

    power automate slice new step

    Add a new step

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

    power automate slice add initialize variable action

    Add Initialize variable action

  4. Configure Initialize variable action: 1) Provide a name,2) select ‘string’, 3) Set the value to ‘I am John Doe.’

    power automate slice initialize variable configuration

    Initialize variable action

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

    power automate slice add compose step

    Add a compose action

  6. Configure compose: 1) Click into Inputs text field (popup appears), 2) click on ‘Expression’, 3) set expression to ‘slice(variables(‘myText’),5,9)’, 4) click ‘OK’

    power automate slice expression

    Power Automate slice expression example

  7. Save and do a test run

    power automate slice test run result

    Result Test run – Power Automate slice function

Leave a Comment

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