3

I have a dynamic formula that needs calculation.

Here's a sample (var1 * var2) / (var3 * (23 * 100))

These variables will be replaced with actual numbers because they are user input. Currently, I am using power automate flow that connects to my web API to throw any calculation and solve it. then send back the result. I am using NCalc and/or DataTable on the API.

What I was hoping to do was to pass the calculation in any Control like label or text. If you put 1+1 to the Text property of a Label Control. It will show the number 2 right away but since I am passing string it doesn't work. I can use something like this Value("100" * "300") and this works as long as we stick with math operators only but fails when we have the open and close parentheses like this (21 * 100) / (0.43 * (23 * 100))

I will appreciate it if you have a workaround in mind. Thank you in advance.

Crismogram
  • 721
  • 11
  • 22
  • this question and answers on it https://stackoverflow.com/questions/333737/evaluating-string-342-yield-int-18 discuss broadly how to evaluate string based math operations, basically they're all using NCalc / DataTable in .NET to achieve the desired result. Unfortunately there's nothing equivalent in PowerApps canvas. Best guess would be implementing something like the [Reverse Polish Notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) but even then PowerApps isn't the right tool for that task. – kshkarin Mar 28 '21 at 07:51

4 Answers4

0

You can handle all calculations with variables without using Power Automate.

Example:

Create a TextInput and in the 'onChange' function insert

Set(var1;Value(TextInput1.Text))

Create a second TextInput and in the 'onChange' function insert

Set(var2;Value(TextInput2.Text))

Create a Label and in the Text function insert

var1 + var2

Every time, you change the values in the TextInputfields, the Label will be updated.

Reiner Fischer
  • 129
  • 1
  • 7
0

This was only a simple example. Enclosed you find one of my calculations, which is written in the text property of a Textfield including Parenthesis:

Round((var_Moment - (var_FuelArm * Slider_Fuel.Value)) / (var_GM -Slider_Fuel.Value );0) & " mm"

It works not only with variables, but also with expressions from other controls.

Because you are passing strings, you have to convert them to Values and assign them to variables.

Set(var1;Value(your String Value))

That was the point, i tried to highlight in my first answer.

Reiner Fischer
  • 129
  • 1
  • 7
  • thanks for another answer. all math operators including the parentheses are part of the string. As mentioned in the question these calculations are dynamic. – Crismogram Mar 29 '21 at 08:22
0

Here's a solution that hopefully works for you, it uses the free mathjs API to perform the calculation. PowerApps sends the user input to a PowerAutomate flow via a button trigger.

Demo of the app: enter image description here

PowerAutomate Flow steps:

  1. PowerApps Trigger
  2. Initialize Variable -> Value: "Ask in PowerApps"
  3. HTTP
  4. Respond to a PowerApp or flow -> Value: body('HTTP')['Body']

enter image description here

PowerApps Button property OnSelect creates a variable and triggers the flow with the user input as a parameter to receive a response:

UpdateContext({ var_Result: PowerAppsHTTPRequestCalculation.Run(tb_Input.Text).result})

PowerApps Text Label property Text to display the result value:

var_Result

kshkarin
  • 488
  • 4
  • 9
  • Thank you for your answer, This is my current approach but mine uses my own hosted web api which works well but I am avoiding this due to optimization. What I wanted was to do the calculation on the canvas app. – Crismogram Mar 29 '21 at 08:25
  • @Crismogram there's no way to achieve this out of the box with PowerApps Canvas only, what's the optimization goal? is it too slow? the mathjs API is surprisingly fast. – kshkarin Mar 29 '21 at 08:47
0

I finally solve this issue by using a PCF Control and since it enables the use of JavaScript I can simply use the eval function and solve any equation you throw at it.

more info here: https://docs.microsoft.com/en-us/powerapps/developer/component-framework/create-custom-controls-using-pcf

Crismogram
  • 721
  • 11
  • 22