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"))
Hi there,
Thank you for the great post. Just a question, I have 2 sets of dates in my powerapp. One is a completed date and the other is a review date.
I’m trying to do something like the below
“If(DataCardValue4 < DataCardValue3+365, Notify("Please correct date"));
Reset(DataCardValue4);"
However when I go to set the review date, it just go blank even though (I think) I am within the rule.
The rule that I am trying to put in is that the review date (DataCardValue4) can not be more than one year of the completed date (DataCardValue3).
Do you know where I may have gone wrong?
Thank you
Thanks a lot. 🙂
You’re welcome!
Hi April,
I am a great fan of yours! 🙂 I used your desk booking Power App to create one for our business and changed it to our requirements. I now have a new requirement, that due to the corona restrictions in our country, there need to be two predefined groups, which are only allowed to go to the office on specific days, alternating per week. This means, group A is only allowed to come Mondays, Tue and Fr, and group B on Wed and Thursday and the week after that it alternates. So is it possible to filter your calendar from the app, to only allow people in this twp groups to book the desks on this specific dates? I could handle these groups in SharePoint Groups, or a SP list for example. I could also have a list with the information about the weeks and the groups in SP.
Thanks in advance!
Isis
Hey April,
Greeting for the day,
How would I disable the button based on the date picker value? If I selected a date say 2nd April 2021 and I don’t want the user to click the button till he reaches 2nd April 2021. Now the app user is sitting on 24th March 2021.
Thanks April, this was really helpful.
You’re welcome – glad it was helpful!
This is great April, will this work if I change the logic a hair to not allow any future dates?