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.
Solved! Go to Solution.
@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.
Params is a Compose that contains the From, To and Precision to be used.
{
"From": "GB",
"To": "TB",
"Precision": 4
}
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
}
Initialize variable creates an array called data that contains some sample data for testing.
[
{
"Number": 981.6
},
{
"Number": 1024
}
]
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'])
)
After running the flow, we would get the following output.
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.
@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?
At the moment I need it within a single flow!
Currently my flow is build like this:
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.
Params is a Compose that contains the From, To and Precision to be used.
{
"From": "GB",
"To": "TB",
"Precision": 4
}
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
}
Initialize variable creates an array called data that contains some sample data for testing.
[
{
"Number": 981.6
},
{
"Number": 1024
}
]
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'])
)
After running the flow, we would get the following output.
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.
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!
User | Count |
---|---|
89 | |
37 | |
26 | |
13 | |
12 |
User | Count |
---|---|
127 | |
54 | |
38 | |
24 | |
21 |