|
1 | 1 | library(tidyverse) |
2 | 2 | library(contoso) |
3 | | -devtools::document() |
4 | | - |
5 | 3 | devtools::load_all() |
6 | 4 |
|
7 | | - |
8 | | - |
9 | 5 | ## create 5-5-4 calendar |
10 | 6 |
|
11 | 7 | x <- contoso::sales |> fpaR::mtd(order_date,revenue,calendar_type = "standard") |
12 | 8 |
|
13 | 9 |
|
14 | 10 | con <- dbplyr::remote_con(x@datum@data) |
15 | 11 |
|
16 | | -# complete calendar with all attributes |
17 | 12 |
|
18 | | -new_cal <- fpaR:::seq_date_sql(start_date = "2025-01-01",end_date = "2025-12-31",time_unit = "day",con =con ) |> |
| 13 | +## find beginng date indicator |
| 14 | + |
| 15 | +min_year <- year(x@datum@min_date) |
| 16 | + |
| 17 | +start_year <- closest_sunday_feb1(min_year) |
| 18 | + |
| 19 | + |
| 20 | +new_cal <- fpaR:::seq_date_sql(start_date = start_year,end_date=x@datum@max_date,time_unit = "day",con =con ) |> |
19 | 21 | augment_calendar(.date = date) |
20 | 22 |
|
21 | 23 |
|
| 24 | + |
22 | 25 | ## next step add in new year indicator |
23 | 26 | new_cal |> |
24 | 27 | dplyr::mutate( |
25 | | - new_year_date_indicator=if_else(month_of_year==2&day_of_year==4,1,0) |
| 28 | + new_week_indicator=if_else(day_of_week==1,1,0) |
| 29 | + ,cumulative_week_count=cumsum(new_week_indicator) |
| 30 | + ,quarter_554=case_when( |
| 31 | + cumulative_week_count<=14~1 |
| 32 | + ,cumulative_week_count<=28~2 |
| 33 | + ,cumulative_week_count<=42~3 |
| 34 | + ,.default=4 |
| 35 | + ) |
| 36 | + ,month_554=case_when( |
| 37 | + cumulative_week_count<=5~1 |
| 38 | + ,cumulative_week_count<=10~2 |
| 39 | + ,cumulative_week_count<=14~3 |
| 40 | + ,.default=0 |
| 41 | + ) |
26 | 42 | ,.before=1 |
27 | 43 | ) |
28 | 44 |
|
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +# Example usage: |
| 49 | +map(2021:2024,\(x) closest_sunday_feb1(x)) |
| 50 | + |
| 51 | + |
| 52 | +generate_5_5_4_daily <- function(start_date, num_years = 1) { |
| 53 | + start_date <- as.Date(start_date) |
| 54 | + |
| 55 | + # 5-5-4 pattern: weeks per month |
| 56 | + weeks_per_quarter <- c(5, 5, 4) |
| 57 | + weeks_per_year <- rep(weeks_per_quarter, 4) # 12 months |
| 58 | + |
| 59 | + # Total weeks to generate |
| 60 | + total_weeks <- length(weeks_per_year) * num_years |
| 61 | + |
| 62 | + # Generate week numbers |
| 63 | + week_number <- 1:total_weeks |
| 64 | + |
| 65 | + # Generate fiscal years |
| 66 | + fiscal_year <- rep(1:num_years, each = length(weeks_per_year)) |
| 67 | + |
| 68 | + # Generate month assignment |
| 69 | + month_pattern <- 1:12 |
| 70 | + month <- rep(month_pattern, times = weeks_per_year) # expand by weeks per month |
| 71 | + month <- rep(month, times = num_years) # expand for multiple years |
| 72 | + month <- month[1:total_weeks] # truncate if needed |
| 73 | + |
| 74 | + # Generate start dates for each week |
| 75 | + week_starts <- start_date + 7 * (week_number - 1) |
| 76 | + |
| 77 | + # Generate daily dates |
| 78 | + daily_dates <- unlist(lapply(week_starts, function(d) d + 0:6)) |
| 79 | + |
| 80 | + # Repeat week, month, fiscal year info for each day |
| 81 | + week_number_daily <- rep(week_number, each = 7) |
| 82 | + month_daily <- rep(month, each = 7) |
| 83 | + fiscal_year_daily <- rep(fiscal_year, each = 7) |
| 84 | + |
| 85 | + # Return daily calendar |
| 86 | + data.frame( |
| 87 | + date = daily_dates, |
| 88 | + fiscal_year = fiscal_year_daily, |
| 89 | + month = month_daily, |
| 90 | + week_number = week_number_daily |
| 91 | + ) |> |
| 92 | + mutate( |
| 93 | + date=as.Date(date) |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +# Example usage: daily calendar starting Feb 2, 2020, for 1 year |
| 98 | +generate_5_5_4_daily("2020-02-02", num_years = 1) |
| 99 | + |
| 100 | + |
29 | 101 | ## next step add in new year indicator |
30 | 102 |
|
31 | 103 |
|
|
0 commit comments