How to count distinct Items in an Array in #PowerAutomate without using an Apply to Each loop

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another video in this video i am going to show you how you can count the number of distinct items in an array so this array um has got id a three times and b once and we want to count quickly and easily without using and apply to each that there are three a's and one b this is actually a response to a new power automate community member called prot3go who's trying to do it and has said that he doesn't want to use and apply to each so i guess in his production system this array is a lot bigger and it's just not fast enough so let's crack on and build it so the first thing that i am going to do is add a select action and i'm going to use the array as my input i'm going to put this select action into text mode and i'm just going to get the id and then i'm going to use a compose action so this select action is just going to give us a plain array um i'm going to call this one distinct i'm going to use the outputs of the select action along with union so i'm going to pick the output of the select and the output of the select and then from there i am going to create another compose action this is going to be called prepare array and it is going to look like this this might not be immediately obvious why i'm doing this but you'll soon see because i'm going to be using the x x path expression and so i'm going to make an xml representation of the array so i'm going to get the array there and then i'm going to do another step and this is going to be called xml i'm going to do xml dynamic content prepared array or prepare array so i'm just going to save that and see what we got okay so i'll select took our original list and broke it down to a simple array so we've got a a a a b a and then distinct that's just a and b and then our xml will look at in a different format i'm just going to put this into a pretty printer so we've got a nicely formatted xml now and then i am going to go to the xpath xpather.com and i'm going to build up an xpath query so what we are looking for is anything that's in the array and then i'm going to take the id forward slash text equal to a okay so that has now collected us all three of the items which are equal to a um and that'll probably do us actually so now we will go into another select action this time for the input select we're going to use the output of distinct so it's going to run once for each entry and we've got two entries which are a and b so on the left side we'll have id and the expression will just be item and then we'll have count on this side and then we'll have length so xpath will return an array i'll show you that again in a second so we'll do x path and then dynamic content xml and here is our xpath expression but as you can see at the moment is hard coded to a so what i'm going to do is inject the current item i need to wrap that in a concat i'm not sure if this is right now so i'm just going to put it into notepad length x path output xml concat yeah it looks about right nope one more of those so let's just see what that gives us it might work might not okay so our inputs were a and b and so the selectors looped around twice once for a once for b completed the search for a and found three and completed the search for b and found one and that is how you can count the number of distinct items without a loop and it should finish very very quickly so i hope that is helpful to the person asked the question prop 3 go and that you managed to improve the performance of your flow for everybody else i hope this has been another useful demonstration of the x-path expression it is actually one of the most powerful expressions in power automate but it is sort of neglected because it is not particularly friendly when you first start to use it but you can always use xpath to make it easier for you to test out your xpath queries before you decide to put it into power automate so i'll see you in the next video and best of luck building your flows
Info
Channel: Paul Murana
Views: 18,073
Rating: undefined out of 5
Keywords: Power Automate, Microsoft Flow, xpath
Id: v8B3zZDvs5g
Channel Id: undefined
Length: 7min 20sec (440 seconds)
Published: Wed Feb 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.