Over the weekend Brian Dang asked me on twitter if there are any easy ways to avoid an apply to each step. He gave me an example where he tried to convert time to other time zones.
So what you do when you want to convert your time to other multiple other time zones?
Well of course this is possible!
The first I will explain the situation that we are trying to solve. In my example I will create a date and time in Power Apps. Then I will l use a button in Power Apps to convert the time to all time zones in the world using a Flow in Power Automate.
I will first show you the end result:
I hope that you will agree that this flow will need to run as fast as possible as we wouldn’t want the user to wait for 30 seconds to do all the work. If we created a flow with an apply to each loop, creating all the data then you will find that the flow might take a bit longer than you would hope for. We want a flow that takes nearly no time at all.
This is the flow that I’ve created. From the flow run you can see, 0 seconds! That is good enough for me.
To understand the above flow you might want to have a look at a couple of posts.
- Returning an array of data to PowerApps
- Format dates in Power Automate
- Improve your flow’s performance in a few easy steps
Pushing the time into the Select
I’m going to create a select action that will take the input time. The input time however is text, rather than an array.
I will now see the following error when I run my flow:
The create an array, I’m using a variable. Yes I’m aware that variables could slow down my flow, but I’m not going to use any loops therefor it will not make any difference and the data type formatting supplied by variables is helpful in this case.
Convert your time
Now that we have an array I can feed my select action.
First I will need to format the input date correctly.
In the above action I used the following expression to get this done:
Generating all the timezones
My select action will now need to contain a mapping for each time zone.
For each mapping, I’m using an expression like this to generate the data
convertFromUtc(item(),'Afghanistan Standard Time')
Returning data to Power Apps
The Respond to a PowerApp or flow action doesn’t support arrays. The response action however does handle arrays.
I’m only going to return the first item in the array given by the select as I will only ever receive on item with all time zones that I’ve generated in my Select.
The Power App
Now the easy part.
My Power App takes a date and time form an input field in my app and when I press a button the flow will be kicked off setting a variable called varZones.
Now I can refer to varZones.whateverzone to get my correct time and date
Quite often building your flows with Apply to each steps can be the easy way to automate your processes, however when you need some performance out of your flow then you might want to consider removing these slow steps. Especcially when you have a user starting a flow and waiting for a response then you don’t want to let them wait forever.
Do you need to get your flows to perform better then please feel free to contact me.