cancel
Showing results for 
Search instead for 
Did you mean: 
ManishSolanki

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:

ManishSolanki_0-1698041690329.png

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:

ManishSolanki_1-1698505224989.png

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

ManishSolanki_1-1698042076810.png

 

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:

ManishSolanki_2-1698042576012.png

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:

ManishSolanki_3-1698042589906.png

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:

ManishSolanki_2-1698505510572.png

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:

ManishSolanki_3-1698505596783.png

Expression used in the above screenshot:

Key Value
Time item()
Appointment if(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:

ManishSolanki_5-1698505774951.png

 

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:

ManishSolanki_6-1698505830759.png

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:

ManishSolanki_7-1698505915267.png

 

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.

 

About the Author
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/