Mastering Date Pickers in Power Apps with the Patch Function

Date fields are a cornerstone of many business applications, from employee onboarding and project management to leave requests and appointment scheduling. In Power Apps, when users select a date from a DatePicker control, developers often need to capture this value and store it in a data source like SharePoint, Dataverse, or SQL Server. The most common and flexible method for achieving this is by using the Patch() function.

In this article, we'll delve into how to effectively use the Patch() function with the DatePicker control in Power Apps to ensure accurate and efficient data storage.

What is a DatePicker Control?

The DatePicker control provides users with an intuitive calendar interface to select dates. Instead of manually typing dates, users can simply click to choose a date, which offers several significant benefits:

What is the Patch Function?

The Patch() function in Power Apps is a powerful tool used for:

Its syntax provides greater flexibility compared to standard forms:

Patch(
  DataSource,
  Record,
  { FieldName: Value }
)

Why Use Patch with DatePicker?

Many business scenarios require saving date-specific information. Organizations frequently need to:

Combining the visual ease of the DatePicker control with the robust capabilities of the Patch() function makes implementing these scenarios straightforward.

Solution Overview: The Workflow

The process of saving a selected date involves a clear workflow:

  1. The user interacts with the DatePicker control.
  2. The selected date value is retrieved.
  3. The Patch() function is invoked to prepare the data.
  4. A new or existing record is saved.
  5. The data source is updated with the selected date.

This automated process ensures that the selected date is reliably stored.

Step-by-Step Implementation

Step 1: Insert a DatePicker Control

To add a DatePicker to your app:

  1. Navigate to the Insert tab.
  2. Select Input.
  3. Choose DatePicker.

Power Apps will automatically create a DatePicker control, often named something like DatePicker1. Users can now interact with this control to select dates.

Step 2: Create a Submit Button

Add a Button control to your form. Set its Text property to something like "Save". This button will trigger the Patch() function.

Step 3: Patch the DatePicker Value

To save a new record with the selected date, you can use the following Patch() function:

Patch(
  Employees,  // Your DataSource
  Defaults(Employees), // Creates a new record
  {
    JoiningDate: DatePicker1.SelectedDate // Your Date Column and DatePicker value
  }
)

Explanation:

Result: The date selected by the user in DatePicker1 will be stored in the JoiningDate column of a new record in the Employees data source.

Updating Existing Records

The Patch() function is equally adept at updating existing records. If you have a gallery displaying records, you can update the selected item:

Patch(
  Employees,
  Gallery1.Selected, // The record selected in the gallery
  {
    JoiningDate: DatePicker1.SelectedDate
  }
)

Result: The JoiningDate of the record currently selected in Gallery1 will be updated with the date chosen in DatePicker1.

Working with Different Data Sources

Using SharePoint Date Columns

If your data source is SharePoint, and you have a date column named JoiningDate within a list called EmployeeRequests, the Patch() function would look like this:

Patch(
  'EmployeeRequests', // SharePoint list name
  Defaults('EmployeeRequests'),
  {
    'JoiningDate': DatePicker1.SelectedDate // SharePoint date column
  }
)

Result: The selected date is stored in the specified SharePoint list.

Using Dataverse Date Fields

Dataverse supports both DateOnly and DateTime field types. The Patch() function works seamlessly with both:

Patch(
  Employees, // Dataverse table name
  Defaults(Employees),
  {
    StartDate: DatePicker1.SelectedDate // Dataverse date/datetime field
  }
)

Result: The date value is saved directly into the appropriate Dataverse field.

Real-World Example: Employee Onboarding Application

Consider an employee onboarding application built in Power Apps:

Requirement: An HR administrator needs to enter the Employee Name, Joining Date, and Department for a new hire.

Workflow:

  1. The administrator selects the JoiningDate using a DatePicker control.
  2. They click the "Save" button.
  3. The Patch() function executes, capturing the selected date along with other entered information.
  4. The new employee record, including the joining date, is stored in Dataverse.

Result: Employee onboarding information is captured accurately and efficiently, streamlining the HR process.

Common Use Cases

Best Practices

To ensure your date patching is robust and user-friendly, consider these best practices:

Validate Date Selection

Always ensure a date has been selected before attempting to patch. Use the IsBlank() function:

If(
  IsBlank(DatePicker1.SelectedDate),
  Notify("Please select a date."),
  Patch(...)
)

Use Meaningful Control Names

Avoid generic names like DatePicker1. Use descriptive names like dpJoiningDate or dpAppointmentDate to improve code readability.

Handle Errors

Implement error handling using IfError() to gracefully manage potential issues during the patching process:

IfError(
  Patch(...),
  Notify("Error saving record. Please try again.")
)

Use Date Formatting Carefully

Ensure consistent date formatting across your application and data source to prevent confusion and data integrity issues.

Test Time Zone Behavior

If your application deals with users across different time zones, pay close attention to how DatePicker and Dataverse DateTime fields handle time zone conversions to avoid discrepancies.

Benefits of Using Patch with DatePicker

Conclusion

The Patch() function, when combined with the DatePicker control, offers a simple yet powerful method for saving and updating date values within Power Apps. Whether you are integrating with SharePoint, Dataverse, SQL Server, or other data sources, Patch() provides the flexibility and reliability needed for robust data updates.

By adhering to best practices such as validation, clear naming conventions, and error handling, you can build user-friendly applications that capture date information accurately and efficiently, enhancing your business processes.