@@ -32,34 +32,41 @@ S7::method(create_calendar,ti) <- function(x){
3232 # For 5-5-4 calendars, we must anchor to the fiscal start (Sunday closest to Feb 1).
3333 # For standard calendars, we use the natural data minimum.
3434
35- if (x @ datum @ calendar_type != " standard" ) {
36- min_year <- lubridate :: year(x @ datum @ min_date )
37- start_date <- closest_sunday_feb1(min_year )
38-
39- # Ensure the anchor doesn't start after our actual data
40- if (min_year < lubridate :: year(start_date )) {
41- start_date <- closest_sunday_feb1(min_year - 1 )
42- }
43- } else {
44- start_date <- x @ datum @ min_date
45- }
46-
4735 # 2. Summarize Raw Data ---------------------------------------------------
4836 # Aggregate the source data to the target time unit (day, month, etc.)
4937 # before building the scaffold.
5038
39+
40+
41+
42+
5143 summary_dbi <- x @ datum @ data | >
5244 dplyr :: ungroup() | >
5345 make_db_tbl() | >
5446 dplyr :: mutate(
55- date = lubridate :: floor_date(!! x @ datum @ date_quo , unit = !! x @ time_unit @ value ),
47+ date = lubridate :: floor_date(!! x @ datum @ date_quo , unit = !! x @ time_unit @ value , week_start = 1 ),
5648 time_unit = !! x @ time_unit @ value
5749 ) | >
5850 dplyr :: summarise(
5951 !! x @ value @ value_vec : = sum(!! x @ value @ value_quo , na.rm = TRUE ),
6052 .by = c(date , !!! x @ datum @ group_quo )
6153 )
6254
55+
56+ if (x @ datum @ calendar_type != " standard" ) {
57+ min_year <- lubridate :: year(x @ datum @ min_date )
58+ start_date <- closest_sunday_feb1(min_year )
59+
60+ # Ensure the anchor doesn't start after our actual data
61+ if (min_year < lubridate :: year(start_date )) {
62+ start_date <- closest_sunday_feb1(min_year - 1 )
63+ }
64+ } else {
65+ start_date <- x @ datum @ min_date
66+ }
67+
68+
69+
6370 # 3. Define "Active Life" Bounds ------------------------------------------
6471 # Optimization: Calculate the first and last activity per group.
6572 # This prevents generating thousands of 'zero' rows for products that
@@ -107,7 +114,8 @@ S7::method(create_calendar,ti) <- function(x){
107114
108115
109116
110- full_dbi <- calendar_dbi | >
117+ full_dbi <-
118+ calendar_dbi | >
111119 dplyr :: full_join(
112120 summary_dbi ,
113121 by = dplyr :: join_by(date , !!! x @ datum @ group_quo )
@@ -118,6 +126,8 @@ S7::method(create_calendar,ti) <- function(x){
118126 )
119127
120128 return (full_dbi )
129+
130+
121131}
122132
123133
0 commit comments