A Comprehensive Comparison of Power Automate and Python with Technical Overview of Workflow Definition Language (WDL)

In this article, we will explore a detailed comparison between Power Automate and Python, focusing on key programming structures such as conditionals, loops, variables, and API calls. Additionally, we will dive deep into the Workflow Definition Language (WDL), which powers the underlying logic and expressions in Power Automate. A detailed table comparing Power Automate to Python is included at the end of this article.


1. Conditionals (If/Else)

Python:

In Python, conditionals allow us to execute different blocks of code based on whether a condition evaluates to True or False. Here’s a basic example:

age = 20

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

Power Automate:

In Power Automate, conditionals are implemented using the Condition action. You can create a condition that evaluates an expression (e.g., if a variable’s value is greater than 18) and route the workflow to different branches accordingly.

  • Configuration:
  • Add the Condition action.
  • Define the condition, such as “age >= 18”.
  • Specify the actions under the “Yes” branch for a True condition.
  • Specify actions under the “No” branch for a False condition.

2. Loops (For/While)

Python:

Python provides for and while loops for iterating over collections or repeating actions.

  • For loop:
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)
  • While loop:
count = 1

while count <= 3:
    print(count)
    count += 1

Power Automate:

In Power Automate, the equivalent of a for loop is the Apply to each action. This action iterates over a collection (like a list of emails or items in a SharePoint list) and performs actions on each element.

  • Configuration:
  • Add the Apply to each action.
  • Select the collection you want to iterate over.
  • Add actions inside the loop that will execute for each item in the collection.

3. Variables and Assignment

Python:

In Python, you assign values to variables directly. Python’s dynamic typing allows variables to change type during runtime.

x = 10
y = "Hello"
z = [1, 2, 3]

Power Automate:

In Power Automate, variables are initialized and modified using specific actions:

  • Initialize Variable: Used to declare a variable with an initial value.
  • Set Variable: Modifies the value of a previously declared variable.
  • Configuration:
  • Use Initialize Variable to create a variable.
  • Use Set Variable to change its value within the flow.

4. Functions and Reusable Actions

Python:

Functions in Python allow you to encapsulate reusable code logic that can be called with parameters. They can also return values.

def greet(name):
    return f"Hello, {name}!"

print(greet("John"))

Power Automate:

Power Automate does not have traditional functions. However, you can group actions together, and Actions serve as reusable components for your workflow logic. Each action can receive inputs (similar to parameters) and produce outputs (similar to return values).


5. Flow Control (Break/Continue)

Python:

In Python, flow control is handled with keywords such as break and continue within loops, as well as return inside functions.

for i in range(5):
    if i == 3:
        break  # Stop loop when i is 3
    elif i == 2:
        continue  # Skip iteration when i is 2
    print(i)

Power Automate:

In Power Automate, flow control is achieved with conditionals and actions like:

  • Condition: For branching based on a condition.
  • Switch: For handling multiple conditional cases.
  • Terminate Flow: To stop the flow based on certain conditions.

Power Automate lacks direct equivalents to break or continue but offers control mechanisms through its user interface for managing execution paths.


6. Expressions (Mathematical/Logical)

Python:

Expressions in Python are used for computations, string manipulation, comparisons, etc. The language provides a rich set of operators for performing operations:

x = 10
y = 5
result = (x + y) * 2  # Example of a simple mathematical expression

Power Automate (WDL Expressions):

Power Automate leverages Workflow Definition Language (WDL) for its expressions. These expressions allow you to perform string manipulations, date calculations, and logical operations.

  • Common WDL Expressions:
  • add(10, 5): Adds two numbers.
  • substring('Hello', 0, 4): Extracts a portion of a string.
  • if(equals(variable1, 'value'), 'True', 'False'): Conditional logic.

Example Expression in Power Automate:

{
  "expression": "add(variables('x'), variables('y'))"
}

This expression adds the values of two variables x and y.


7. API Calls

Python:

In Python, external APIs are accessed using libraries like requests. This allows developers to send HTTP requests to external services and work with their responses.

import requests

response = requests.get('https://api.example.com/data')

if response.status_code == 200:
    print(response.json())

Power Automate:

In Power Automate, you can make API calls using the HTTP action, which allows you to connect to external APIs, send data, and process responses within the workflow.

  • Configuration:
  • Add the HTTP action.
  • Specify the method (GET, POST, etc.), the URL, and any headers or body content.
  • Handle the response data in subsequent actions.

8. Workflow Definition Language (WDL) Overview

What is Workflow Definition Language (WDL)?

WDL is a domain-specific language used by Power Automate, Azure Logic Apps, and PowerApps to express the flow logic. It enables users to build complex workflows that integrate various services. WDL is responsible for handling expressions, conditionals, loops, and API interactions within these platforms.

WDL Syntax:

WDL uses a JSON-like structure for defining workflows. At the heart of WDL are expressions that enable users to perform operations on variables and data. These expressions can be mathematical, logical, string manipulations, or date calculations.

Key WDL Functions:

  • Mathematical Operations:
  • add: Adds numbers (add(5, 10) returns 15).
  • sub: Subtracts numbers (sub(10, 5) returns 5).
  • String Operations:
  • concat: Concatenates strings (concat('Hello ', 'World') returns ‘Hello World’).
  • substring: Extracts part of a string (substring('abcdef', 1, 3) returns ‘bcd’).
  • Date and Time Functions:
  • utcNow(): Returns the current UTC date and time.
  • addDays(): Adds a specified number of days to a date (addDays(utcNow(), 5)).

Advanced Control Flow in WDL:

  • Switch Case:
    A multi-branch decision-making mechanism similar to the switch statement in traditional programming.
  {
    "switch": {
      "expression": "@triggerOutputs()?['body/priority']",
      "cases": {
        "High": [ ... actions for high priority ... ],
        "Low": [ ... actions for low priority ... ]
      }
    }
  }
  • Scope and Parallel Actions:
    WDL allows grouping actions in Scopes and running actions in parallel using Parallel branches for improved performance.

Comparison Table: Power Automate vs Python

FeaturePythonPower Automate
Conditionalsif condition: ... else:Condition action
Loopsfor item in collection:Apply to each action
Variablesvariable = valueInitialize/Set Variable actions
Functionsdef my_function():Group of Actions
Flow Controlbreak, continue, returnTerminate Flow, Switch
Expressionsx + y, substring(), mathWDL Expressions (e.g., add, concat)
API Callsrequests.get('url')HTTP action
Date/Time Handlingdatetime.now()WDL Date/Time Functions
Parallel ExecutionThreads, `multiprocessing

` library | Parallel Branches |


Conclusion

Both Power Automate and Python offer powerful workflows, each excelling in different contexts. Power Automate is ideal for non-developers or business professionals needing to automate workflows across services quickly, while Python provides unparalleled flexibility and control in coding custom logic, especially for more complex operations.

Understanding WDL enhances the ability to write more complex and maintainable workflows in Power Automate, particularly in scenarios requiring advanced expressions and logic.


Edvaldo Guimrães Filho Avatar

Published by

Categories:

Leave a comment