Send One Email per Person in a Mixed List

apply to eachOne of the most common questions I see on the Power Platform forums is: “I have a data source that contains a list of information that I need to send as an HTML table to the individual mentioned in each item. But its sending more than one email to each person.  How do I get it to send just one email for each person with all the records for that person?”  In this post I’ll demonstrate a general method to accomplish that goal using Power Automate. For this example we’ll use a simple list from SharePoint, but the same technique should work for any data source that can be sorted.  Here’s our list:

list

We want to send a table with the hours spent by each programmer and their current status to each Project Manager.

Step#1 – Retrieve the records from data source.  Sort them based on the field that references the person, in this case Project Manager. This will not work if the field includes multiple people. Here’s a screenshot of that part of the flow.

list1

Step#2 – The next step is to get a list of the distinct project managers in the results we have retrieved.  Unfortunately, Power Automate doesn’t have a Distinct() function.  But we can achieve the same result using a combination of a Data Select action and the Union() function. When we combine the Data Select array with itself using Union() we will get an array that has a single instance of each unique record in the Data Select array. In this case a list of each Project Manager’s Email address.

Distinct1

Step#3 – Now that we have an array of distinct Project Manager Emails we can run an Apply to Each loop on each Email, extract the records for that manager, create a table and send the email. Here’s what the overall Loop looks like then we’ll review each step. The loop runs on the array produced by the union in the last Compose action.

loop 1

Loop Step #1 – The first thing we do inside the loop is filter the original set of records to create an array of records where the Project Manager’s Email in the record matches the current item’s email.  Since we stored the email as a record using the data select we need to retrieve it using JSON notation in the Expressions tab.  The JSON will be for the current loop item ‘PM’ property.  items(‘Apply_to_each’)?[‘PM’]

apply1

Loop Step#2 – Now that we have the records for one of the Project Managers we need to turn them into a table. First we need to use Parse JSON to re-apply a schema to the filtered records so we can access individual columns.  We parse the Body output of the filter array action. Run the flow once after adding the Parse JSON and use the output from the filter array to generate a JSON schema.

apply2

Loop Step#3 – Now we are ready to create the HTML table to embed in the email. Use the Create HTML Table set for custom and map the columns you want in the table from the Parse JSON output. I used the programmer’s displayName, Hours, Status, and Project columns. You may have multiple columns with the same name, like DisplayName.  These represent the embedded properties in objects.  For example the DisplayName might be for the Project Manager, the Programmer, or the person who created the item in the list. You can tell which one you added by hovering over the column and looking at the JSON.

apply3

Loop Step#4 – The last step in the loop is to send the email. Use the same JSON that you used to filter the array in Loop Step#1, since that is the email address of the Project manager. Fill out the subject and Body of the email with whatever information you want, including the HTML table you just created.

Apply4

That’s all you need.  The loop will now process each Person in the list and send one email to each with the correct information. I’m sure your list will be different, but the steps should be essentially the same.

Running a Recurrence Flow on Weekdays Only

RecurrenceRecurrence Triggers are essential for a wide variety of scenarios.  They can be used to run a flow on a specific date and time.  Or they can be used to run a flow on a recurring schedule, like once a day or once a month.  They can even be used in situations where  a process needs to run longer than the 30 day timeout limit on flows.

But what if your recurring schedule has exceptions.  For example, you might want to run it on the first Monday of each month or only on weekdays.  How can we use the recurrence trigger to handle these more complicated scenarios?

One possible solution is to trigger the flow on a simple schedule, like daily, and then test to see if its the right day.  If it is proceed with the flow, but if its not just exit.  This does work, but it consumes a lot of flow runs that are unnecessary.  Not only is this inefficient it may cause an issue with the limits on API calls in a 24 hour period.  It would be much better if we could take these conditions into account BEFORE the flow triggers.  But we can’t always reach that level of granularity in the trigger configuration itself. But we can if we use the trigger conditions available under settings.

Let’s look at a specific scenario.  I want this flow to trigger on each workday of the month. I start out by creating a flow with a trigger set to fire once a day.  This will create a flow that runs everyday of the month.  So far the trigger will look like this.

RecurrenceTrigger

Next I need an appropriate filter that will limit the trigger to only fire on Monday thru Friday.  If you aren’t familiar with how to write that filter I suggest using a ‘Filter array’ action to put it together.  After adding a ‘Filter array’ action to your flow fill in the left side of the comparison with the following formula:

dayOfWeek(utcNow())

That will take the current date and convert it to a number for the day of the week.  Monday = 1, Tuesday = 2, etc. Now finish the condition by choosing ‘is less than or equal to’ in the middle and enter ‘5’ on the right.  We haven’t filled in the Array to filter, but since all we want is the formula that’s OK.  We’ll be deleting this action after we copy the formula.  Your action should now look like this.

filter array

Now comes the trick that makes this worthwhile.  Click on the ‘Edit in advanced mode’ link. The action will now look like the following.

filter array advanced

The formula we need is the one highlighted in yellow.  Copy that formula to your clipboard and delete the ‘Filter array’ action.  Our finished flow doesn’t actually use it.  Now open the settings dialog for the recurrence trigger using the ellipses (…) menu on the trigger. Click the + Add link under Trigger Conditions and paste the formula you copied into the textbox that appears.  Your Settings should now look like this.

Settings

You can now click Done and your flow trigger each day if it is a Monday through Friday.  It will not trigger on Saturday, day 6, or Sunday, day 7. Using trigger conditions you can fine tune the schedule of your recurrence trigger to only fire when you want it to.

Microsoft Flow Virtual Summit – Demos

collab365-virtual-summitsToday was the first day of the Microsoft Flow Virtual Summit sponsored by Collab 365. I really enjoyed being able to sit back and watch my recorded session and spend my time answering questions from the attendees. The comments I got in the chat window were all very gracious and complimentary. It didn’t take long during the session for people to start asking if I would make my demos available after the session. I promised that I would export all seven and post them here on my blog and also send them to the Collab 365 folks to be posted on the Summit site. The demos have all been exported and sent via email to Collab 365.  Now I just need to upload them and post them here:

  1. Applying custom formatting to the approval email
  2. Recording approval results and comments
  3. Creating custom responses without premium licensing
  4. Forwarding an approval if the approver is out of the office
  5. Escalating an approval that hasn’t been responded to
  6. Periodic reminders about pending approvals
  7. Serial Approvals with dynamic list of approvers

12 years as an MVP

July 1st is always a day that I both look forward to and dread.  That’s the morning I find out whether my MVP status has been renewed or not. I was particularly worried this year since its my first year after I semi-retired. I’m still working as a consultant, but I’m letting business chase me rather than me chasing business.  So I’m not as busy as I used to be. That’s been both a good and a bad thing. Its a good thing because I have more time to spend with my family and doing things for the Office 365 community. Its a bad thing because I don’t feel like I’m doing as much as in previous years. But I have evidently been doing enough because I was renewed for my 12th year as an MVP this year. I’m looking forward to another year of learning new things and helping spread that knowledge throughout the community.

Last year as I contemplated retirement I made an offer to community leaders.  I would like to expand on that offer this year.

Community Leaders this is for YOU

If you are looking for a SharePoint, Office 365, PowerApps or Flow speaker for your event, and you can help me defray travel expenses, I will be willing to travel almost anywhere, at anytime to share what I have learned over the years. If you don’t have any funds I can also do remote presentations.

Over the last few years I’ve been spending my time working with SharePoint, PowerApps, Flow, and Office 365 Security & Compliance center.  I’m still working to learn new things in the industry and plan to keep  consulting and training for many years to come.  But if paying jobs don’t happen to come my way then I’ll live off my retirement savings and spend even more time speaking and answering questions.

So here’s to another year as an MVP. I’m really looking forward to it.