cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
rubenvanleusden
Regular Visitor

Need help with creating a Convert-Size Power Automate action

Hi,

I'm having trouble wrapping my head around a way to convert a function I have in PowerShell to something that can be used in my Flow.

 

The function:

 

 

function Convert-Size {
	[cmdletbinding()]
	param(
		[validateset('Bytes', 'KB', 'MB', 'GB', 'TB')]
		[string]$From,
		[validateset('Bytes', 'KB', 'MB', 'GB', 'TB')]
		[string]$To,
		[Parameter(Mandatory = $true)]
		[double]$Value,
		[int]$Precision = 4
	)
	switch ($From) {
		'Bytes' { $value = $Value }
		'KB' { $value = $Value * 1024 }
		'MB' { $value = $Value * 1024 * 1024 }
		'GB' { $value = $Value * 1024 * 1024 * 1024 }
		'TB' { $value = $Value * 1024 * 1024 * 1024 * 1024 }
	}

	switch ($To) {
		'Bytes' { return $value }
		'KB' { $Value = $Value / 1KB }
		'MB' { $Value = $Value / 1MB }
		'GB' { $Value = $Value / 1GB }
		'TB' { $Value = $Value / 1TB }

	}
	return [Math]::Round($value, $Precision, [MidPointRounding]::AwayFromZero)
}

 

 

 

which allows me to enter:

 

 

Convert-Size -From GB -To TB -Value 981.6 -Precision 2

 

 

Which will return "0.9"

 

It could be my brain not being able to function properly right now, but I have absolutely no clue how to even begin to achieve this.

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

@rubenvanleusden Sorry for the late reply on this. See example that might get what you're looking for. It's not a function per-se but might get the result. You might want to do some testing on the results to ensure they're accurate/correct.

 

See full flow below. I'll go into each of the actions.

grantjenkins_0-1673698170251.png

 

Params is a Compose that contains the From, To and Precision to be used.

{
    "From": "GB",
    "To": "TB",
    "Precision": 4
}

grantjenkins_2-1673698263304.png

 

Convert is a Compose that's used as a conversion table for both the From and To. Each of the values are incrementally multiplied by 1024.

{
    "Bytes": 1,
    "KB": 1024,
    "MB": 1048576,
    "GB": 1073741824,
    "TB": 1099511627776
}

grantjenkins_3-1673698317588.png

 

Initialize variable creates an array called data that contains some sample data for testing.

[
    {
        "Number": 981.6
    },
    {
        "Number": 1024
    }
]

grantjenkins_4-1673698361513.png

 

Select uses the output from our data array, then maps our values, converting them using the Params and Convert values. The expression used is below:

formatNumber(div(mul(item()?['Number'], outputs('Convert')?[outputs('Params')?['From']]), outputs('Convert')?[outputs('Params')?['To']]), concat('N', outputs('Params')?['Precision']))

 

If I try to format the expression, we can see what's happening a bit better. Effectively, we are multiplying the original value with the value for From, then dividing the result of that by the value for To. We're then wrapping that into a formatNumber expression to format it using the value from Precision.

formatNumber(
    div(
        mul(
            item()?['Number'], 
            outputs('Convert')?[outputs('Params')?['From']]
        ), 
        outputs('Convert')?[outputs('Params')?['To']]
    ), 
    concat('N', outputs('Params')?['Precision'])
)

 

grantjenkins_5-1673698746217.png

 

After running the flow, we would get the following output.

grantjenkins_6-1673698796591.png

 

Hopefully this is what you're after.


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.

View solution in original post

5 REPLIES 5
grantjenkins
Super User
Super User

@rubenvanleusden Are you looking to do this just within a single flow, or as a function (of sorts) that you can use across multiple flows?


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.

At the moment I need it within a single flow!

rubenvanleusden
Regular Visitor

Currently my flow is build like this:

 

  • When a new email arrives
  • Get the HTML content and extract the <table></table> from the email body
  • Clean up the HTML for further processing (removes styling etc)
  • Compose: xml(outputs('htmlTable'))
  • Compose: xpath(outputs('xmlTable'), '//tr')
  • Select: From: skip(outputs('tableRows'), 1) (To skip the header row)
    • Map: Column Names - xpath(item(),'//td[5]//text()')

Where "xpath(item(),'//td[5]//text()')" would contain the value I'd like to be able to convert as explained in my original post.

@rubenvanleusden Sorry for the late reply on this. See example that might get what you're looking for. It's not a function per-se but might get the result. You might want to do some testing on the results to ensure they're accurate/correct.

 

See full flow below. I'll go into each of the actions.

grantjenkins_0-1673698170251.png

 

Params is a Compose that contains the From, To and Precision to be used.

{
    "From": "GB",
    "To": "TB",
    "Precision": 4
}

grantjenkins_2-1673698263304.png

 

Convert is a Compose that's used as a conversion table for both the From and To. Each of the values are incrementally multiplied by 1024.

{
    "Bytes": 1,
    "KB": 1024,
    "MB": 1048576,
    "GB": 1073741824,
    "TB": 1099511627776
}

grantjenkins_3-1673698317588.png

 

Initialize variable creates an array called data that contains some sample data for testing.

[
    {
        "Number": 981.6
    },
    {
        "Number": 1024
    }
]

grantjenkins_4-1673698361513.png

 

Select uses the output from our data array, then maps our values, converting them using the Params and Convert values. The expression used is below:

formatNumber(div(mul(item()?['Number'], outputs('Convert')?[outputs('Params')?['From']]), outputs('Convert')?[outputs('Params')?['To']]), concat('N', outputs('Params')?['Precision']))

 

If I try to format the expression, we can see what's happening a bit better. Effectively, we are multiplying the original value with the value for From, then dividing the result of that by the value for To. We're then wrapping that into a formatNumber expression to format it using the value from Precision.

formatNumber(
    div(
        mul(
            item()?['Number'], 
            outputs('Convert')?[outputs('Params')?['From']]
        ), 
        outputs('Convert')?[outputs('Params')?['To']]
    ), 
    concat('N', outputs('Params')?['Precision'])
)

 

grantjenkins_5-1673698746217.png

 

After running the flow, we would get the following output.

grantjenkins_6-1673698796591.png

 

Hopefully this is what you're after.


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.

Thank you so much for taking the time and help me out!

 

Your explanation has thought me a lot more than just the answer/solution to my original question.

 

Very very much appreciated, sir!

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Users online (2,535)