PowerApps – Restricting Date Picker Options
A common requirement I see in PowerApps is the need to restrict the dates that a user can select in a Date Picker. For example, when setting a due date you want to only allow the user to select a date at least 2 days in the future.
Let me preface this post by saying currently, you cannot directly control the selectable values in the date picker control itself. This is something that is currently under review in the PowerApps idea forum so I encourage you to vote and share out the idea: https://powerusers.microsoft.com/t5/PowerApps-Ideas/Restrict-available-dates-in-Datepicker-controls/idi-p/139887
So, knowing that we can’t directly control the date picker options itself, how can we restrict the allowed dates? My work-around is to do a check upon submission of the data.
Requirements
I want the user to only be able to select a date in the date picker which is at least 3 Business days in the future. On submission of the form, I need to check that date and display an error message to the user if the date does not fall into that range.
Here’s a video of how the app will function:
Determining Day of the Week
In order to determine if the selected date falls within 3 business days or more, we need to write some logic to get the date to compare your date picker to.
We need to use the Weekday function to determine if the current date plus 3 days falls on a weekend. The Weekday function returns a number 1- 7 which tells you which day of the week the date you passed it in falls on. Sunday would return 1 and Saturday would return 7. If it returns a 1 for Sunday, we need to add 4 days to ensure that the date doesn’t fall on a weekend. If it returns a 7 for Saturday, we need to add 5 days to ensure the date doesn’t fall on a weekend.
To handle this, put the following formula in your Screen’s OnVisible Property:
If(
Weekday(
Today() + 3,
StartOfWeek.Sunday
) = 1,
Set(
varDate,
Today() + 4
),
Weekday(
Today() + 3,
StartOfWeek.Sunday
) = 7,
Set(
varDate,
Today() + 5
),
Weekday(
Today() + 3,
StartOfWeek.Sunday
) <> 7 && Weekday(
Today() + 3,
StartOfWeek.Sunday
) <> 1,
Set(
varDate,
Today() + 3
)
)
Date Validation
Now that we have the logic to determine if the date is a weekday or weekend, we need to add the logic in our Submit button to check if the selected date in our date picker is less than our varDate which we defined above.
If it is less than, we want to display an error message to the user. If it’s not then that’s a valid date so we will allow the user to submit. Here’s the validation formula:
If(
dpDueDate.SelectedDate < varDate,
Set(
varMessage,
"Please select a date within 3 business days or later"
),
Patch(
Tasks,
Defaults(Tasks),
{
Title: tbtask.Text,
Due_x0020_Date: dpDueDate.SelectedDate
}
);
Set(
varMessage,
"Valid Date"
);
Reset(tbtask);
Reset(dpDueDate)
)
To display the error message to the user, you’ll want to insert a label into your app and set its Text value to the varMessage variable.
Alternate Options
The fact that we want to restrict to 3 Business days in the future adds a little complexity because we have to do some checking to see if the date falls on a weekend or not.
If we did not care whether the selected date falls on a weekend then the solution is much easier. We could just use a function like this in the OnSelect of our Submit Button:
If(DatePicker.SelectedDate < Today()+3, Set(varMessage:"Error",Set(varMessage:"Valid Date"))
Thanks a lot. 🙂
You’re welcome!
Thanks April, this was really helpful.
You’re welcome – glad it was helpful!