Thursday, December 19, 2024

Microsoft Power Fx formula to Auto Populate User Name and Time Stamp

Power Apps Guide

Yesterday, someone from a WhatsApp group asked me to help with the following scenario:

I have a Power Apps requirement:
Created 2 fields: First Response By and First Response On on the form.
When a case is assigned to a user, that user's value should auto-populate in the First Response By field, and the time should be filled in the First Response On field.

To achieve this Power Apps requirement, you can use Power Fx formulas to configure the behavior for the First Response By and First Response On fields. Here's a step-by-step guide:

Steps to Implement:

1. Add Fields to the Form:

  • Ensure the fields First Response By (Text/Person field) and First Response On (DateTime field) are added to your data source (e.g., Dataverse table, SharePoint list, etc.) and included in the form.

2. Set Default Value for "First Response By":

When the case is assigned to a user, this field should auto-populate with the assigned user's name.

  • Go to the OnChange property of the field used to assign the case (e.g., "Assigned To").
  • Add this formula:
If(
  IsBlank(ThisItem.'First Response By'),
  UpdateIf(
    YourDataSource,
    ID = ThisItem.ID,
    { 'First Response By': AssignedTo.DisplayName }
  )
)
  • Replace YourDataSource with the name of your table and AssignedTo with the name of your user assignment field.

3. Set Time for "First Response On":

To capture the timestamp when the case is assigned, you can use the OnChange property of the same "Assigned To" field.

  • Add this formula:
If(
  IsBlank(ThisItem.'First Response On'),
  UpdateIf(
    YourDataSource,
    ID = ThisItem.ID,
    { 'First Response On': Now() }
  )
)

4. Handle Updates in the Form:

If you're working with a Power Apps form, ensure these updates are reflected correctly:

  • Use the Patch function to update the fields in your data source whenever the "Assigned To" field changes:
Patch(
  YourDataSource,
  ThisItem,
  {
    'First Response By': AssignedTo.DisplayName,
    'First Response On': If(IsBlank('First Response On'), Now(), 'First Response On')
  }
)

Optional Enhancements:

  • Add validation to ensure these fields are not overwritten after the first update.
  • Use conditions to handle edge cases (e.g., if a case is reassigned).

No comments:

Post a Comment

Power Automate Optimization: Filter Rows vs Trigger Conditions - When and Why to Use Each

Filter Rows vs Trigger Conditions in Power Automate Filter Rows vs Trigger Conditions in Power Automate Why your flow...