Power Automate Coalesce Function Explained

Power Automate Coalesce Function

The Power Automate Coalesce function is a less known function in Power Automate, but it is worth knowing the function.

It’s for example quite common to check if a value is null and in case it is null, you want to assign a certain value. So basically a fallback value or a default value. In an if statement you would do something like this:

if(equals(variables('color'),null),'white',variables('color'))

You can implement the same logic in a shorter more elegant variant by using coalesce function in Power Automate.

coalesce(variables('color'),'white')

Note that the function gets even more elegant in contrast to an if expression, when you have to check multiple values for null.

coalesce(variables('color1'),variables('color2'),variables('color3'),'white')

Let’s take a closer look at the coalesce function and see it in action within a flow!

Power Automate Coalesce Function

The Power Automate coalesce function returns the first non-null item of a given list of parameters.

Syntax

coalesce(<object_1>, <object_2>, ...)

Input parameters

  • object_1 (mandatory): An object of any type that is checked if it is non-null.
  • object_2,.. (optional): One or more additional objects of any type that are checked, if they are non-null.

Return value

  • The first non-null value of the given parameters.
  • Null, if there is no non-null parameter.

Power Automate Coalesce Expression Examples

Power Automate Coalesce integer example

Since the coalesce function returns the first non-null value, in the example below a 1 is returned.

coalesce(null,1,2)
=> 1

Power Automate Coalesce String example

This is coalesce function example shows, that an empty string is not a null value. Therefore the return value in the example is the empty string.

coalesce('','abc')
=> ''

Power Automate Coalesce mixed types example

This one was a little surprise to me, you can actually mix different types within coalesce parameters. In the following example we use a string and a number.

coalesce(null,'a',1)
=> 'a'

Power Automate Coalesce array example

This example demonstrates that the Power Automate coalesce function works with arrays aswell.

coalesce(null,createArray(1,2,3),createArray('a','b'))
=> [1,2,3]

How To Use The Coalesce Function In Power Automate

Let’s create step-by-step a Microsoft flow that demonstrates the Power Automate coalesce function.

  1. Create a new instant cloud flow

    Power Automate Coalesce Function New Flow

    New cloud flow

  2. Add an ‘Initialize variable’ step, set Name to ‘myObject’ (1) and Type to ‘Object’ () and leave Value empty.

    Power Automate Coalesce Function initialize variable

    Create null object

  3. Add a Compose step and set Inputs to the expression ‘coalesce(variables(‘myObject’),’default’)’

    Power Automate Coalesce Expression 1

    Power Automate Coalesce Expression

  4. Do a test run and check the result

    Power Automate Coalesce Test Run

    Test run result: default

Note: You have to use a variable of type object for this example. String variables always get converted to empty strings, if you do not set them or even when you explicitly set them to null. (Feels wrong to me) For numbers it is the same, they get set to 0.

Leave a Comment

Your email address will not be published. Required fields are marked *