Hi Caleb,
If I'm reading your table correctly, the unit numbers are i column C, In date in column E, Out date in column F and duration of stay in column G.
For stays that do not cross from one month to another, a simple SUMIFS statement woud work to calculate the number of occupied days in a given month. As you've found, theough, the stays that begn in one month and end in another are a bit trickier.
One way to resolve the issue is to use an auxiliary table which calculates the occuped days (for all units) using a separate column for each month, then a summary table to sum the monthly data for each unit.
Here's an example, using random data over a two and a half month span.
Note that the dates in the header row of Aux and Summary are actual Date and Time Values set to the first of each month, and formatted to show only the month and year. These are used in the calculations.
The only formula in Bookings is =F-E in each cell of column G (Stay).
Aux has two formulas:
A2, and filled down: =Bookings :: C2
This simpy copies the values from column C of Bookings to column A of Aux.
B2, and filled down and right:
=IF(AND(Bookings :: $E2>=B$1,Bookings :: $F2<EOMONTH(B$1,0)+1),Bookings :: $F2-Bookings :: $E2,IF(AND(Bookings :: $E2<B$1,Bookings :: $F2>B$1),Bookings :: $F2-B$1,IF(AND(Bookings :: $E2<=EOMONTH(B$1,0),Bookings :: $F2>EOMONTH(B$1,0)),EOMONTH(B$1,0)+1-Bookings :: $E2,"no")))
Same formula, separated into sections:
=
IF(AND(Bookings :: $E2>=B$1,Bookings :: $F2<EOMONTH(B$1,0)+1),Bookings :: $F2-Bookings :: $E2,
IF checks if the IN date AND the OUT Date are both in the month for this column. If so, the calculation is done and the formula is done; if not, control is passed to the next if:
IF(AND(Bookings :: $E2<B$1,Bookings :: $F2>B$1),Bookings :: $F2-B$1,
IF checks it the IN date is before the month for this column AND the OUT date is after the beginning of this month. If so,th calculaton is done and the formula is done; if not, contorl is passed to the thrid IF:
IF(AND(Bookings :: $E2<=EOMONTH(B$1,0),Bookings :: $F2>EOMONTH(B$1,0)),EOMONTH(B$1,0)+1-Bookings :: $E2,
IF checks if the IN Date is before the end of the month for this coumn AND the OUT date is after the end of the month. If so, the calculation is done; if not, the text blow is returned to the cell.
"no")))
This text was chosen to give me a visual indication of the result. In practice, I would replace it with the empty string ( "" ).
Summary has a single formula:
B2, and filled down and right:
=IFERROR(DUR2DAYS(SUMIF(Aux :: $A,$A2,Aux :: B))/DAY(EOMONTH(B$1,0)),"")
SUMIF sums the duatons of Stay for each unit in a specific month for each column.
DUR2DAYS converts the duration to a number of days.
DAY(EOMONTH(B$1,0) returns the number of days in the month whose date is in row 1.
The first is divided by the second, and the result is formatted as percentage, with 0 decimal places.
IFERROR traps the error that occurs when the source column on Aux contain only text values, and replaces the error message with the empty string.
Regards,
Barry