The Patch function in Power Fx is a versatile tool for modifying or creating records within a data source. It provides granular control over data manipulation, making it indispensable for complex data management scenarios.
Basic Syntax:
DataSource: The data source containing the record to be modified or created.
BaseRecord: The existing record to be updated or a default record for creating a new one.
ChangeRecord: A record containing the fields to be modified or added.
Key Use Cases:
Updating an Existing Record:
This example updates the Name field of the currently selected employee record in the Employees data source.
Creating a New Record:
This creates a new employee record with the specified details in the Employees data source.
Patching Multiple Records:
This updates multiple records in the Orders data source based on the provided tables.
Data Samples from Dataverse and SharePoint:
Dataverse Example:
Let's assume we have a Contacts entity in Dataverse with fields like FirstName, LastName, and Email.
SharePoint Example:
Let's assume we have a SharePoint list called "Tasks" with columns like "Title", "DueDate", and "Status".
Advanced Usage:
Combining Patch with Other Functions:
- Use LookUp function to retrieve the base record before patching.
- Employ Filter to create a table of records to patch.
- Leverage Collect to store patched records for further processing.
Additional Tips:
- Always validate user input before patching to prevent data inconsistencies.
- Use descriptive variable names to improve code readability.
- Test your patch logic thoroughly to ensure correct behavior.