I'm working on a hotel booking calendar tool where I need to calculate the average price per night for reservations that appear between 2 arbitrary search dates. Things to consider:
- the bookings are recorded using a check-in date and a check-out date
- together with a single booking price (everything is on the same row)
- check-out date counts as vacant since the room will be empty by the daily check-in time
- we only want the booking value of the days that fall between the search 2 dates
- to be clear, bookings can extend paste the 1st & 2nd search dates, but we will ignore the daily values for those days beyond the date extremities
- I really want to do this using formulas, not code
I've been playing with a SUMPRODUCT formula all day, and have got pretty close to the desired result - this particular rendition only captures the total number of rooms filled during the period, but modifying it to return the values isn't a problem:
=ARRAYFORMULA(SUMPRODUCT(GTE(Bookings_EndDate,ReportExec_StartDate+1), LTE(Bookings_StartDate,ReportExec_EndDate+0),IF((Bookings_StartDate<=ReportExec_StartDate)*(Bookings_EndDate>=ReportExec_EndDate),((ReportExec_EndDate-ReportExec_StartDate+1)),IF(Bookings_StartDate<ReportExec_StartDate,Bookings_EndDate-ReportExec_StartDate,IF(Bookings_EndDate>ReportExec_EndDate,(ReportExec_EndDate+1)-Bookings_StartDate,0)))))
What I think is the problem here, is that the array calculation only outputs the condition met by the first IF(), so I'm pretty sure this is the wrong approach. Quick orientation of the named ranges:
- ReportExec_StartDate / ReportExec_EndDate = search date 1 / search date 2
- Bookings_StartDate / Bookings_EndDate = columns containing check-in / check-out dates
First IF() condition hunts out all the bookings that extend up to/past the search dates
2nd nested IF() hunts bookings that extend only before the 1st search date
3rd nested IF() hunts bookings that extend only beyond the 2nd search date
In Excel this would be easier, but Google is a new toy for me - any thoughts welcome!