cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
david_ashworth
Frequent Visitor

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
Community Champion
Community Champion

@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
Community Champion
Community Champion

@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

Helpful resources

Announcements
MPA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (4,270)