- Microsoft Power Automate Community
- Welcome to the Community!
- News & Announcements
- Get Help with Power Automate
- General Power Automate Discussion
- Connecting To Data
- Building Flows
- Using Flows
- Power Automate Desktop
- Process Advisor
- AI Builder
- Power Automate Mobile App
- Translation Quality Feedback
- Connector Development
- Share, Chat, Discuss
- Community Chat & Announcements
- Power Automate Cookbook
- Off Topic Discussion
- Power Platform Integration - Better Together!
- Power Platform Integrations
- Power Platform and Dynamics 365 Integrations
- Watch & Share Videos
- Webinars and Video Gallery
- 2021 MSBizAppsSummit Gallery
- 2020 MSBizAppsSummit Gallery
- 2019 MSBizAppsSummit Gallery
- Give Feedback & Share Ideas
- Power Automate Ideas
- Report Issues
- I Found A Bug
- Find & Join Local User Groups
- Power Automate User Groups
- Read & Share Blogs
- Power Automate Community Blog
- Community Blog Topic Suggestions
- Community Support
- Community Accounts & Registration
- Using the Community
- Community Feedback

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results forย

- Microsoft Power Automate Community
- Forums
- Get Help with Power Automate
- Building Flows
- Chunking an array down to manageable groups

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Chunking an array down to manageable groups

โ05-12-2021
07:27 AM

I have an array of email addresses like this:

[

"email@email.com",

"another@email.com",

"qwerty@abc.com"

]

The array can contain anywhere from 1 to thousands of emails. I want to change the array so that the emails are in groups of 300 e.g.

[

{

"email@email.com",

"another@email.com",

"qwerty@abc.com"

...to 300

},

{

"more@email.com"

...to another 300

}

]

What is the best way to achieve this?

Solved! Go to Solution.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

โ05-12-2021
07:59 AM

**Quick Answer**

You will need to use the **take()** and **skip()** functions in an expression.

It's best to do this with the **length()** of the original array in mind.

If you're already pretty good with the logic side, then that should be all you need.

**Solution**

Here's my flow to handle this, you will replace the "3" with "100".

This could be minimised a bit by moving some of the actions into expressions, but this shows you the logic I was aiming for.

I will edit in explanation shortly.

You can read on if you like.

**Logic**

Essentially, this works out how many runs are needed to divide the array into chunks of 3 (100 for you), then, for the set amount of turns, it will set the arrayVAR from the arrayWorkerVAR, with increasingly less (by 3) items than before.

Each run, it will also place those three deleted items into an object that will be placed into the newArrayVAR array.

**Expressions/Code Used**

*Initialize divvyVAR*

This looks more complicated than it is (*see explanation*) but essentially a normal division doesn't see the "remainder" so I had to see if the number of loops needed one more.

`if(contains(string(div(float(length(variables('arrayVAR'))), 3)), '.'), add(div(length(variables('arrayVAR')), 3), 1), div(length(variables('arrayVAR')), 3))`

*jsonObject*

This uses the **take()** function to take the first 3 items from the arrayVAR variable and create a new array.

The code:

```
{
"chunk": @{take(variables('arrayVAR'), 3)}
}
```

The expression within:

`take(variables('arrayVAR'), 3)`

*Set arrayWorkerVAR*

This uses the **skip()** function to skip over the first 3 items in the arrayVAR variable, to create a new array.

The reason the flow needs an extra array here, is because you cannot set a variable's value from itself.

`skip(variables('arrayVAR'), 3)`

**Detailed Explanation for the Long IF Statement in divvyVAR**

Explaining quickly the basic functions:

- length() - This counts the number of entries in an array or characters in a string.
- float() - This formats a number in to a floating point type, allowing decimal points in any further actions.
- div() - In two parts separated by a comma this will divide the second number from the first.
- string() - This converts any other data type into a text string.
- contains() - In two parts this checks the first part (a string or array) before the comma, for the second part after it.
- add() - Adds any numbers (split by commas) to each other.
- if() - In three parts separated by commas, if the first part is 'true' this will return the value of the second part, but if it is 'false' it will return the third part.

So ... if I get a floating point number from my division, then I will need an extra step to catch the remainder of the items in the array ... meaning:

**IF** - The result of dividing the number of entries in the original array produces a number with a decimal point, then the **contains()** function will return **TRUE**.

**THEN** - Add 1 to the number of a *normal* (not floating point) division of the length.

**ELSE** - Just take the normal division of the number.

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

โ05-12-2021
07:59 AM

**Quick Answer**

You will need to use the **take()** and **skip()** functions in an expression.

It's best to do this with the **length()** of the original array in mind.

If you're already pretty good with the logic side, then that should be all you need.

**Solution**

Here's my flow to handle this, you will replace the "3" with "100".

This could be minimised a bit by moving some of the actions into expressions, but this shows you the logic I was aiming for.

I will edit in explanation shortly.

You can read on if you like.

**Logic**

Essentially, this works out how many runs are needed to divide the array into chunks of 3 (100 for you), then, for the set amount of turns, it will set the arrayVAR from the arrayWorkerVAR, with increasingly less (by 3) items than before.

Each run, it will also place those three deleted items into an object that will be placed into the newArrayVAR array.

**Expressions/Code Used**

*Initialize divvyVAR*

This looks more complicated than it is (*see explanation*) but essentially a normal division doesn't see the "remainder" so I had to see if the number of loops needed one more.

`if(contains(string(div(float(length(variables('arrayVAR'))), 3)), '.'), add(div(length(variables('arrayVAR')), 3), 1), div(length(variables('arrayVAR')), 3))`

*jsonObject*

This uses the **take()** function to take the first 3 items from the arrayVAR variable and create a new array.

The code:

```
{
"chunk": @{take(variables('arrayVAR'), 3)}
}
```

The expression within:

`take(variables('arrayVAR'), 3)`

*Set arrayWorkerVAR*

This uses the **skip()** function to skip over the first 3 items in the arrayVAR variable, to create a new array.

The reason the flow needs an extra array here, is because you cannot set a variable's value from itself.

`skip(variables('arrayVAR'), 3)`

**Detailed Explanation for the Long IF Statement in divvyVAR**

Explaining quickly the basic functions:

- length() - This counts the number of entries in an array or characters in a string.
- float() - This formats a number in to a floating point type, allowing decimal points in any further actions.
- div() - In two parts separated by a comma this will divide the second number from the first.
- string() - This converts any other data type into a text string.
- contains() - In two parts this checks the first part (a string or array) before the comma, for the second part after it.
- add() - Adds any numbers (split by commas) to each other.
- if() - In three parts separated by commas, if the first part is 'true' this will return the value of the second part, but if it is 'false' it will return the third part.

So ... if I get a floating point number from my division, then I will need an extra step to catch the remainder of the items in the array ... meaning:

**IF** - The result of dividing the number of entries in the original array produces a number with a decimal point, then the **contains()** function will return **TRUE**.

**THEN** - Add 1 to the number of a *normal* (not floating point) division of the length.

**ELSE** - Just take the normal division of the number.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

a month ago

This is fantastic thank you, I will try this out today and let you know how I get on.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

a month ago

You, sir, are a fantastic human being. This does exactly what I wanted! Very comprehensive answer and well explained, thank you so much.

Announcements

Check out new user group experience and if you are a leader please create your group

Top Solution Authors

User | Count |
---|---|

36 | |

32 | |

25 | |

22 | |

21 |

Top Kudoed Authors

User | Count |
---|---|

50 | |

39 | |

33 | |

28 | |

25 |