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

Extracting certain data from initialize variable

Hi guys,

 

I am trying to extract certain bits of information from my flow. Basically the flow starts a runbook which extracts information about license usage in the tenant. I am trying to extract the number of licenses available  vs the number of licenses used. This is the overall Flow:

 

david_ashworth_0-1629262741816.png

 

This is the section i want to extract information from:

 

david_ashworth_4-1629263037397.png

 

In particular i want 2 bits of info from the Value output:

 

david_ashworth_3-1629262977366.png

 

ConsumedUnits : 1

PrepaidUnits - Enabled: 2

 

That way i can tell the tenant has 2 licenses, 1 is used therefore 1 is available. How would i extract those 2 bits of information so i can email the details?

 

Thanks,

David

 

1 ACCEPTED SOLUTION

Accepted Solutions
poweractivate
Super User
Super User

@david_ashworth 

 

You can try something like this:

 

Presuming an input somewhat like this:

82021-class-00.png

 

1. You can Initialize a variable for each of the 2 bits of info you want:

consumedUnits

enabledPrepaidUnits

 

 

Make them both Integer for the Type. You don't have to if it doesn't make sense for your scenario (e.g. if String makes more sense) but I presume for now the data type of the Variables being Integer makes most sense and may even be required.

 

Leave Value blank for both of them.

 

82021-class-01.png

 

2. You can use a Compose block and an expression to split the whole thing by the new line character

 

split(outputs('Compose'),decodeUriComponent('%0A'))

 

 

In Power Automate, the expression in a Flow to explicitly specify a newline character is

 

decodeUriComponent('%0A')

 

and

 

split(outputs('Compose'),decodeUriComponent('%0A'))

 

 

 

The "split" function converts a string given in the first argument (in this case, the original input), into a resulting Array output, where each element of Array is split up (delimited) by the string in the given second argument (in this case, that would be decodeUriComponent('%0A') or in other words, a "new line" character in Power Automate).

 

Like below:

 

82021-class-02.png

 

 

3. Now you can enter into an Apply to Each using the Compose Action Block output, that should have what is now an Array, where each item is each line in sequence from the original input:

82021-class-03.png

 

 

Now inside the Apply to Each:

 

4. First, a Compose can be defined here to split the line by the colon (:) character, as follows:

 

split(items('Apply_to_each'),':')

 

 

82021-class-04.png

 

Next, you can enter into a Switch which has two cases. For now, for the sake of simplicity, the two cases will be consumedunits and enabled. Going over each step, that would be:

 

5. Enter into a Switch  (yes, this is still inside the Apply to Each) and then for the value of "On" use this Expression:

 

toLower(trim(outputs('Compose_3')[0]))

 

 

To break it down,

the meaning of 

 

outputs('Compose_3')[0]

 

 

is that since now Compose_3 should contain an Array which is that particular line split by the colon (:) character, the element 0 should contain the left side. If using [1] that would refer to the right side. It is presumed for now and for simplicity, that we are only interested in those lines which have Key : Value like syntax (if any line does not have that format, it's fine - there won't be any error, you'll see later why).

 

Please note that for good practice, it may be better to use the syntax ?[0] rather than [0] as I am using here, as the question mark in the expression prefixing the index access may result in the index access attempt being more tolerant to unexpected cases such as a "completely empty value" for "outputs('Compose_3')" and to at least return an empty value rather than to just error out in some cases - for simplicity and for now, this question mark syntax is not used, and with the test run (see end of post) this did not cause any error anyway.

 

trim(outputs('Compose_3')[0])

 

The above "trim" function will remove leading and trailing white space to make sure any comparisons later won't fail due to leading or trailing whitespace related reasons, and finally

 

toLower(trim(outputs('Compose_3')[0]))

 

The above "toLower"  function will make it all lower-case to make sure any comparisons later won't fail due to potential case-sensitivity related reasons.

 

 

So you can enter into the switch like this:

82021-class-05.png

 

Even if some of the lines don't even have a colon - that's fine - the Switch will just flow to the Default case and do nothing, which is exactly fine for these purposes.

 

 

Here is the content of each Case:

 

6. For the condition of the first Case, i.e. the field that says "Equals" you can have:

consumedunits

Yes, it is all lowercase and with no trailing and leading whitespace - due to the expression used in the Switch On itself previously, the comparison will be case insensitive and should work no matter how many trailing or leading spaces there were during the split.

 

 You can add a Set variable Action block,

for the Name select the following from the dropdown:

consumedUnits

(Note: this is the name of one of the variables defined in the Initialize Variable in Step 1 at the top, and it should be selected from the dropdown under 'Name' for the 'Set Variable' Action block)

and for the Value, use

 

int(trim(outputs('Compose_3')[1]))

 

"trim" removes leading and trailing whitespace.

 

The reason for the outer wrapped function "int" is to convert the result to an Integer - since remember the variable was initialized as an Integer. Not using the int expression here, will cause in an error in the Flow run, because just the trim will still make it tobe left as a string if the "int" function is not wrapped there, which takes that String and converts it to an Integer.

 

As mentioned before, [0] was to access the left side - now [1] is used to access the right side, or in other words, the actual value of the bit of data that is desired!

 

82021-class-06.png

 

 

7. To the right the ellipses on the "Case" Action block, there should be a circle with a plus sign. Click that to add another Case.

There should now be a block named Case 2

Do something almost identical the previous step - use a Set variable block - use the same Value 

but this time, use this for the "Equals" use this:

enabled

And yes, all lowercase like before.

Also, for the "Name" select the following value from the dropdown

enabledPrepaidUnits

(Note: this is the name of one of the variables defined in the Initialize Variable in Step 1 at the top, and it should be selected from the dropdown under 'Name' for the 'Set Variable 2' Action block)

 

See below for example:

 

82021-class-07.png

 Now, outside of the Switch and outside of the Apply to Each:

8. Finally, you can add two Compose blocks (outside of the Switch and outside of the Apply to Each) at the very end of the Flow to clearly check if it is really working.

 

82021-class-08.png

 

9. When testing my above Flow, I get the expected values for the Variables based on the Compose 4 and 5 :

2 for consumedUnits, and

1 for enabledPrepaidUnits:

 

For consumedUnits (Compose 4) and prepaidEnabledUnits (Compose 5) shown below, test run results:

 

82021-class-09.png

 

 

As you can see, it is working correctly. 

 

Despite the custom format of the data based on the result of a runbook making it seem like it would be impossible to extract any data, you can use Power Automate and something like my approach above to go through it and extract the data you want seamlessly like this.

 

There is one thing to note: 

 

For now I simplified the PrepaidUnits - Enabled: 2 part.

For example, what happens if you want to extract something else later that's called "Enabled"?

For simplification purposes, I did not cover it in this post, so you may need to adjust that part for your scenario.

 

For now, here are some possible approaches to consider if you really do need to make it more complex for the  PrepaidUnits - Enabled part:

 

Potential Approaches - knowing when you have entered a "class" in your data format

 

Suppose you have one or more lines later with "Enabled" on the left side of a colon - that could pose an issue and the above solution may not suffice. If so, you may have to use additional functionality in the Flow - such as using another variable at the top to keep track when you "enter" and "exit" a "class" and that actually keeps track of the name of the current class entered (you can potentially determine if you entered one by checking if the "right side" of the line contains, e.g. "class licenseunitsdetail", etc. - then using split, etc. to just get that name and set it in the variable - checking for right side of the line only containing a } to check if it exits, etc. - and setting an appropriate variable. What you can then do is something like perhaps having an "outer switch" that branches to different switch based on which "class" it is in, or if it is not in a class use the similar branch kind of like the one I already gave here.

 

If you are able to rely on the specific order of the information in this specific case, and you still need a way to have multiple bits of data that are called "Enabled" across multiple lines but want to avoid the complexity of the above idea, then you might prefer to do a "semi-easier" way for now and just presume that the first occurrence of Enabled must be LicenseUnitsDetail, the second occurrence must be Y, third occurrence must be Z, etc. You would basically do the same as the above except using a hard-coded Array (perhaps a Compose) and the variable can be the index of the occurrence of, say, "Enabled", etc.  In fact, this may be a good idea to do it first this way to test before doing the above way. In case it suffices, it can just be used - if it does not suffice, it can be a way to test the above idea without implementing it all the way at first - then doing the above after that.

 

 

By the way:

To clarify, the steps given above are in order.

In case it helps, here is a 10,000 foot view of the completed Flow:

 

82021-class-10000.png

 

And a 1,000 foot view of what's in just the Apply to Each (with the Switch collapsed / folded in):

82021-class-1000.png

 

 

 

 

Check if it helps @david_ashworth 

 

View solution in original post

1 REPLY 1
poweractivate
Super User
Super User

@david_ashworth 

 

You can try something like this:

 

Presuming an input somewhat like this:

82021-class-00.png

 

1. You can Initialize a variable for each of the 2 bits of info you want:

consumedUnits

enabledPrepaidUnits

 

 

Make them both Integer for the Type. You don't have to if it doesn't make sense for your scenario (e.g. if String makes more sense) but I presume for now the data type of the Variables being Integer makes most sense and may even be required.

 

Leave Value blank for both of them.

 

82021-class-01.png

 

2. You can use a Compose block and an expression to split the whole thing by the new line character

 

split(outputs('Compose'),decodeUriComponent('%0A'))

 

 

In Power Automate, the expression in a Flow to explicitly specify a newline character is

 

decodeUriComponent('%0A')

 

and

 

split(outputs('Compose'),decodeUriComponent('%0A'))

 

 

 

The "split" function converts a string given in the first argument (in this case, the original input), into a resulting Array output, where each element of Array is split up (delimited) by the string in the given second argument (in this case, that would be decodeUriComponent('%0A') or in other words, a "new line" character in Power Automate).

 

Like below:

 

82021-class-02.png

 

 

3. Now you can enter into an Apply to Each using the Compose Action Block output, that should have what is now an Array, where each item is each line in sequence from the original input:

82021-class-03.png

 

 

Now inside the Apply to Each:

 

4. First, a Compose can be defined here to split the line by the colon (:) character, as follows:

 

split(items('Apply_to_each'),':')

 

 

82021-class-04.png

 

Next, you can enter into a Switch which has two cases. For now, for the sake of simplicity, the two cases will be consumedunits and enabled. Going over each step, that would be:

 

5. Enter into a Switch  (yes, this is still inside the Apply to Each) and then for the value of "On" use this Expression:

 

toLower(trim(outputs('Compose_3')[0]))

 

 

To break it down,

the meaning of 

 

outputs('Compose_3')[0]

 

 

is that since now Compose_3 should contain an Array which is that particular line split by the colon (:) character, the element 0 should contain the left side. If using [1] that would refer to the right side. It is presumed for now and for simplicity, that we are only interested in those lines which have Key : Value like syntax (if any line does not have that format, it's fine - there won't be any error, you'll see later why).

 

Please note that for good practice, it may be better to use the syntax ?[0] rather than [0] as I am using here, as the question mark in the expression prefixing the index access may result in the index access attempt being more tolerant to unexpected cases such as a "completely empty value" for "outputs('Compose_3')" and to at least return an empty value rather than to just error out in some cases - for simplicity and for now, this question mark syntax is not used, and with the test run (see end of post) this did not cause any error anyway.

 

trim(outputs('Compose_3')[0])

 

The above "trim" function will remove leading and trailing white space to make sure any comparisons later won't fail due to leading or trailing whitespace related reasons, and finally

 

toLower(trim(outputs('Compose_3')[0]))

 

The above "toLower"  function will make it all lower-case to make sure any comparisons later won't fail due to potential case-sensitivity related reasons.

 

 

So you can enter into the switch like this:

82021-class-05.png

 

Even if some of the lines don't even have a colon - that's fine - the Switch will just flow to the Default case and do nothing, which is exactly fine for these purposes.

 

 

Here is the content of each Case:

 

6. For the condition of the first Case, i.e. the field that says "Equals" you can have:

consumedunits

Yes, it is all lowercase and with no trailing and leading whitespace - due to the expression used in the Switch On itself previously, the comparison will be case insensitive and should work no matter how many trailing or leading spaces there were during the split.

 

 You can add a Set variable Action block,

for the Name select the following from the dropdown:

consumedUnits

(Note: this is the name of one of the variables defined in the Initialize Variable in Step 1 at the top, and it should be selected from the dropdown under 'Name' for the 'Set Variable' Action block)

and for the Value, use

 

int(trim(outputs('Compose_3')[1]))

 

"trim" removes leading and trailing whitespace.

 

The reason for the outer wrapped function "int" is to convert the result to an Integer - since remember the variable was initialized as an Integer. Not using the int expression here, will cause in an error in the Flow run, because just the trim will still make it tobe left as a string if the "int" function is not wrapped there, which takes that String and converts it to an Integer.

 

As mentioned before, [0] was to access the left side - now [1] is used to access the right side, or in other words, the actual value of the bit of data that is desired!

 

82021-class-06.png

 

 

7. To the right the ellipses on the "Case" Action block, there should be a circle with a plus sign. Click that to add another Case.

There should now be a block named Case 2

Do something almost identical the previous step - use a Set variable block - use the same Value 

but this time, use this for the "Equals" use this:

enabled

And yes, all lowercase like before.

Also, for the "Name" select the following value from the dropdown

enabledPrepaidUnits

(Note: this is the name of one of the variables defined in the Initialize Variable in Step 1 at the top, and it should be selected from the dropdown under 'Name' for the 'Set Variable 2' Action block)

 

See below for example:

 

82021-class-07.png

 Now, outside of the Switch and outside of the Apply to Each:

8. Finally, you can add two Compose blocks (outside of the Switch and outside of the Apply to Each) at the very end of the Flow to clearly check if it is really working.

 

82021-class-08.png

 

9. When testing my above Flow, I get the expected values for the Variables based on the Compose 4 and 5 :

2 for consumedUnits, and

1 for enabledPrepaidUnits:

 

For consumedUnits (Compose 4) and prepaidEnabledUnits (Compose 5) shown below, test run results:

 

82021-class-09.png

 

 

As you can see, it is working correctly. 

 

Despite the custom format of the data based on the result of a runbook making it seem like it would be impossible to extract any data, you can use Power Automate and something like my approach above to go through it and extract the data you want seamlessly like this.

 

There is one thing to note: 

 

For now I simplified the PrepaidUnits - Enabled: 2 part.

For example, what happens if you want to extract something else later that's called "Enabled"?

For simplification purposes, I did not cover it in this post, so you may need to adjust that part for your scenario.

 

For now, here are some possible approaches to consider if you really do need to make it more complex for the  PrepaidUnits - Enabled part:

 

Potential Approaches - knowing when you have entered a "class" in your data format

 

Suppose you have one or more lines later with "Enabled" on the left side of a colon - that could pose an issue and the above solution may not suffice. If so, you may have to use additional functionality in the Flow - such as using another variable at the top to keep track when you "enter" and "exit" a "class" and that actually keeps track of the name of the current class entered (you can potentially determine if you entered one by checking if the "right side" of the line contains, e.g. "class licenseunitsdetail", etc. - then using split, etc. to just get that name and set it in the variable - checking for right side of the line only containing a } to check if it exits, etc. - and setting an appropriate variable. What you can then do is something like perhaps having an "outer switch" that branches to different switch based on which "class" it is in, or if it is not in a class use the similar branch kind of like the one I already gave here.

 

If you are able to rely on the specific order of the information in this specific case, and you still need a way to have multiple bits of data that are called "Enabled" across multiple lines but want to avoid the complexity of the above idea, then you might prefer to do a "semi-easier" way for now and just presume that the first occurrence of Enabled must be LicenseUnitsDetail, the second occurrence must be Y, third occurrence must be Z, etc. You would basically do the same as the above except using a hard-coded Array (perhaps a Compose) and the variable can be the index of the occurrence of, say, "Enabled", etc.  In fact, this may be a good idea to do it first this way to test before doing the above way. In case it suffices, it can just be used - if it does not suffice, it can be a way to test the above idea without implementing it all the way at first - then doing the above after that.

 

 

By the way:

To clarify, the steps given above are in order.

In case it helps, here is a 10,000 foot view of the completed Flow:

 

82021-class-10000.png

 

And a 1,000 foot view of what's in just the Apply to Each (with the Switch collapsed / folded in):

82021-class-1000.png

 

 

 

 

Check if it helps @david_ashworth 

 

Helpful resources

Announcements

Power Platform Connections - Episode 7 | March 30, 2023

Episode Seven of Power Platform Connections sees David Warner and Hugo Bernier talk to Dian Taylor, alongside the latest news, product reviews, and community blogs.     Use the hashtag #PowerPlatformConnects on social media for a chance to have your work featured on the show.  

Announcing | Super Users - 2023 Season 1

Super Users – 2023 Season 1    We are excited to kick off the Power Users Super User Program for 2023 - Season 1.  The Power Platform Super Users have done an amazing job in keeping the Power Platform communities helpful, accurate and responsive. We would like to send these amazing folks a big THANK YOU for their efforts.      Super User Season 1 | Contributions July 1, 2022 – December 31, 2022  Super User Season 2 | Contributions January 1, 2023 – June 30, 2023    Curious what a Super User is? Super Users are especially active community members who are eager to help others with their community questions. There are 2 Super User seasons in a year, and we monitor the community for new potential Super Users at the end of each season. Super Users are recognized in the community with both a rank name and icon next to their username, and a seasonal badge on their profile.  Power Apps  Power Automate  Power Virtual Agents  Power Pages  Pstork1*  Pstork1*  Pstork1*  OliverRodrigues  BCBuizer  Expiscornovus*  Expiscornovus*  ragavanrajan  AhmedSalih  grantjenkins  renatoromao    Mira_Ghaly*  Mira_Ghaly*      Sundeep_Malik*  Sundeep_Malik*      SudeepGhatakNZ*  SudeepGhatakNZ*      StretchFredrik*  StretchFredrik*      365-Assist*  365-Assist*      cha_cha  ekarim2020      timl  Hardesh15      iAm_ManCat  annajhaveri      SebS  Rhiassuring      LaurensM  abm      TheRobRush  Ankesh_49      WiZey  lbendlin      Nogueira1306  Kaif_Siddique      victorcp  RobElliott      dpoggemann  srduval      SBax  CFernandes      Roverandom  schwibach      Akser  CraigStewart      PowerRanger  MichaelAnnis      subsguts  David_MA      EricRegnier  edgonzales      zmansuri  GeorgiosG      ChrisPiasecki  ryule      AmDev  fchopo      phipps0218  tom_riha      theapurva  takolota     Akash17  momlo     BCLS776  Shuvam-rpa     rampprakash  ScottShearer     Rusk  ChristianAbata     cchannon  Koen5     a33ik  Heartholme     AaronKnox  okeks      Matren   David_MA     Alex_10        Jeff_Thorpe        poweractivate        Ramole        DianaBirkelbach        DavidZoon        AJ_Z        PriyankaGeethik        BrianS        StalinPonnusamy        HamidBee        CNT        Anonymous_Hippo        Anchov        KeithAtherton        alaabitar        Tolu_Victor        KRider        sperry1625        IPC_ahaas      zuurg    rubin_boer   cwebb365   Dorrinda   G1124   Gabibalaban   Manan-Malhotra   jcfDaniel   WarrenBelz   Waegemma   drrickryp   GuidoPreite    If an * is at the end of a user's name this means they are a Multi Super User, in more than one community. Please note this is not the final list, as we are pending a few acceptances.  Once they are received the list will be updated. 

Register now for the Business Applications Launch Event | Tuesday, April 4, 2023

Join us for an in-depth look into the latest updates across Microsoft Dynamics 365 and Microsoft Power Platform that are helping businesses overcome their biggest challenges today.   Find out about new features, capabilities, and best practices for connecting data to deliver exceptional customer experiences, collaborating, and creating using AI-powered capabilities, driving productivity with automation—and building towards future growth with today’s leading technology.   Microsoft leaders and experts will guide you through the full 2023 release wave 1 and how these advancements will help you: Expand visibility, reduce time, and enhance creativity in your departments and teams with unified, AI-powered capabilities.Empower your employees to focus on revenue-generating tasks while automating repetitive tasks.Connect people, data, and processes across your organization with modern collaboration tools.Innovate without limits using the latest in low-code development, including new GPT-powered capabilities.    Click Here to Register Today!    

Check out the new Power Platform Communities Front Door Experience!

We are excited to share the ‘Power Platform Communities Front Door’ experience with you!   Front Door brings together content from all the Power Platform communities into a single place for our community members, customers and low-code, no-code enthusiasts to learn, share and engage with peers, advocates, community program managers and our product team members. There are a host of features and new capabilities now available on Power Platform Communities Front Door to make content more discoverable for all power product community users which includes ForumsUser GroupsEventsCommunity highlightsCommunity by numbersLinks to all communities Users can see top discussions from across all the Power Platform communities and easily navigate to the latest or trending posts for further interaction. Additionally, they can filter to individual products as well.   Users can filter and browse the user group events from all power platform products with feature parity to existing community user group experience and added filtering capabilities.     Users can now explore user groups on the Power Platform Front Door landing page with capability to view all products in Power Platform.      Explore Power Platform Communities Front Door today. Visit Power Platform Community Front door to easily navigate to the different product communities, view a roll up of user groups, events and forums.

Microsoft Power Platform Conference | Registration Open | Oct. 3-5 2023

We are so excited to see you for the Microsoft Power Platform Conference in Las Vegas October 3-5 2023! But first, let's take a look back at some fun moments and the best community in tech from MPPC 2022 in Orlando, Florida.   Featuring guest speakers such as Charles Lamanna, Heather Cook, Julie Strauss, Nirav Shah, Ryan Cunningham, Sangya Singh, Stephen Siciliano, Hugo Bernier and many more.   Register today: https://www.powerplatformconf.com/   

Users online (3,273)