In this blog post, we’re going to learn how to use the concurrent function within Power Apps. The concurrent function, as the name suggests, allows you to run functions concurrently within Power Apps.
Usually, when you run any processes or workflows, they need to run right after another. But there are special cases where you can optimize your app by making them run concurrently or at the same time instead. And that’s what we’ll be discussing in this post.
What I have here is a very simple application that we want to optimize within Power Apps. When a user types in a phrase here, the Run Sequence button does a lot of different things.
Let’s go to the OnSelect property of the Run Sequence button.
As we can see over here, there are quite a few things going on. We first set the variable StartTime = Now, just so we can time how long this takes.
We then clear the collection variable called Output.
And then we do these sets of things which probably take the longest amount of time. We basically ask Microsoft Translator to translate whatever we put into text input to French, German, Dutch, Korean, Greek, Spanish, and Hindi. The semicolons at the end of each line represents that this happens sequentially.
One of the last things we do is take all of these inputs, all of these variables, and create a collection from them. This collection has the actual text that we want to translate, the language that we would translate to, and the actual translated text as well. And again, we do it for French, German, Dutch, and so on and so forth.
Refresh SharePoint Connection
We will also refresh a SharePoint connection to add an extra layer of processing time. Then, we’ll set the variable EndTime = Now because we want to track how long this entire sequence takes.
Again, this is a sequence which we know by the presence of semicolon here. Each semicolon represents a new function starting so this all happens in sequential order.
Let’s run this program by clicking on the Run Sequence button. We can see that it outputs a table with the actual phrase Hello, the language, and its corresponding translation.
We can see also see at the bottom that when we refreshed a SharePoint list, it took 500 milliseconds.
Obviously, this didn’t take too long. And no one will get worked up for something that takes 500 milliseconds to process.
But what if these processes (which are using third party services) do take a long time? What if instead of just writing one word over here, we copy and paste section two of the US Constitution over here, then run the sequence?
This one took a little bit longer. It took 1700 milliseconds or about 1.7 seconds.
If I copy and paste section 3 of the US Constitution, it will take exponentially longer. Now we’re at about four seconds.
Sequential Vs Concurrent Function In Power Apps
You can see the use case of running these functions concurrently because if you run them in sequence and if you tend to have very complex functions, it really tends to take a while.
Now let’s do the exact same thing, but on the right hand side, we’ll do it concurrently. Let’s change this back to Hello, run the sequence, make a copy of this, and bring it to the right hand side.
We need to go to the OnSelect property of this button and we need to change it to make it concurrent. After the Clear Output, we will type in the word Concurrent.
The concurrent function within Power Apps takes different functions as arguments, or what Power Apps calls formulas. So we’re going to make this all concurrent, which means instead of Power Apps doing French first, then German, then Dutch, it will do it all at the same time.
We’re going to surround all of these functions in the concurrent function. The reason why it’s giving us an error is because the concurrent function takes arguments as formulas that are separated by commas. We are currently using semicolons so all we need to do is to go into each one of these things and change them to commas.
For the last time semi-colon, we’ll just get rid of it because it’s the last argument in this function. Then we’ll need a semicolon at the very end of this because concurrent is technically a function as well.
One of the last things we’ll do is move the Refresh Customers List to the concurrent function because there’s no dependency over here.
Again, the reason we’re doing this is because all of these functions are not dependent on each other, which means they don’t need to happen sequentially. They can happen at the same time, which is why we’re doing this in the first place.
So after we’ve done all this, we then need to make sure that we go over here and set this to Start Time two and End Time two, just so it doesn’t conflict with the other start time and the other end time variables.
And we’ll also change Output to Output Two so that the variables don’t mash up.
Let’s close this function and test it out. We’ll change the variables from End Time to End Time Two and Start Time to Start Time Two.
We’ll then modify the data table to look at Output Two, because that’s what this button produces. We’ll change the button from Run Sequence to Run Concurrent.
So now we have two things that do the exact same thing. One does it sequentially, and one does it concurrently.
Let’s do a few tests. We’ll type in Hello, and run the sequence. We’ll run the sequence button first and the concurrent one next.
We can see that concurrent is about two times or three times faster than sequential. Now, let’s go ahead and copy sections 1, 2, 3, 4, 5, 6, and 7 of the US Constitution, and see how long this one takes when using the sequence and concurrent buttons.
The sequence button took a long time and there was noticeable pause. Your users will not like that. But if you are to run it concurrently, it will only take one second.
It still takes a while. But you know that you’re doing it in the most optimized way you can. The difference between one second and three seconds for your users can mean a lot.
A 3x multiplier is pretty good for optimizing your Power Apps.
Advantage Of The Concurrent Function In Power Apps
Notice there are some things that are not optimized here. Let’s say you have 40, 50, or 60 languages, then you would not only notice a 2x or 3x multiplier, you might notice a 10x or 12x multiplier because you’re doing more things concurrently than you were doing sequentially.
So in this example, you only see a 3x multiplier, but again, if you have more concurrent processes than the one we’re running here, then you’ll notice an even faster speed.
In this blog post, we discussed what the concurrent function is and how to use it to make your Power Apps more optimized. When you do multiple data updates or read multiple data sources, you can do it faster with the concurrent function.
All the best,