James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


James Montemagno

Randomizing Lists in Power Automate Flow

Every Monday at 9:15 my team and I gather for our weekly team meeting to talk about our achievements from the previous week and what are plans are for the current week.  Traditionally during status updates people just go around the room, but in a virtual standup it is easy just to go down the list alphabetically. I don't feel like this is very fair to members in the meeting. When I became a new manager and setup the weekly meeting I knew I wanted to do something a bit different. That is why each week I have randomized the speaking order. At first it was by reverse name, city, state, length at Microsoft, but then I ran out of ideas and ran to random.org's list randomizer.

A list of names randomized by random.org

This was fine, but I had to remember every Monday morning to enter this list, copy and paste it, and hope it is random enough. I knew there must be a better way.... and Power Automate (Flow) has been my go to recently for automating all of the tedious stuff I don't want to do. I have it send reminder emails, put stuff on Planner, and even approve upcoming Xamarin Shows!

Randomizer Flow

The flow that I decided to create is triggered every Monday at 9AM. It will randomize the list and then post it to our Teams channel. So, to get started we will a scheduled flow that repeats at 9AM:

A new flow that is set to repeat weekly at 9AM

Initialize Variables

I love that Flow has variables that you can use over and over and update. We need to create two string variables. The first will be our final ordered team list named Ordered Team:

A string variable named Ordered Team

The next will be a named Team which will be a JSON array (with an Id and a Name) blob that we will use later to randomize the list. I have Flow randomly generate a random Id by using the guid() function and then specify the name:

Another string variable with a json list of names
[
  { "Id": "@{guid()}", "Name": "Abdullah Hamed" },
  { "Id": "@{guid()}", "Name": "James Montemagno" },
  { "Id": "@{guid()}", "Name": "Jayme Singleton" },
  { "Id": "@{guid()}", "Name": "Jeff Fritz" },
  { "Id": "@{guid()}", "Name": "Jon Galloway" },
  { "Id": "@{guid()}", "Name": "Nish Anil" },
]

Now, you may be wondering... why are we using a JSON blob and why not an array that we can magically sort. Well... from all of my searching there is no built in Sort or Shuffle inside of Power Automate. If there was or when there is this will make it easier. For now however, we will turn to a custom Azure Function to randomize our list!

Azure Functions to the Rescue

Now, I know that it sounds a bit silly to use a function for this, but why not! The nice thing is once it is published I will never have to update it again as I can update the flow if I have new members join the team. I can also automate sending the message to teams via flow and also adjust the schedule. It is a nice combo!

This is probably the easiest Azure Function in the world as it is just a HTTP function that takes in a JSON body that we specified and order's the Ids (which are always random GUIDs).

public class Item
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}
public static class Function1
{
    [FunctionName("SortListFunction")]
    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        var data = JsonConvert.DeserializeObject<Item[]>(requestBody);
        data = data.OrderBy(i => i.Id).ToArray();
        var json = JsonConvert.SerializeObject(data);
        return new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent(json, Encoding.UTF8, "application/json")
        };
    }
}

5 lines of code! DONE!

Calling the Function & Parsing Data

Now it is time to head back over to our Flow and make an HTTP POST to our function and pass it the Team data as the Body. If you desire to use my function I will let you :)

An HTTP POST calling the Azure Function and passing in the Team as the Body

When the Flow calls this function it will get return data, which is the new JSON list of randomized team members! Under the Data Operations you will find a parse JSON action. Here we will pass in the Body from the HTTP action and then the schema.

Parsing JSON Data from the HTTP Post

You can have it generate it from sample data or use mine:

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "Id": {
                "type": "string"
            },
            "Name": {
                "type": "string"
            }
        },
        "required": [
            "Id",
            "Name"
        ]
    }
}

Post to Teams

For me I want to post the list to my Teams channel every Monday, so we need to create a tiny bit of HTML to post. We can do this by Appending to string variable each name in the list followed by a <br> tag on the Ordered Team variable we created earlier. This is a loop operation so the easiest thing to do is to add an action for Appending to string variable and select the Body from the Parse JSON action. This will automatically put it inside of an Apply to each loop where we can append Name and <br> to the string:

Apply each name and break tag to the ordered team

Finally, we can post to the team channel:

Post the ordered list to the teams channel
My post in the teams channel with the list

There you have it! Combining Power Automate Flow and Azure Functions together to simplify sorting and organizing lists. I love this type of automation to really streamline the small tedious things throughout the work day. I hope you enjoyed! <3

Copyright © James Montemagno 2020 All rights reserved. Privacy Policy

View Comments