Skip to main content
Post

#PowerPlatformTip 136: 'Patch Coalesce Upsert'

#PowerPlatformTip 136: 'Patch Coalesce Upsert'

TL;DR: Choose between update and create in Power Apps with one Patch(Source, Coalesce(LookUp(...), Defaults(Source)), {...}) call. The lookup and the write are still two separate steps, so this is not an atomic upsert: where duplicates must not happen, enforce uniqueness in the data source.

💡 Challenge

In Power Apps, keeping separate logic branches for “Create” and “Update” bloats your formulas and invites bugs. How can you fold this into a single operation?

✅ Solution

Use a single Patch() call combined with Coalesce(). If LookUp() finds no record, Coalesce() falls back to Defaults(DataSource), so the same call either updates an existing record or creates a new one.

🔧 How It’s Done

1. Define your data source

🔸 Make sure you have a table or collection connection, e.g. Contacts.

2. Retrieve any existing record

🔸 Store the lookup result in a global variable: Set(varRecord, LookUp(Contacts, Email = varEmail)). LookUp returns the record if it exists, otherwise Blank. A stored variable only fits the key it was read for: if varEmail changes, run the lookup again with Set(varRecord, LookUp(Contacts, Email = varEmail)) (clearing the variable alone would send the next save down the Defaults() path without checking the new email; or do the LookUp inside the Patch formula when the user saves), and after a successful write update it with the record that Patch returns.

3. Patch with Coalesce

🔸 Combine Patch() and Coalesce():

Patch(
    Contacts,
    Coalesce(
        varRecord,
        Defaults(Contacts)
    ),
    {
        Email: varEmail,
        FullName: txtName.Text,
        Phone: txtPhone.Text
    }
)

🔸 Coalesce(varRecord, Defaults(Contacts)) uses the Defaults template when varRecord is Blank, creating a new record.

4. Publish and test

🔸 If a matching record exists it’s updated; otherwise a new record is created with your field values. This relies on LookUp finding an existing record: if the source can’t delegate your lookup condition, or the lookup fails, a missing hit is not proof that the record doesn’t exist and you can end up with a duplicate. Use a lookup the data source can delegate and don’t treat an error as “not found”.

🎉 Result

You’ve consolidated two code paths into one: cleaner form logic, easier to maintain and less error-prone. “Upsert” (update or insert) now happens in one formula, with LookUp and Patch still as two steps behind it.

🌟 Key Advantages

🔸 One Patch formula instead of separate Create/Update branches

🔸 No If() check is needed to choose between the existing-record and create paths

🔸 Cleaner maintenance

🔸 Use with a connected data source that supports the Patch operations in the formula; connector behavior and delegation support can differ.

🔍 What Coalesce Does with Blank and Empty Text

Coalesce returns its first argument that is not blank. Empty text counts as blank, whitespace does not:

🔸 Coalesce("", "fallback") and Coalesce(Blank(), "fallback") both return "fallback".

🔸 Coalesce(" ", "fallback") returns " ", three spaces are a value.

🔸 Coalesce("", "", "third") returns "third"; Coalesce(Blank(), Blank()) returns Blank().

🔸 Coalesce(0, 5) returns 0, zero is not blank.

🔸 With records: LookUp(Table({ID:1}), ID = 99) is Blank(), and Coalesce(thatResult, {ID:0, Name:""}) returns the fallback record.

🗝️ Why the Fallback Record Matters

Tested on a Dataverse Accounts table:

🔸 Patch(Accounts, LookUp(Accounts, 'Account Number' = "ZZ-NOPE"), {'Account Name': "x"}) with no matching record returns Blank() and writes nothing. A blank base record is not a create, it is a no-op. That is the gap Coalesce(..., Defaults(...)) closes.

🔸 Patch(Accounts, LookUp(Accounts, 'Account Number' = "ZZ-C"), {'Account Name': "ZZ tip test C7"}) with a match returns the updated record, .'Account Name' is "ZZ tip test C7" afterwards.

Known ID instead of LookUp

When you already hold the primary key, a record with just the key works as the base record and saves the LookUp:

Patch(Accounts, {Account: GUID("<full-account-id>")}, {'Account Name': "ZZ tip test C10"})

This updated the account with that ID (use the full ID of an existing test account instead of the placeholder). Coalesce(LookUp(Accounts, 'Account Number' = "ZZ-NOPE"), {Account: GUID("<full-account-id>")}) falls back to the key record and the subsequent Patch updated the same row.

🛠️ FAQ

1. Do I still need If() conditions to check for existing records? Coalesce() handles the check for you. If LookUp() returns Blank, it automatically uses Defaults(), so If() is unnecessary.

2. Will this approach work with Dataverse tables? Yes. Patch() and Coalesce() work with Dataverse tables. Replace Contacts with your table name (e.g. Accounts) and also adapt the lookup key, the field names (Email, FullName and Phone belong to the example), their data types and any values that are required to create a row (for Accounts, for example, 'Account Name').

3. What if LookUp() returns more than one record? Make your lookup criteria unique (for example, a primary key or unique field). Otherwise LookUp() returns the first match and may lead to unexpected updates.

4. Does Coalesce treat an empty text box as blank? Yes. Coalesce("", "fallback") returns "fallback", so Coalesce(txtPhone.Text, "n/a") works for an empty input. Whitespace-only text is kept, and 0 is kept.

5. What happens if I Patch with a blank base record and no Coalesce? Nothing is written and Patch returns Blank(). The record is neither updated nor created, which is why the Defaults() fallback is needed for the create path.

6. Can I skip the LookUp when I know the ID? Yes, where the data source accepts it. Tested on Dataverse Accounts (key column Account): Patch(Accounts, {Account: knownId}, {...}) with a record that holds only the primary key updates that record directly, no LookUp call needed. For other connectors check first that a key-only record works as the base record.

This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy, which runs free live workshops (in German). See the next free workshop dates (in German). For companies that want this implemented for them, see KMUpower: Power Platform consulting & automation for SMEs (DACH).

This post is licensed under CC BY 4.0 by the author.

Microsoft and the Power Platform product names (for example Power Apps, Power Automate, Copilot Studio) are trademarks of the Microsoft group of companies. This is an independent website. It is not operated, sponsored or endorsed by Microsoft.