Question

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!

Was it helpful?

Solution

In the end, I got the right numbers. There are a number of solutions around for populating a column adjacent to the dataset, but I had a lot of trouble getting a single value solution.

The way this works is pretty similar to most SUMPRODUCT formulae:

  • first 2 result sets filter in the bookings where part or all the period lies between the search dates
  • third result set returns the avg PPN for all filtered bookings
  • last result set uses a nested IF() to return the number of booking days that fall between the 2 search dates

Hope it helps someone else :)

=ARRAYFORMULA(SUMPRODUCT(NOT(GT(Bookings_StartDate,ReportExec_EndDate)), NOT(LT(Bookings_EndDate,ReportExec_StartDate)),IF(Bookings_Nights,(Bookings_ValueA/Bookings_Nights),0),(IF(Bookings_EndDate>ReportExec_EndDate,ReportExec_EndDate+1,Bookings_EndDate)-IF(Bookings_StartDate<ReportExec_StartDate,ReportExec_StartDate,Bookings_StartDate))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top