I have a home rental business, I wish to calculate the occupancy rate.
For that, I need to calculate the number of nights in a specific month between 2 dates (check-in and check-out).
Here's an example:
A B C D E F
1 | IN | OUT | NIGHTS | 01/01/18 | 01/02/18 |...
------------------------------------------------------------
2 | 15/01/18 | 25/01/18 | 10 | 10 | 0 |...
------------------------------------------------------------
3 | 28/01/18 | 04/02/18 | 7 | 3 | 3 |...
Here are the formulas for:
C3 = B3-A3
D3 = MAX(0; MIN(EOMONTH(D$1;0); $B3) - MAX(D$1; $A3))
I think I'm not too far but the formula is still incorrect.
As you can see, D2 is correct, but D3 is wrong.
This is the tricky part, when the stays overlaps on 2 months, Jan and Feb in thisi case.
D3 should show 4 nights and not 3.
Anyone can help please? Thanks in advance!
PS: sorry for the formatting but I am not able to add a screenshot..
I was thinking that you just add a day to the end of the month unconditionally - I need to try this with one or two test cases though.
MAX(0; MIN(EOMONTH(D$1;0)+1; $B3) - MAX(D$1; $A3))
Just to spell out the four possible scenarios - given start date d1/m1/y1 (A3), end date d2/m2/y2 (B3) ,first day of current month 01/mm/yy (D1) and last day of current month dd/mm/yy (last day of month in D1)
(1) d1/m1/y1>=01/mm/yy and d2/m2/y2 <= dd/mm/yy -> d2/m2/y2 - d1/m1/y1
(Both days in current month - all nights between the two dates)
(2) d1/m1/y1 < 01/mm/yy and d2/m2/y2 <= dd/mm/yy -> d2/m2/y2 - 01/mm/yy
(Start before current month, end in current month - all nights from 1st of month up to end date)
(3) d1/m1/y1 >= 01/mm/yy and d2/m2/y2 > dd/mm/yy -> 01/(mm+1)/yy - d1/m1/y1
(Start in current month, end after current month - all nights from start date up to 1st of following month)
(4) d1/m1/y1 < 01/mm/yy and d2/m2/y2 > dd/mm/yy -> 01/(mm+1)/yy - 01/mm/yy
(Start before current month, end after current month - all nights in month).
In other words, it counts each night following a day in the range, but not the night preceding a day in the range.
You could conditionally add a day when the stay period stretches across month end. This would make that period inclusive as opposed to a simple subtraction.
=MAX(0, MIN(EOMONTH(D$1, 0), $B4)-MAX(D$1, $A4))+(EOMONTH(D$1, 0)<$B4)
Related
I have the Year, Week-of-Year and Day-of-the-Week as follows:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
and I would like to estimate the Date as dd.mm.yyyy, which is highlighted in yellow as it shows in the EXCEL picture.
I tried many formulas, but I am sure there might be an easy one.
I think you are counting the weeks starting from zero because for 9/1/2022 (YYYY/MM/DD format) the corresponding week is 36 as per the result of function WEEKNUM(DATE(2022,9,1)). In order to use the logic to multiply the number of weeks by 7. You need to use as a reference the first day of the year, if it was a Sunday, if not then go back to the previous Sunday, so you can count the entire week. Bottom line use as a reference date, the Sunday of the first week of the year, not the first day of the year (YYYY/1/1)
Here is the approach we use in cell E2:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1))
We use the LET function to avoid repeating the same calculation. The following expression finds the previous Sunday if the first day of the year (fDay) was not a Sunday:
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2))
The XLOOKUP function is used to get the numeric representation of the weekday and use the TEXT function to generate the weekdays in a long format. Since we count the entire week, if the weekday is a Sunday (column C in my screenshot), then we don't need to add any day to our reference date, that is why we use seq-1.
Here is the output for several sample data. Assuming the week count starts with zero, if not the formula needs to be adjusted as also the input data.
Notice that the year 2021 started on a Friday, so if we want to find a day for the first week (0) before Friday it will return a date from the previous year. Like in the case of Monday. If you want an error message instead, then the formula can be modified as follow:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
result, fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1),
IF(YEAR(result) <> y, "ERROR: Date from previous year", result))
I found the solution:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
=DATE (A2,1,3)-WEEKDAY(DATE(A2,1,3)) + 7 * B2 + C2 - 6
I found this solution, but you need to do further testing if it really works.
I calculate month from week: =+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)
I calculate week day number from week day name: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)
And then make date using: =DATE(A2;C2;E2)
https://www.jooq.org/doc/latest/manual/sql-building/column-expressions/datetime-functions/quarter-function/
This works fine for a normal quarter. what if I want to make it work for a fiscal quarter with a custom month?
FISCAL QUARTER MEANS.. instead of January as the starting month of the first quarter it can be any month.
If I consider August as the first month of my fiscal year.
My fiscal quarter look likes these :
Aug-Sep-Oct'2021 => 1st fiscal quarter '2021
Nov-Dec-Jan'2022 => 2nd fiscal quarter '2021
Feb-March-April'2022 => 3rd fiscal quarter '2021
May-June-July'2022 => 4th fiscal quarter '2021
If your fiscal quarters are strictly month based, why not do some arithmetic on the DSL.month(date), such as:
trunc(month(DATE_COLUMN).add(4).div(3)).mod(4).add(1)
An example in PostgreSQL:
select d, trunc((extract(month from d) + 4) / 3) % 4 + 1
from (
select (date '2000-01-01' + i * interval '1 month')::date
from generate_series(0, 11) as t(i)
) t(d)
Resulting in
|d |?column?|
|----------|--------|
|2000-01-01|2 |
|2000-02-01|3 |
|2000-03-01|3 |
|2000-04-01|3 |
|2000-05-01|4 |
|2000-06-01|4 |
|2000-07-01|4 |
|2000-08-01|1 |
|2000-09-01|1 |
|2000-10-01|1 |
|2000-11-01|2 |
|2000-12-01|2 |
dbfiddle here. Now, depending on whatever the first month of your fiscal year is, adapt the formula accordingly, by toggling the add(4) value.
My count of business days in end date should be, for example, May 30 to May 31 will be 2 days and May 30 to May 30 is already counted as 1 day. My problem with WORKDAY is that it counts the day from May 30 to May 31 as 1 day only.
What I've think of is to subtract the result of WORKDAY with 1 business day to get my desired result. However, with my current formula, I was only able to subtract the result of WORKDAY without concerning the weekends (=WORKDAY(C2,B2-1)).
So for example, column C is June 3 (Friday) and column B is 2. The output of my formula will be June 5 (Sunday) because of the subtraction, I want it to be June 6. How will I do that?
Column B = Duration
Column C = Start Date
Column D = End Date (Formula-based)
Column D:
=WORKDAY(C2,B2-1)
Provide Input
B | C | D
2 | 2016/06/03 |
Desired outcome
B | C | D
2 | 2016/06/03 | 2016/06/06
TRY
=WORKDAY.INTL(C2,B2-1,1)
This will set sat. and sun. as offdays
It is not clear exactly what you want to do.
If you want to do an inclusive count of Workdays from 2016/06/03 to 2016/06/06 then you could just use the NETWORKDAYS function.
If you want to add two workdays to Jun 3, and have Jun 3 count as the first day, to give you the result of Jun 6, then use
=WORKDAY(C2-1,B2)
That takes care of the issue where C2 is not a workday, and you want to count 1 workday as the first workday after C2
Otherwise, your formula should work as written.
I have successfully managed to calculate the number of days in a specific month between two dates. However I need to calculate those days as work days within the formula. For example.
| ------------- A -------------------- B --------------- C
| 1 ------- 11/12/2014 ----- 17/03/2015 ----- 01/03/2015
| 2
A - Being the start date
B - Being the End date
C - Being the month to check the number of days range A1:B1 falls within. (In this case March)
=MAX(0,MIN(EOMONTH(C$1,0),$B1)-MAX(C$1,$A1)+1)
This should produce 17, which is the number of regular days. How can I produce the number of working days, and incorporate
=NETWORKDAYS()
Ultimately the answer should be 12.
Thanks in advance for your suggestions.
How about this:
=NETWORKDAYS(MAX(A1,C1),MIN(B1,EOMONTH(C1,0)))
It might be wise to expand it with some error checking like so:
=IF(NETWORKDAYS(MAX(A1,C1),MIN(B1,EOMONTH(C1,0)))<0,0,NETWORKDAYS(MAX(A1,C1),MIN(B1,EOMONTH(C1,0))))
I'm trying to figure out how to auto generate/populate a column in excel based on the number of hours filled in another column:
Start date: 04-08-2011
Hours in one day: 8
Issues Hours Date
part 1 | 2 | 04-08-2011
part 2 | 6 | 04-08-2011
part 3 | 2 | 05-08-2011
part 4 | 2 | 05-08-2011
part 5 | 8 | 06-08-2011
part 6 | 2 | 06-08-2011
So when i add issue 'part 1' the date starts # the start date, when i add hours or 'parts' it continues to use the start days until it reaches 8 hours and continues to the next day.
Also when im adding a row between the existing rows it automatically recalculates the dates.
Anny ideas where to start? Tnx
If I understand what you are asking correctly, and assuming start date is in Cell B1, and hours in 1 day B2
And data header in A4:C4, with data below
Then put the formula '=$B$1+INT(SUM($B$5:B5)/$B$2-0.1)' in C5
Copy C5 down and it should generate the date automatically
The '-0.1' is to account for exactly 8 hrs being allowed in 1 day