There are many reasons why you may want to chunk an array into smaller arrays and you may be surprised to find out there isn’t an out of the box action or expression to do this. I am going to walk you through how you can use a handful of variables, a do until and some of expressions to do this.
For demo purposes I am going to simply generate an array of x number within Power Automate but this will work with any any of objects.
Our setup is going to consist of 4 variables. The first one is going to be our dataSource and we are going to use the range function to generate an array.
Next we will need to create a variable to store the remaining items from our dataSource array. This is because Power Automate doesn’t allow you to reference the variable you are trying to set or update within the function.
Next we set up the array which will house the smaller arrays, we will simply call this arrayChunk.
The last variable is totally optional because we could hardcode it into the expression later on but it allows us to change it later with ease. This is the arrayChunkSize and we will be setting this to 1000 to keep it easy.
We are using a do until loop so that we can loop through the remaining array taking 1000 items (arrayChunkSize) from it at a time and storing that array of items in our arrayChunk array. Our loop runs until there are no more items left in the remaining array and we do this by checking to see if the length of the array is equal to 0.
Our Do until loop has 2 actions inside of it, Append to array variable and Set variable. We use the Append to array variable to add to our arrayChunk and the Set Variable to update the remaining array.
The expression in the Append to array variable checks the length of the remaining variable to see if it is less than the arrayChunkSize variable. If it is it will stringify the remaining array and append it to the arrayChunk array. Otherwise it will take the first 1000 (arrayChunkSize) items from the remaining array and stringify it before appending it to the arrayChunk array.
Note: We need to string the results because Power Automate does not allow you to append an array to an array variable. This means we need to use the json function to convert the stringified array back.
skip(variables("dataSource"),mul(variables("arrayChunkSize"), add(iterationIndexes("Do_until"), 1)));
The expression in the Set variable action skips x number of items from the dataSource variable and updates the remaining array with the result. To determine how many items need to be skipped we simply multiply our arrayChunkSize (100) by the current index of the Do until loop + 1, since the index is 0 based. The great thing about the skip function is that if you skip more than the items that are in the array it will simply return an empty array.
After the Do until loop runs you can inspect the arrayChunk array to see the results. We will use a Compose action to check the output and should see an array of stringified arrays.
From here we can loop through the arrayChunk variable and take action on our smaller arrays. This can help you work on larger data sets from SharePoint or maybe an Excel file and stay within the limits of the Apply to Each Action for the free version of Power Automate, currently at 5000.