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
Most Valuable Professional
Most Valuable Professional

@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
Most Valuable Professional
Most Valuable Professional

@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

Celebrating the May Super User of the Month: Laurens Martens

  @LaurensM  is an exceptional contributor to the Power Platform Community. Super Users like Laurens inspire others through their example, encouragement, and active participation. We are excited to celebrated Laurens as our Super User of the Month for May 2024.   Consistent Engagement:  He consistently engages with the community by answering forum questions, sharing insights, and providing solutions. Laurens dedication helps other users find answers and overcome challenges.   Community Expertise: As a Super User, Laurens plays a crucial role in maintaining a knowledge sharing environment. Always ensuring a positive experience for everyone.   Leadership: He shares valuable insights on community growth, engagement, and future trends. Their contributions help shape the Power Platform Community.   Congratulations, Laurens Martens, for your outstanding work! Keep inspiring others and making a difference in the community!   Keep up the fantastic work!        

Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!    

Welcome to the Power Automate Community

You are now a part of a fast-growing vibrant group of peers and industry experts who are here to network, share knowledge, and even have a little fun.   Now that you are a member, you can enjoy the following resources:   Welcome to the Community   News & Announcements: The is your place to get all the latest news around community events and announcements. This is where we share with the community what is going on and how to participate.  Be sure to subscribe to this board and not miss an announcement.   Get Help with Power Automate Forums: If you're looking for support with any part of Power Automate, our forums are the place to go. From General Power Automate forums to Using Connectors, Building Flows and Using Flows.  You will find thousands of technical professionals, and Super Users with years of experience who are ready and eager to answer your questions. You now have the ability to post, reply and give "kudos" on the Power Automate community forums. Make sure you conduct a quick search before creating a new post because your question may have already been asked and answered. Galleries: The galleries are full of content and can assist you with information on creating a flow in our Webinars and Video Gallery, and the ability to share the flows you have created in the Power Automate Cookbook.  Stay connected with the Community Connections & How-To Videos from the Microsoft Community Team. Check out the awesome content being shared there today.   Power Automate Community Blog: Over the years, more than 700 Power Automate Community Blog articles have been written and published by our thriving community. Our community members have learned some excellent tips and have keen insights on the future of process automation. In the Power Automate Community Blog, you can read the latest Power Automate-related posts from our community blog authors around the world. Let us know if you'd like to become an author and contribute your own writing — everything Power Automate-related is welcome.   Community Support: Check out and learn more about Using the Community for tips & tricks. Let us know in the Community Feedback  board if you have any questions or comments about your community experience. Again, we are so excited to welcome you to the Microsoft Power Automate community family. Whether you are brand new to the world of process automation or you are a seasoned Power Automate veteran - our goal is to shape the community to be your 'go to' for support, networking, education, inspiration and encouragement as we enjoy this adventure together.     Power Automate Community Team

Hear what's next for the Power Up Program

Hear from Principal Program Manager, Dimpi Gandhi, to discover the latest enhancements to the Microsoft #PowerUpProgram, including a new accelerated video-based curriculum crafted with the expertise of Microsoft MVPs, Rory Neary and Charlie Phipps-Bennett. If you’d like to hear what’s coming next, click the link below to sign up today! https://aka.ms/PowerUp  

Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!

Users online (3,446)