I have a table of dates (not sorted). For each row, I need to find:
- the
RANK
theReport Date
is - the
Report Date
'sReport Month
- the
Report Date
'sReport Quarter
- the the immediate previous
Report Date
- the greatest
Report Date
in the immediate previousReport Month
- the greatest
Report Date
in the immediate previousReport Quarter
I have 1-4 figured out. I'm not sure how to get 5 and 6.
Here is my data, and the various formulas I have:
Report Date | Report Date Rank | Report Month | Report Quarter | Previous Report Date | Previous Month Report Date | Previous Quarter Report Date |
---|---|---|---|---|---|---|
2022-08-19 | 8 | 2022-08 | 2022 Q3 | 2022-08-03 | ??? | ??? |
2022-06-08 | 15 | 2022-06 | 2022 Q2 | 2022-05-31 | ||
2022-09-23 | 2 | 2022-09 | 2022 Q3 | 2022-09-16 | ||
2022-07-18 | 12 | 2022-07 | 2022 Q3 | 2022-06-27 | ||
2022-08-26 | 6 | 2022-08 | 2022 Q3 | 2022-08-22 | ||
2022-09-30 | 1 | 2022-09 | 2022 Q3 | 2022-09-23 | ||
2022-08-03 | 9 | 2022-08 | 2022 Q3 | 2022-08-02 | ||
2022-09-09 | 4 | 2022-09 | 2022 Q3 | 2022-09-02 | ||
2022-09-16 | 3 | 2022-09 | 2022 Q3 | 2022-09-09 | ||
2022-06-27 | 13 | 2022-06 | 2022 Q2 | 2022-06-14 | ||
2022-06-14 | 14 | 2022-06 | 2022 Q2 | 2022-06-08 | ||
2022-05-31 | 16 | 2022-05 | 2022 Q2 | 2022-05-24 | ||
2022-07-26 | 11 | 2022-07 | 2022 Q3 | 2022-07-18 | ||
2022-08-22 | 7 | 2022-08 | 2022 Q3 | 2022-08-19 | ||
2022-04-26 | 18 | 2022-04 | 2022 Q2 | |||
2022-08-02 | 10 | 2022-08 | 2022 Q3 | 2022-07-26 | ||
2022-05-24 | 17 | 2022-05 | 2022 Q2 | 2022-04-26 | ||
2022-09-02 | 5 | 2022-09 | 2022 Q3 | 2022-08-26 |
Formulas:
B1
:={ "Report Date Rank"; ARRAYFORMULA( IF( A2:A <> "", RANK( A2:A, A:A ), ) ) }
C1
:={ "Report Month"; ARRAYFORMULA( IF( A2:A <> "", TEXT(A2:A, "YYYY-MM"), ) ) }
D1
:={ "Report Quarter"; ARRAYFORMULA( IF( A2:A <> "", YEAR(A2:A) & " Q" & ROUNDUP(MONTH(A2:A)/3, 0), ) ) }
E1
:={ "Previous Report Date"; ARRAYFORMULA( IF( A2:A <> "", XLOOKUP( B2:B 1, B:B, A:A, ), ) ) }
For 5 and 6, I was thinking I could RANK
the Report Month
and Report Quarter
columns, but I don't know how to use that information to get the greatest date in the previous "rank".
So, for example, for 5, for the row with 2022-08-19
for Report Date
, 5 and 6 would be:
Previous Month Report Date
=2022-07-26
because that is the greatestReport Date
in the2022-08-19
's previous month2022-08-19
'sReport Month
is2022-08
- The previous
Report Month
is2022-07
- The largest
Report Date
in2022-07
is2022-07-26
Previous Quarter Report Date
=2022-06-27
because that is the greatestReport Date
in the2022-08-19
's previous quarter2022-08-19
'sReport Quarter
is2022 Q3
- The previous
Report Quarter
is2022 Q2
- The largest
Report Date
in2022 Q2
is2022-06-27
CodePudding user response:
Try approximate match (next smallest) on the sorted dates:
=ArrayFormula(if(A2:A="",,xlookup(eomonth(A2:A,-1),sort(A2:A),sort(A2:A),,-1)))
and
=ArrayFormula(if(A2:A="",,xlookup(eomonth(A2:A,-(mod(month(A2:A)-1,3) 1)),sort(A2:A),sort(A2:A),,-1)))