Implement booking availability in Power Apps in less than 5 minutes

Today I was asked how to implement a booking availability solution in Power Apps.
The booking availability app
So I’m looking to create an app that will ask the user for some booking details, a start date and an end date for the booking period. Once you user has supplied those details we need to know if the slot is available or not.
You could sue this for calendar management in for example a room booking solution.
I’ve created an app in Power Apps that looks like this:

On the right I’m listing all the existing bookings and on the right I’m asking for the details and checking for the availability of my requested booking slot..
The data layer
I’m using SharePoint to store all my bookings in a list. In my list I’ve created two bookings already.

So now we need to look at the code behind that button in the app.
Check the booking availability
So we have two date pickers in the app. One collects the from date and the other one collects the to Date
I will start by checking the number of day that is covered bin the period between the from and to dates. This can be done using the following line of code:
Set(varDays,DateDiff(DatePickerFrom.SelectedDate,
DatePickerTo.SelectedDate,TimeUnit.Days)+1);
Now I’m going to generate a range of numbers starting with 0
Set(varSequence, Sequence(varDays,0,1));
The above should result in an array like 0,1,2,3 if there are 3 days between the two dates given. Notice that I need to include both the start and end date in this array. Hence the +1 earlier.
Compare the Booking slot with the existing bookings
Now we need to check the availability by going through all the dates in the requested period. If we find any of these dates covered by any of the records already in the bookings list then we have to reject the booking.
Set(varAllChecks,
ForAll(varSequence,
CountRows(Filter(Bookings,
'Start Date' <= DateAdd(DatePickerFrom.SelectedDate, Value,TimeUnit.Days)
And
'End Date' >= DateAdd(DatePickerFrom.SelectedDate, Value,TimeUnit.Days)))
));
This will now give us an array telling us how many records were found for each date in the date range. So for an available period we should just get an array with 0s.
Now we need to count the not 0s found in the array using the following line of code:
Set(varAvailable, CountRows(Filter(varAllChecks, Value <>0))=0)
The Full Code
The overall code behind the button should now look like this.
Set(varDays,DateDiff(DatePickerFrom.SelectedDate,
DatePickerTo.SelectedDate,TimeUnit.Days)+1);
Set(varSequence, Sequence(varDays,0,1));
Set(varAllChecks,
ForAll(varSequence,
CountRows(Filter(Bookings, 'Start Date' <= DateAdd(DatePickerFrom.SelectedDate, Value,TimeUnit.Days) And 'End Date' >= DateAdd(DatePickerFrom.SelectedDate, Value,TimeUnit.Days)))
));
Set(varAvailable, CountRows(Filter(varAllChecks, Value <>0))=0)
Continue Reading Pieter Veenstra’s Article on their blog
Implement booking availability in Power Apps in less than 5 minutes
Today I was asked how to implement a booking availability solution in Power Apps.
Blog Syndicated with Pieter Veenstra’s Permission
More About This Author
Array
Modern Workplace14 July 2023Implement booking availability in Power Apps in less than 5 minutes
Modern Workplace10 July 2023Delegation with SharePoint lists Power Apps vs Power Automate
Modern Workplace4 July 2023Timing issues in relation to App OnStart, Screen OnVisible, Timers and user interruptions in Power Apps
Modern Workplace3 July 2023Implement a PDF preview in a Model Driven Apps