Solve Math Equation(string) in Canvas App

Viewed 589

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.

4 Answers

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.

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.

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

Related