I've a table as Under:
+----------+------------+-------------+-------+
| CUSTOMER | BILL_DT | DELIVERY_DT | UNITS |
+----------+------------+-------------+-------+
| A | 2007-12-07 | 2008-11-04 | 1 |
| A | 2012-11-21 | 2013-01-31 | 1 |
| A | 1992-12-11 | 1993-05-18 | 1 |
| A | 2018-09-06 | 2019-05-28 | 1 |
| A | 2004-11-29 | 2005-10-07 | 1 |
| B | 2003-02-27 | 2004-03-22 | 1 |
| B | 2017-11-13 | 2018-10-19 | 1 |
| B | 2018-04-07 | 2018-05-18 | 2 |
| B | 2019-06-18 | 2019-11-25 | 1 |
| C | 2018-05-18 | 2018-08-11 | 1 |
| C | 2018-03-16 | 2018-05-30 | 1 |
| C | 2006-12-22 | 2007-08-17 | 1 |
+----------+------------+-------------+-------+
I wish to aggregate the UNITS based on the bill_dt and delivery_dt.
So for February 2020 my criteria would be:
df %>%
group_by(CUSTOMER) %>%
filter(
BILL_DT>='2009-01-01' & BILL_DT<='2020-02-29',
DELIVERY_DT>= '2020-02-29'| is.na(DELIVERY_DT)
) %>%
summarize(sumiv=sum(UNITS),
DATE= format(as.Date('2020-02-01', "%Y-%m-%d"), "%Y-%m"))
For Jan 2020 it would be
df %>%
group_by(CUSTOMER) %>%
filter(
BILL_DT>='2009-01-01' & BILL_DT<='2020-01-31',
DELIVERY_DT>= '2020-01-31'| is.na(DELIVERY_DT)
) %>%
summarize(sumiv=sum(UNITS),
DATE= format(as.Date('2020-01-01', "%Y-%m-%d"), "%Y-%m"))
I wish to repeat this until DATE = 2019-02 and then to bind all of them together.
Is there a way to loop to get the result? Thanks for the help in advance.