Translate

Tuesday, 15 April 2025

Integer and Float Variables in Power Automate: Explanation with Examples

 🚀 Integer and Float Variables in Power Automate: Explanation with Examples

In Power Automate, integer and float variables are used to store numerical values. Here's a breakdown of their usage, along with practical flow examples:

1️⃣ Integer Variables

  • Purpose: Store whole numbers (e.g., 0, 15, -3).
  • Common Actions: Initialize, increment, decrement.
  • Example Flow: Counting Email Attachments
    • Scenario: Count the number of attachments in an email.
    • 🔔 Trigger: "When a new email arrives" (Office 365 Outlook).
    • 🧰 Initialize Integer Variable:
      • Name: attachmentCount
      • Type: Integer
      • Value: 0
    • 🔄 Loop Through Attachments:
      • Apply to Each (attachments):
        • 🔢 Increment Variable:
          • Name: attachmentCount
          • Value: 1
    • 📧 Send Result:
      • Send Email:

        Body: "Total attachments: @{variables('attachmentCount')}"
        

2️⃣ Float Variables

  • Purpose: Store decimal numbers (e.g., 3.14, -5.67).
  • Common Actions: Initialize, update via expressions.
  • Example Flow: Calculating Total Order Cost
    • Scenario: Sum the prices of items in a SharePoint list.

    • 🔔 Trigger: "When an item is created" (SharePoint list with items containing a Price column).

    • 🧰 Initialize Float Variable:

      • Name: totalCost
      • Type: Float
      • Value: 0.0
    • 🔄 Loop Through Items:

      • Apply to Each (items):
        • 💰 Set Variable:
          • Name: totalCost

          • Value:

            @add(variables('totalCost'), float(item()?['Price']))
            
    • 📊 Format and Log Result:

      • Update SharePoint:

        Total: @{formatNumber(variables('totalCost'), 'C2')}
        

⚖️ Key Differences & Best Practices

  • Aspect | Integer Variables | Float Variables
    • 🧰 Initialization | Initialize variable → Type: Integer | Initialize variable → Type: Float
    • 🛠️ Modification | Increment variable action | Set variable with add(), sub(), etc.
    • 📂 Use Cases | Counting, indexing, loops | Financial calculations, measurements
    • 🧰 Common Functions | add(), sub(), mul(), div() | round(), formatNumber(), float()

Advanced Example: Dynamic Discount Calculation

  • Scenario: Apply tiered discounts to an order total.

  • 🔔 Trigger: "When a new item is added" (SharePoint list with OrderTotal).

  • 🧰 Initialize Variables:

    • OrderTotal (float): @\{triggerBody()?['OrderTotal']}
    • DiscountRate (float): 0.0
  • 🤔 Conditional Discounts:

    • If OrderTotal >= 1000:

      @setVariable('DiscountRate', 0.15)
      
    • Else If OrderTotal >= 500:

      @setVariable('DiscountRate', 0.10)
      
  • 💰 Calculate Final Price:

    • Set Variable:

      @mul(variables('OrderTotal'), sub(1, variables('DiscountRate')))
      
  • 📧 Send Approval:

    • Teams Message:

      "Final Price: @{formatNumber(variables('FinalPrice'), 'C2')}"
      

⚠️ Common Pitfalls & Solutions

  • Type Mismatch: Use int() or float() to convert strings/numbers.

    @float(triggerBody()?['PriceString'])
    
  • Precision Issues: Use round() to limit decimal places.

    @round(variables('totalCost'), 2)
    
  • Incrementing Floats: Manually update using add().

    @add(variables('totalCost'), 2.5)
    

Conclusion

  • 🔢 Integers are ideal for counting and discrete values.
  • ⚖️ Floats handle precise calculations involving decimals.
  • Use Initialize variable, Set variable, and arithmetic functions (add(), mul()) to manage numerical logic effectively.

🏆 By leveraging these variables, you can build dynamic flows for inventory tracking, financial reporting, and more! 🚀

No comments:

Post a Comment

Note: only a member of this blog may post a comment.