Power Automate
Manish Solanki  

Join arrays efficiently without using loops in Power Automate

Use Case

Usually, we come across the problem of joining the arrays. One most common case is “left join” where resultant array contains the matched values and if second array does not have the matching value then it stores blank or empty value in the final result. I will show you the way to perform left join without using nested loops with a real case scenario.

Scenario & Sample data

In an education institution, there are pre booked walk-in appointments scheduled for 30 minutes duration slot for students. The admin shared the full day schedule with the required recipients. Here is the sample booking slots for 23rd Oct 2023:

When we directly fetch the events in Power Automate using “Get”, we will get the list of events which are scheduled for that day but does not return the blank slots when there is no appointment. We will generate the full day appointment list in this example.

Solution

1. Create a new scheduled cloud flow and set the occurrence as per the requirement. Here, I have set up the flow to run daily at 9:30 am:

Click on the Recurrence action and set the time zone, start hours & minutes:

2. Add Compose action that stores all possible start time of 30 duration event during business hours. Here, I have considered the business hours from 10:00 am to 5:00 pm, you may modify it as per your need:

Please note that I have used 13:00 – 14:00 as lunch timings in the above screenshot.

[
"10:00",
"10:30",
"11:00",
"11:30",
"12:00",
"12:30",
"13:00",
"14:00",
"14:30",
"15:00",
"15:30",
"16:00",
"16:30"
]

3. Add “Get calendar view of events” action to fetch the events. Choose the Calendar Id from the drop-down list. To get the current date event. we need to write an expression in the “Start Time” & “End Time” parameter:

Expression used for Start Time:

startOfDay(utcNow())

Expression used for End Time:

startOfDay(addDays(utcNow(),1))

4. Add Select action to create an array of StartTime & Subject from the output of get events action. Pass the value object from the output of ‘Get calendar view of events’ action. Expression needs to be written for converting UTC to local time zone and get the value in HH:mm format:

Expression used for StartTime property, you may modify the target time zone as per your need:

convertTimeZone(item()?['start'],'UTC','India Standard Time','HH:mm')

5. Add another Select action which will perform the left join operation. Pass the output of Compose action (contains all possible start time for a day) in “From” parameter. For both map properties, expression needs to be written in the expression box:

Expression used in the above screenshot:

KeyValue
Timeitem()
Appointmentif(greater(length(xpath(xml(json(concat(‘{“Root”:{“Item”:’,body(‘Select’),’}}’))),concat(‘//StartTime[text()=’,””,item(),””,’]/..’))),0),json(xml(base64ToString(first(xpath(xml(json(concat(‘{“Root”:{“Item”:’,body(‘Select’),’}}’))),concat(‘//StartTime[text()=’,””,item(),””,’]/..’)))?[‘$content’])))?[‘Item/Subject’],if(equals(item(),’13:00′),’No Appointments – Lunch’,”))

Expression used in Appointment value lookup for the time value present in the output array from Select action. If found, it gets the corresponding subject & set it in Appointment column. In nested if condition, it checks for lunch timings (13:00) & accordingly returns the value.

6. Next, add Create HTML table action & pass the output of ‘Select 2’ action in the “From” parameter:

7. Finally, add “Send an email” action, set the email subject & recipients. Pass the output of “Create HTML table” action to the body of email. I have used an expression to create a border for html table in email body:

Expression used for creating border in html table:

replace(body('Create_HTML_table'),'<table>','<table border="1">')

Output

The result of the flow execution will look like:

Conclusion

As you have seen in this blog, it is possible to join arrays using Select action & fx expressions. We should avoid using loops or “Apply to each” action in joining or comparing arrays especially with large elements exists in the arrays. I hope that this tutorial would help you in using the above technique where joining of arrays is required.

Leave A Comment