Power Automate: Do While Loop | Calculate Power Of


The ask, create a Power Automate cloud flow to calculate exponentiation, a base value taken to the power of an exponent value. Ultimately, this flow will be one of many flows filling in where Microsoft hasn’t yet provided an expression.

For this ask, create a new cloud flow and choose “When Power Apps calls a flow (V2)” as the trigger. This will allow other cloud flows and canvas apps in the solution to call this flow as a child flow, essentially outsourcing the work:

Figure 1 - Power Automate cloud flow creation.
Figure 1 – Power Automate cloud flow creation.

An additional perk of using this trigger is that it allows flow makers to require input, like parameters of a function. To calculate exponentiation and return a result, what parameters should be provided? The Base Number and the Exponent Number:

Figure 2 - Power Automate cloud flow trigger for Power Apps action.
Figure 2 – Power Automate cloud flow trigger for Power Apps action.

Now, with the flow trigger configured, create four flow variables:

  • loopCounter is an integer for use within the “Do Until” loop to be added. Its default value should be 1.
  • tempInput is also an integer. For its default value, parse the Exponent Number parameter using the int() expression.
  • tempProduct is a float with no default value.
  • finalProduct is also a float, but for its default value, assign the Base Number parameter.

.

Figure 3 - Power Automate cloud flow trigger and variables.
Figure 3 – Power Automate cloud flow trigger and variables.

With the early prep work completed, calculate the exponentiation using the Base Number and Exponent Number parameters. Add a “Switch” block and pass in the tempInput variable as the On parameter, then add two cases. If the exponent is 0, then return 1. If the exponent is 1, then return the Base Number. Otherwise, under the “Default” path, add a “Do Until” loop and within this loop, increment the loopCounter, set the tempProduct, and set the finalProduct:

Figure 4 - Power Automate cloud flow with "Switch" block.
Figure 4 – Power Automate cloud flow with “Switch” block.

The “Do Until” block should iterate until the loopCounter variable is equal to the flow’s Exponent Number. The loopCounter variable was initialized outside of the loop but is incremented by 1 each loop iteration.

Because Power Automate is low-code, variables can’t be calculated and self-assigned in the same action, as with pro-code. The workaround, perform the calculation in two steps. First, assign the value of finalProduct to tempProduct variable, then assign the product of tempProduct and the Base Number to the finalProduct variable:

  • mul( triggerBody()?[‘number’], variables(‘tempProduct’) )
Figure 5 - Power Automate cloud flow "Do Until" parameters.
Figure 5 – Power Automate cloud flow “Do Until” parameters.

Lastly, add the “Respond to a Power App or flow” action to the end of this flow, then “Add an output.” The ask is to calculate the exponentiation, so the flow’s output will be a number, which would be the value of the finalProduct variable:

Figure 6 - Power Automate cloud flow "Respond to a Power App or flow" action.
Figure 6 – Power Automate cloud flow “Respond to a Power App or flow” action.

Finally, test the flow and provide two positive values as flow parameters. Everything should run successfully with plenty of green check marks:

Figure 7 – Power Automate cloud flow successful test.

If the Base Number is 2 and the Exponent Number is 16, then the flow’s Result will be 65,536:

Figure 8 – Power Automate cloud flow exponentiation result.

Quick FYI, the tempProduct and finalProduct variables are floats because they’re a data type better suited for calculating larger values. Integers work, but not so well with larger numbers, so the cloud flow may fail. Though, as the exponentiation results get too large, Power Automate may lose some accuracy even with floats, so it’s still not perfect:

Figure 9 – Power Automate cloud flow run history.

Conclusion:
Power Automate expressions are great, but there isn’t an expression for everything yet. For many of those missing expressions, makers can create child flows to address gaps in the current feature-set.

“The first step toward tolerance is respect and the first step toward respect is knowledge.”

Henry Louis Gates

#BlackLivesMatter

One thought on “Power Automate: Do While Loop | Calculate Power Of

  1. Pingback: Power Automate: Do While Loop | Calculate Binary Of | console.log('Charles');

Leave a comment