Shuffle an array with Power Automate

Shuffle an array with Power Automate header image

While preparing for a demo I had a challenge to shuffle some items in an array. In my use case I wanted to quiz users and ask them questions. To prevent users figuring out order or trying to trick the system it made sense to shuffle the data. Nothing to fancy I figured but as it turns out there is no shuffle available in Power Automate.


Now there are quite a few good samples out there that explain the options for sorting and shuffling. My first take was to introduce a random number and sort my array by that number. Yet sorting isn’t too easy either. My other take was to build an Azure Function that did the trick but that felt a bit to complex for what I was trying to achieve. Next, I had a look at Excel but didn’t want to have an Excel file dependency.

I finally settled on something that works if your array is not to long working with the expression functions available. This most likely isn’t the nicest solution for larger sets but with 3 to 5 items you can easily use this to randomize your data.

The sample data is some combination of three options:

   "title": "Option 1",
   "value": "Choice 1"
   "title": "Option 2",
   "value": "Choice 2"
   "title": "Option 3",
   "value": "Choice 3"

In order to randomize this we need a RandomNumber that starts from 0 and has a max of our array length. In this case a rand(0,2) function. It has to be action in our flow as we need this value multiple times. With that random number we can use the skip() and take() functions. The skip() function will get our data from the starting index we pass (0,1 or 2). The take() function will get everything before that index. Using both functions would get is two parts of the array. Using a union() we can then combine both parts together. The only remark here is that to use skip() or take() the passed object needs to be of type json. So to achieve that you can either cast it explicitly or make sure that the input is of type json.

The final expression will look as follows:

    skip(json(variables('ObjectArray')), variables('RandomStart')),
    take(json(variables('ObjectArray')), variables('RandomStart'))

Execution of this command will get a random value and our array will either start with option 1, 2 or 3.

Shuffle array flow

Larger data sets

I understand that this is not truly shuffling from a mathematical perspective. The results will one of the following:

  • 1,2,3
  • 2,3,1
  • 3,1,2

So it is missing options like:

  • 1,3,2
  • 2,1,3
  • 3,2,1

If you are working with larger datasets, or need some more ‘shuffling’ you could run this through a loop and execute it multiple times. In my case having the feeling of random options in a set of three was sufficient 🔀.

⚒️ Just in case you want to give it a try yourself you can download the sample Flow from here.

Loading comments…