Home > Back-end >  Month to date, excluding current week in R
Month to date, excluding current week in R

Time:06-04

I am trying to filter dates to be the current month and up to the previous Saturday (last day of the previous calendar week). If the previous Saturday is not in the current month, then the filter would return a blank dataset. I am working in Power BI in the R script editor.

Getting dates only in the given month is easy in Power BI and R. I don't know how to exclude the current week.

CodePudding user response:

one approach with {lubridate}, somewhat verbose for clarity:

library(lubridate)

TODAY = ymd('2022-05-20') ## replace w. today() for production
LAST_SATURDAY = TODAY - wday(TODAY)

## example data:
df <- data.frame(Date = ymd('2022-05-01')   1:60)

df %>%
  filter(year(Date) == year(TODAY),
         month(Date) == month(TODAY),
         Date <= LAST_SATURDAY
         )
  • Related