cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ARAlmac
Helper II
Helper II

Check if Dates selected are in range of multiple booking dates in SP list

Hi PowerApps Community,

 

I am working on a booking system where a user can book an appliance out up to 2 weeks in advance. 

I have an appliance SP list and then a Bookings SP list.

 

ARAlmac_0-1619182114403.png

I have ran into some trouble with trying to check that the dates selected in the Calendar do not match or are not in between any of the dates that this appliance is already booked out on the right hand side which is the bookings table data.

 

At first I was using this code with "LookUp":

LookUp(Bookings,
SelectedItem.Title = ApplianceBooked
If(
CalendarBooking.StartDate >= 'Booked From' And
CalendarBooking.EndDate <= 'Booked To',
Disabled,
Edit
))

then realised that lookup only retrieves one value. 
I then tried ForAll but had no luck with it working in the displaymode field (was thinking of Disabling the booking if the dates have already been booked).

Anyone have any ideas how I can check the dates selected havent already been booked? 

 

Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@ARAlmac 

Please consider changing your Formula to the following:

If(
   LookUp(Bookings,
      SelectedItem.Title = ApplianceBooked &&
      'Booked From' <= CalendarBooking.StartDate &&
      'Booked To' >= CalendarBooking.EndDate,
      true
   ),
   Disabled,
   Edit
)

 

This should give you the logic you are looking for.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

9 REPLIES 9
RandyHayes
Super User
Super User

@ARAlmac 

Please consider changing your Formula to the following:

If(
   LookUp(Bookings,
      SelectedItem.Title = ApplianceBooked &&
      'Booked From' <= CalendarBooking.StartDate &&
      'Booked To' >= CalendarBooking.EndDate,
      true
   ),
   Disabled,
   Edit
)

 

This should give you the logic you are looking for.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Thanks again! @RandyHayes 

 

It seems to work well! Does Lookup check more than one record then?

RandyHayes
Super User
Super User

@ARAlmac 

LookUp looks through a table and applies the condition in the criteria of the lookup to each record.  Once it finds a match, it stops and returns that record.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
Rajeey3
Helper I
Helper I

Hey @RandyHayes ,

Its ARAlmac here, unfortunately lost my login for other account.

I am having trouble with the above formula. It seems that the SelectedItem.Title = ApplianceBooked isnt triggering and it is disabling for dates that have been selected for other Appliances. I checked that SelectedItem.Title is getting a matching value and it is?

Any ideas? 

How would I handle if an appliance isnt in the booking list yet? I.e. no record in the ApplianceBooked field.

 

Basically I am trying to ensure users cant book over the top of dates that have been booked PER Appliance.

 

Thanks !!

RandyHayes
Super User
Super User

@Rajeey3 

Welcome back under a new name!

 

Well, the key to the formula is based around the Title of the SelectedItem being the ApplianceBooked.  So, if that does not match, then there will be issues with getting any result.

BUT, you say that the ApplianceBooked and the Title are matched and you get results from that, so the rest of the matching is up to the dates.

 

So, I am not understanding something then perhaps.  I have looked over the photo again and tried to understand where you might be doing the things you are and how it works, but I am not seeing it.  Perhaps you could explain the flow of your app a little more.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Sure @RandyHayes ,

Basically we have an appliance list. Then we have a Bookings List.

An appliance can only be booked by one person at a given time so if a person has booked an appliance from the 4th to the 6th then another user cannot book from the 2nd to the 5th or from the 5th to the 10th as it was booked during that time. Bookings can be made 2 weeks in advance which is why we display the current bookings on the right.

 

You will see in the screenshot above the current bookings for that item are on the right hand side and this uses the selecteditem.Title = ApplianceBooked filter so im sure that it works!

 

Thanks for getting back to me again.

 

Aaron

RandyHayes
Super User
Super User

@Rajeey3 

For the most part, that bit of information I understood.  What I mean by the flow of the app is (and I'm guessing) that the user has some sort of Gallery of Appliances to choose from.  When they click on that it takes them to a screen with some sort of Calendar on it and a list of the current bookings.  The user then does what??  How do they pick dates and ranges what role does the calendar play into it?  What are formulas for actions based on things clicked, changed or otherwise interacted with?

Keeping in mind that I only have the initial picture you sent on the first post to reference and determine what is going on.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

@RandyHayes,

 

After some re-thinking of my code and walking through it, I think I have it working.

I basically needed to separate the Selecteditem.Appliance = ApplianceBooked from the Date ranges code by putting them in brackets. Like so:

If(
   LookUp(Bookings,
      ApplianceBooked = SelectedItem.Appliance &&
      (CalendarBooking.StartDate >= 'Booked From' && CalendarBooking.EndDate <= 'Booked To' ||
      CalendarBooking.StartDate <= 'Booked From' && CalendarBooking.EndDate <= 'Booked To' ||
      CalendarBooking.StartDate <= 'Booked To' && CalendarBooking.EndDate >= 'Booked To')
      ,
      true
   ),
   Disabled,
   Edit
)
 

Seems to be allowing me to book for the right times for each appliance now and disabling when that particular one is booked.

 

In case you were interested, yes this is just a typical appliance booking system where the user has a list of appliances in a Gallery. They click in and can see the current bookings on the right. I have added PowerApps components own Calendar component in to allow users to select date ranges they would like to book.

 

Thanks for the help again and there is no doubt we will come across each other soon!

RandyHayes
Super User
Super User

@Rajeey3 

Yes, this is a different formula.  Originally you were looking at And'ing the criteria.  With this new one, you have Or conditions.  When you Or conditions, they should be bracketed from the rest of the formula to result in their own true or false result.

 

Glad you got it working!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (2,400)