JOOQ : Get fiscal quarter with custom month range - jooq

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.

Related

how to calculate the date from the year, week-of-year and day-of-week in EXCEL?

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)

how to find Due Date on excel formulas?

How to add DUE DATE, counting start from table INVOICE DATE in Microsoft Office Excel ?
I don't know how to using this formula, but I got some hints :
if an invoices start date between 2-15 per months, due date automatically filled date 15 next month
Examples:
INVOICE DATE | DUE DATE format (D-M-Y)---------------------------------
11/08/2019 | 15/09/2019
15/08/2019 | 15/09/2019
02/09/2019 | 15/10/2019
if an invoices start date between 16-31 per months and date 1st next month, due date automatically filled date 1 every 2 next months
Examples:
INVOICE DATE | DUE DATE format (D-M-Y)---------------------------------
21/08/2019 | 01/10/2019
16/08/2019 | 01/10/2019
01/09/2019 | 01/10/2019
01/12/2019 | 01/01/2020
30/12/2019 | 01/02/2020
what should I do, prefer on table A or table B ? for easy to use ?
fixed image here
or you can download it here -> Google Drive(Fixed)
Try this one (the invoice date is in cell A1 here):
=IF(AND(DAY(A1)<16,DAY(A1)>1),DATE(YEAR(EOMONTH(A1,1)),MONTH(EOMONTH(A1,1)),15),DATE(YEAR(EOMONTH(A1,2)),MONTH(EOMONTH(A1,2)),1))
Try below formula. You have to format the resulting cell as date format.
=IF(AND(DAY(A2)>=2,DAY(A2)<=15),DATE(YEAR(A2),MONTH(A2+1),15),DATE(YEAR(A2),MONTH(A2)+2,1))

Get the first Friday of the Quarter of Today's Date

I was trying to get the first Friday of the quarter of today's date or any given date. Let's say the date is 12/06/2020, which falls to the 2nd quarter of the year. I should be able to get the first Friday of the 2nd Quarter and that should be April 3rd.
I was only been able to get the quarter of a given date, which you can see in the code below. Been stuck here for a while. Thanks in advance.
quarter = Int((Month(Now) + 2) / 3)
Here's a function that takes a date and returns the first Friday of the quarter:
Function FirstFridayOfTheQuarter(MyDate As Date) As Date
Dim FirstDayOfTheQuarter As Date
FirstDayOfTheQuarter = DateSerial(Year(MyDate), Int((Month(MyDate) - 1) / 3) * 3 + 1, 1)
FirstFridayOfTheQuarter = DateAdd("d", (13 - Weekday(FirstDayOfTheQuarter)) Mod 7, FirstDayOfTheQuarter)
End Function
This function is taking advantage of the Weekday function that returns:
1 for a Sunday
2 for a Monday
3 for a Tuesday
4 for a Wednesday
5 for a Thursday
6 for a Friday
7 for a Saturday
In case you want a non VBA solution:
My formula is:
=CEILING(EOMONTH(DATE(YEAR(A1);ROUNDUP(MONTH(A1)/3;0)+(ROUNDUP(MONTH(A1)/3;0)-1)*2;1);-1)-5;7)+6

Check day is the last working day of the month

I need to write the formula to check the last working day of the month in excel.
Data look like this:
| Date |
| 2018/10/22 |
| 2018/10/31 |
The output should be:
| Date | Check |
| 2018/10/22 | o |
| 2018/10/31 | x |
Working day: from Mon to Friday, No holidays.
The 'last working day of the month' is a bit ambiguous given different holiday structures and the fact that not everyone works Monday to Friday. However, if a list of local holidays is provided in Z2:Z13, the WORKDAY.INTL function should be able to return the last workday of any month with a variety of work and holiday schedules.
=WORKDAY.INTL(EOMONTH(A2, 0)+1, -1, "0000011", Z$2:Z$13)
For the purpose of demonstration, I've added the weekday to the date format with a custom number format of ddd, yyyy/mm/dd_).
In the following sample image, note that the fictional holiday of Wed, 2018/01/31 pushed the 'last workday of the month' to Tue, 2018/01/30 and March's last day is Fri, 2018/03/30 since Saturday is considered a non-workday.

Excel - count days in specific month between two dates

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)

Resources