Skip to content

Introduction

Using this prompt is an efficient way to generate PowerFx formula:

You are an expert in Power FX formulas for Power Apps Canvas Design.
You assist by writing on demand formulas.
Please write a Patch function that ...
If you need more details, ask now, otherwise write the statement.

Patch snippets

// *** BULK CREATE ***
// Note that repeating call will fail as RequiredUniqueText must be unique
With({
    data:[{ Title: "Sample A", RequiredUniqueText: "aaa2", StartDate: Date(RandBetween(2024,2026),RandBetween(1,12),RandBetween(1,31)) },
        { Title: "Sample B", RequiredUniqueText: "bbb2", StartDate: Date(RandBetween(2024,2026),RandBetween(1,12),RandBetween(1,31)) },
        { Title: "Sample C", RequiredUniqueText: "ccc2", StartDate: Date(RandBetween(2024,2026),RandBetween(1,12),RandBetween(1,31)) }]
    },
    Patch(
        'Temp records',
        ForAll(Sequence(CountRows(data)),Defaults('Temp records')),
        data
    )
)

// *** SIMPLE ONLY: UPDATEIF AND DELETEIF ***
RemoveIf('Temp records',true)

UpdateIf(
    'Temp records',
    true,
    { StartDate: DateAdd(StartDate, 1,TimeUnit.Days) }
)

// *** BULK UPDATE PATCH ***
ClearCollect(
    colUpdates2,
    Table(
        'Temp records'@{ ID: 7, StartDate: DateValue("2025-01-02") },
        'Temp records'@{ ID: 9, StartDate: DateValue("2025-01-02") }
    )
);
Patch(
    'Temp records',
    ShowColumns(colUpdates2, ID),
    colUpdates2
)

// *** BULK UPSERT PATCH ***
ClearCollect(
    colUpdates2,
    Table(
        'Temp records'@{ ID: 7, StartDate: DateValue("2025-01-03") },
        'Temp records'@{ ID: 9, StartDate: DateValue("2025-01-03") },
        'Temp records'@{ ID: Blank(), StartDate: DateValue("2025-01-03"), RequiredUniqueText: GUID() }
    )
);
Patch(
    'Temp records',
    ShowColumns(colUpdates2, ID),
    colUpdates2
)

Remove delegation handling

fxRemoveSelectedItems(ok: Text, nok:Text):Text ={
    // Remove of the found items is used instead of a RemoveIf construct to handle delegation limits.
    // Only the most recent 500 items are considered as those are the only ones selectable anyway.
    IfError(
        With({tbl: ShowColumns(pcfDetailsList.SelectedItems,ID)},
            Remove('List',Filter(FirstN(Sort([@'List'],ID,SortOrder.Descending),500) As tgt, !IsBlank( LookUp(tbl, ID = tgt.ID))))
        ),
        nok,
        ok
    )
};
If the removal items can be found with filtering (the field Index is indexed), this is better:
Remove('List',Filter(List, Index >125000, Index < 130000))

Reference