Power Automate CreateArray Function with Real-Time Examples

Power Automate CreateArray Function with Real-Time Examples

No comments

Loading

In this “Power Automate CreateArray Function” article, we will learn about what a createArray function is in Power Automate and how to work with the createArray function in Power Automate with real-time project-based examples.

Power Automate, formerly known as Microsoft Flow, is a powerful automation platform that allows you to create workflows and automate various tasks across a wide range of applications and services. One of the key functions in Power Automate is the CreateArray function, which enables you to create arrays, a fundamental data structure used to store and manipulate collections of data. In this article, we’ll explore the CreateArray function in Power Automate and provide real-time examples to demonstrate its practical applications.

Understanding the Power Automate CreateArray Function

The CreateArray function in Power Automate allows you to create an array by specifying the number of elements it should contain. You can use this function to initialize an array with specific values, making it a versatile tool for working with data within your workflows.

The basic syntax of the createArray function is as follows:

createArray(Number of Elements, Initial Value)
  • Number of Elements: This is the number of elements you want in your array.
  • Initial Value (optional): You can specify an initial value to populate the array. If omitted, the array will be initialized with null values.

Now, let’s dive into some real-time examples to better understand how to use the CreateArray function in Power Automate.

Power Automate CreateArray Function Syntax
Power Automate CreateArray Function Syntax

Power Automate CreateArray Function with Real-Time Examples

In this section, we will learn about how to work with the CreateArray function in Power Automate with real-time examples.

CreateArray Function demo with Boolean variable

Let’s create a boolean-type variable and assign the default value to true.

Then, add a compose data operation action and add the below createArray function expression:

createArray('Power Automate',variables('varBoolTest'))
Power Automate CreateArray Function Demo with Boolean variable
Power Automate CreateArray Function Demo with Boolean variable

Output:

Once we run the flow, we can see the below output:

[

  "Power Automate",

  true

]
Power Automate createArray Function Demo with Boolean variable output
Power Automate createArray Function Demo with Boolean variable output

CreateArray Function demo with Integer variable

Let’s create an integer-type variable and assign the default value to 21.

Then, add a compose data operation action and add the below createArray function expression:

createArray('Power Automate', variables('varIntTest'))
Power Automate CreateArray Function Demo with Integer variable
Power Automate CreateArray Function Demo with Integer variable

Output:

Once we run the flow, we can see the below output:

[

  "Power Automate",

  21

]
Power Automate CreateArray Function Demo with Integer variable output
Power Automate CreateArray Function Demo with Integer variable output

CreateArray Function demo with Array variable

Let’s create an array-type variable and assign the default as below:

[
"Power ",
"Automate"
]

Then, add a compose data operation action and add the below createArray function expression:

createArray('I like',variables('varArrayTest'))
Power Automate CreateArray Function Demo with Array variable
Power Automate CreateArray Function Demo with Array variable

Output:

Once we run the flow, we can see the below output:

[

  "I like",

  [

    "Power ",

    "Automate"

  ]

]
Power Automate CreateArray Function Demo with Array variable output
Power Automate CreateArray Function Demo with Array variable output

Create an Array of Objects: CreateArray Function demo with Object variable

Let’s create an object-type variable and assign the default as below:

{
"Name": "John",
"Age": 30,
"Department": "HR"
}

Then, add a compose data operation action and add the below createArray function expression:

createArray(3,variables('varObjectTest'))

Power Automate CreateArray Function Demo with Object variable

Power Automate CreateArray Function Demo with Object variable

Output:

Once we run the flow, we can see the below output:

[

  3,

  {

    "Name": "John",

    "Age": 30,

    "Department": "HR"

  }

]
Power Automate CreateArray Function Demo with Object variable output
Power Automate CreateArray Function Demo with Object variable output
From the above examples, it seems like the “CreateArray” function has similarities with the “Append to array variable” action. Now let’s understand the difference between the “CreateArray” function and “Append to array variable” in Power Automate. In which scenario should we create the CreateArray” function and the “Append to array variable” action?

Difference between the “CreateArray” function and the “Append to array variable” action in Power Automate

The “CreateArray” function and the “Append to array variable” action in Power Automate both deal with arrays, but they serve different purposes. Let’s explore the differences between these two and when to use each one.

CreateArray Function:

  • Initialization: The “CreateArray” function is used for initializing a new array with a specified number of elements. It’s typically used to create an empty array or an array with null values initially. You can also initialize an array with an optional initial value, which sets the same value for all elements.
  • Static Arrays: “CreateArray” is suitable for creating static arrays, where the number of elements is known in advance, and you don’t need to modify the array’s size during the flow’s execution.
  • Use Cases: It is handy when you know the exact number of elements you want in your array, and you want to work with a fixed-size array. For example, when you need to set up an array with specific indexes to hold data that will be added later in your flow.

Append to Array Variable Action:

  • Appending Data: The “Append to array variable” action is used to add new elements to an existing array variable. It’s ideal when you need to dynamically add data to an array during the execution of your flow.
  • Dynamic Arrays: This action is suitable for creating dynamic arrays where the size of the array can change based on the flow’s logic and input.
  • Use Cases: Use the “Append to array variable” action when you want to accumulate data over time, such as collecting form responses, gathering user input, or processing items in a loop, and you need to expand the array as new data arrives.

The choice between the “CreateArray” function and the “Append to array variable” action depends on your specific flow requirements:

  • CreateArray Function: Use it when you need to initialize a fixed-size array, and you know the number of elements in advance. This is suitable for creating static arrays with a predefined structure.
  • Append to Array Variable Action: Use it when you want to dynamically build an array by adding elements during the execution of your flow. This is helpful when dealing with variable-sized collections of data that grow or change as your flow progresses.

Understanding when to use each of these options will help you design more efficient and flexible flows in Power Automate, tailored to your specific needs.

Summary: CreateArray function in Power Automate

Thus, in this article, we have learned about what a createArray expression or function is in Power Automate and how to use this createArray function in Power Automate with real-time project scenarios.
In Power Automate, the CreateArray function is one of the powerful functions in Power Automate for initializing arrays with a specified number of elements, making it a valuable asset in automation workflows. This function allows you to create arrays of various data types, including numbers, strings, and even complex objects, enabling you to efficiently handle and manipulate data within your flows. Whether you need to generate static arrays with a predetermined structure or set up dynamic arrays that can grow and change during the workflow’s execution, the CreateArray function is a versatile feature that empowers users to streamline their processes and automate tasks with precision and flexibility.

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. For the Power Automate function references, you may refer to this Microsoft Power Automate Function references  article.

 

About Post Author

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