Mastering Power Automate: The createArray() Function Explained

Power Automate offers powerful data manipulation capabilities, enabling users to work with collections of data efficiently. A cornerstone of this is the createArray() function, which allows multiple values to be stored and processed as a single array. This is invaluable when dealing with SharePoint lists, Dataverse records, Excel rows, API responses, email attachments, and dynamic content. Instead of managing individual values separately, arrays enable you to process multiple items collectively.

The createArray() function is designed to create an array containing multiple elements within a single expression. In this article, we will explore how to effectively create and utilize array functions in Power Automate.

What is an Array?

An array is essentially a collection of values stored within a single variable. For example, instead of having:

You can represent this as a single array:

[
  "Employee1",
  "Employee2",
  "Employee3",
  "Employee4"
]

Arrays help organize and process multiple values much more efficiently.

Why Use Arrays?

Arrays are particularly useful in various common business scenarios:

The benefits of using arrays include:

Business Scenario Example

Imagine you need to send an email to multiple users. Instead of managing individual email addresses, you can create a single array containing all the emails. This array can then be used in a loop to send notifications to each user.

Understanding createArray()

The syntax for the createArray() function is straightforward:

createArray(
  value1,
  value2,
  value3
  // ... more values
)

For instance:

createArray(
  'John',
  'David',
  'Smith'
)

This expression will output the following array:

[
  "John",
  "David",
  "Smith"
]

The createArray() function allows multiple values to be combined into a single, manageable array.

Step-by-Step Guide to Creating an Array in Power Automate

Step 1: Create an Instant Flow

  1. Navigate to make.powerautomate.com.
  2. Click on Create.
  3. Select Instant cloud flow.
  4. Choose Manually trigger a flow as the trigger.

Step 2: Add a Compose Action

  1. Add a new action and search for Compose.
  2. In the Inputs field, enter the createArray() expression. For example:
    createArray(
          'Employee1',
          'Employee2',
          'Employee3'
        )

The output of this action will be:

[
  "Employee1",
  "Employee2",
  "Employee3"
]

Step 3: Test the Flow

  1. Save your flow.
  2. Click Test and then Manually.
  3. Run the flow.

You should see the output confirming that the array was created successfully.

Examples of Using createArray()

Example 1: Create an Email Array

This is commonly used for sending email notifications to multiple recipients.

createArray(
  'user1@company.com',
  'user2@company.com',
  'user3@company.com'
)

Output:

[
  "user1@company.com",
  "user2@company.com",
  "user3@company.com"
]

Example 2: Create a Number Array

Useful for calculations and reporting purposes.

createArray(
  100,
  200,
  300,
  400
)

Output:

[
  100,
  200,
  300,
  400
]

Example 3: Create an Array from Variables

This approach is commonly used when combining dynamic values from different variables.

First, initialize two variables:

Then, use the createArray() function:

createArray(
  variables('Person1'),
  variables('Person2')
)

Output:

[
  "Alice",
  "Bob"
]

Example 4: Create an Array of Arrays (Nested Array)

You can create nested arrays using createArray(), which can be useful for complex data structures.

Initialize two array variables:

Expression:

createArray(
  variables('Questions'),
  variables('Answers')
)

Result:

[
  [1,2,3,4,5],
  [2,4,1,5,3]
]

Using Array Variables

Power Automate supports array variables, which are ideal for storing collections of data dynamically.

Looping Through Arrays

The Apply to each control is the standard way to iterate through the items in an array. You can use the output of a createArray() function or an array variable as the input for the Apply to each loop. Inside the loop, you can then perform actions on each individual item.

Real-World Example: Employee Notification System

Consider an employee list: John, David, Smith, Chris. A Power Automate flow can:

  1. Use createArray() to put these names into an array.
  2. Use Apply to each to loop through the array.
  3. Inside the loop, send an email to each employee.

This results in emails being sent to all employees efficiently.

SharePoint Example

When working with multiple SharePoint items, you can create an array of item identifiers or relevant data. For instance, if you have tasks like 'Task1', 'Task2', 'Task3':

  1. Create an array: createArray('Task1', 'Task2', 'Task3')
  2. Use Apply to each to loop through this array.
  3. Within the loop, you could create corresponding records in another SharePoint list or perform other actions based on each task.

This is useful for processing multiple SharePoint records in a structured manner.

Common Array Functions in Power Automate

Workflow Summary

A typical workflow involving arrays might look like this:

  1. Data Acquisition: Gather data from various sources.
  2. Create Array: Use createArray() or other methods to form an array.
  3. Store Values: Optionally store the array in a variable.
  4. Apply to Each: Loop through the array items.
  5. Process Records: Perform actions on each item.
  6. Output Result: Generate the final output or update systems.

Best Practices

Common Challenges

Benefits of Using Arrays

Conclusion

The createArray() function is a fundamental and highly useful expression in Power Automate for managing collections of data. Whether you are working with email lists, SharePoint items, Dataverse records, Excel rows, or API responses, arrays simplify automation and enhance flow efficiency.

By effectively combining createArray(), array variables, Append to array variable actions, and Apply to each loops, developers can construct scalable and dynamic Power Automate solutions that efficiently process large amounts of data. The createArray() function is specifically designed to create arrays with multiple values, offering greater flexibility than the array() function for many common business scenarios.