# How to round data to the nearest Quarter?

I deleted my previous post since I believe it had unnecessary information and the direction of the question seemed to be straying away from what I wanted. I apologize to anyone that looked into it.

I was able to boil the question down to a simple function I am looking for:

How am I able to look at a time series data set and then look at the first date in the data and then insert a new date that is the closest beginning of a quarter of that dataset

For example, my data might be something like:

data={{{2015, 8, 10, 0, 0, 0.}, 21.1}, {{2015, 8, 21, 0, 0,
0.},20.7}, {{2015, 8, 24, 0, 0, 0.},18.8}, {{2015, 8, 25, 0, 0,
0.}, 22.0}, {{2016, 2, 3, 0, 0, 0.},13.8}, {{2016, 2, 4, 0, 0, 0.},
15.3}, {{2016, 2, 5, 0, 0, 0.},12.6}}


I want to be able to find the quarter it is in and start inserting data2 (see below) where it starts.

I want to join this data with another dataset that inserts "0" values into months that aren't present:

data2=Transpose[{#, Array[0 &, {Length@#}]}] &[
DateRange[{data[[1, 1, 1]]}, {data[[-1, 1, 1]]}, "Month"]]


From the example above, looking at data, I want to be able to write data2 such that it will start at quarter 3 {{2015,7,1,0,0,0.},XX},{{2015, 8, 10, 0, 0, 0.}, 21.1}, {{2015, 8, 21, 0, 0, 0.},20.7}, ...}}

Thank you!

## 2 Answers

Does this do what you want?

roundback[{y_, m_, ___}] := {y, Ceiling[m, 3] - 2, 1, 0, 0, 0}

roundback[{2015, 8, 10, 0, 0, 0.}]

{2015, 7, 1, 0, 0, 0.}

{{roundback @ #[[1]], "XX"}, ##} & @@ data

{{{2015, 7, 1, 0, 0, 0.}, "XX"},
{{2015, 8, 10, 0, 0, 0.}, 21.1},
{{2015, 8, 21, 0, 0, 0.}, 20.7},
{{2015, 8, 24, 0, 0, 0.}, 18.8},
{{2015, 8, 25, 0, 0, 0.}, 22.},
{{2016, 2, 3, 0, 0, 0.}, 13.8},
{{2016, 2, 4, 0, 0, 0.}, 15.3},
{{2016, 2, 5, 0, 0, 0.}, 12.6}}

• Thanks for such a quick response Mr. Wizard! This is absolutely what I needed. Thank you so much :) Will accept in 1 min!
– jeff
Aug 7, 2017 at 16:04

In M11.1, there is CurrentDate:

CurrentDate[DateList[], "Quarter"] //DateList
CurrentDate[{2015,3,3}, "Quarter"] //DateList
`

{2017, 7, 1, 0, 0, 0.}

{2015, 1, 1, 0, 0, 0.}