How to filter data for the last 1 month up to yesterday (exclude today)

Suppose you have time series data and you want to filter your data for the last one month up to yesterday (exclude today). For example, if today is Jan 10th, 2020, then it means you want data from Dec 9th, 2019 to Jan 9, 2020.

Data Filter with relative dates operator

To do so, you can create a custom filter with relative dates operator like below.

between(
    valid, 
    today() - days(1) - months(1), 
    today() - days(1)
)

So let’s look into this between filter condition.

The first argument for the between is the Date Column Name that you want to use for the filter. In this case valid is the column that holds the date information.

The second argument today() - days(1) - months(1) is the left edge (begin date) of the period that you want to include in your filter result. today function call returns today’s Date and subtracting days(1) means getting one day before today, which is yesterday. Then subtract one month (i.e months(1)) from yesterday to get a Date for “12/09/2019” (assuming today is “01/10/2020”)

The last argument today() - days(1) is the right edge (end date) of the period that you want to include in your filter result. it simply calculating yesterday.

When you apply this filter to your chart, then you can see chart like below.

R Package References

dplyr

between is the function from dplyr package.

lubridate

today, days, months functions are all from lubridate package.