Skip to content

Commit 775cb2f

Browse files
committed
working augment_non_standard calendar, waiting on chagnes to commplete calendar to extend horizon and start on new fiscal date so that we get the right non-standard calendar
1 parent 0eb86a4 commit 775cb2f

File tree

8 files changed

+184
-89
lines changed

8 files changed

+184
-89
lines changed

554.R

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,71 @@
11
library(tidyverse)
22
library(contoso)
3+
devtools::document()
34
devtools::load_all()
45

56
## create 5-5-4 calendar
67

7-
x <- contoso::sales |> fpaR::mtd(order_date,revenue,calendar_type = "standard")
8+
x <- contoso::sales |> fpaR::mtd(order_date,margin,calendar_type = "standard")
89

910

10-
con <- dbplyr::remote_con(x@datum@data)
1111

1212

1313
## find beginng date indicator
1414

15-
min_year <- year(x@datum@min_date)
1615

17-
start_year <- closest_sunday_feb1(min_year)
1816

19-
new_cal <- fpaR:::seq_date_sql(start_date = start_year,end_date=x@datum@max_date,time_unit = "day",con =con ) |>
20-
augment_calendar(.date = date)
17+
2118

2219
# this should generate a type of calendar
23-
# need to validate year logic -- I am getting 53 weeks all the time
20+
# lets start with this for now -- we need to add a 5th year rebalancing logic (eg specify which year we have an extra week)
21+
# lets use this for a momnth over month calculation to see how we would do that
2422

2523

2624

27-
pattern <- "544"
2825

2926

30-
days_in_week=7
31-
weeks_in_quarter=13
32-
quarters_in_year=4
3327

34-
# out <-
35-
new_cal |>
36-
dbplyr::window_order(date) |>
37-
select(date,day_of_week) |>
38-
dplyr::mutate(
39-
year_index=dplyr::if_else(dplyr::row_number()%%(days_in_week*weeks_in_quarter*quarters_in_year)==1,1,0)
40-
,year_ns=cumsum(year_index)
41-
) |>
42-
dplyr::mutate(
43-
week_index=dplyr::if_else(dplyr::row_number()%%7==1,1,0)
44-
,week_ns=cumsum(week_index)
45-
,.by=year_ns
46-
) |>
47-
create_ns_month(pattern=pattern) |>
48-
dplyr::mutate(
49-
quarter_ns=dplyr::case_when(
50-
month_ns<=3~1
51-
,month_ns<=6~2
52-
,month_ns<=9~3
53-
,.default=4
54-
)
55-
) |> collect() |> arrange(date)
28+
## replicate mtd to see this would work in this system
29+
30+
31+
#create claendar
32+
## take the create_Calendar output and then siply pass then through to the augmnet_ns_calendar rather than default calendar so that I
33+
## sot aht I don't need to recreate that
34+
35+
# full_dbi <-
36+
## this should say complete_calendar
5637

5738

5839

59-
out |> collect() |> arrange(date)
40+
if(x@datum@calendar_type=="standard"){
41+
full_dbi <- create_calendar(x) |>
42+
dplyr::mutate(
43+
year=lubridate::year(date)
44+
,month=lubridate::month(date)
45+
,.before = 1
46+
)
6047

48+
}
49+
50+
51+
if(x@datum@calendar_type!="standard")
52+
53+
full_dbi <- create_calendar(x) |>
54+
augment_non_standard_calendar(pattern="544")
55+
)
56+
#this should be augment_standard_calendar
57+
58+
59+
out_dbi <-
60+
full_dbi |>
61+
dbplyr::window_order(date) |>
62+
dplyr::mutate(
63+
!!x@value@new_column_name_vec:=cumsum(!!x@value@value_quo)
64+
,.by=c(year,month,!!!x@datum@group_quo)
65+
) |>
66+
dplyr::mutate(
67+
days_in_current_period=lubridate::day(date)
68+
)
6169

6270

71+
return(out)

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
export(abc)
44
export(atd)
5-
export(augment_calendar)
5+
export(augment_standard_calendar)
66
export(calculate)
77
export(cohort)
88
export(create_calendar)

R/methods.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ complete_calendar <- S7::new_generic("complete_calendar","x")
2929
#' @keywords internal
3030
S7::method(create_calendar,ti) <- function(x){
3131

32+
33+
## neeed to add in standard and non-standard logic here.
34+
35+
# start_year <- closest_sunday_feb1(min_year)
36+
37+
38+
39+
3240
## summarize data table
3341
summary_dbi <- x@datum@data |>
3442
dplyr::ungroup() |>

R/utils-misc.R

Lines changed: 104 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ augment_standard_calendar <- function(.data,.date){
419419

420420
out <- augment_standard_calendar_dbi(.data = .data,.date = .date_var)
421421

422-
return(out)
423422

424423
}
425424

@@ -428,9 +427,11 @@ augment_standard_calendar <- function(.data,.date){
428427

429428
out <- augment_standard_calendar_tbl(.data = .data,.date = !!.date_var)
430429

431-
return(out)
432430
}
433431

432+
433+
434+
434435
}
435436

436437

@@ -465,7 +466,7 @@ closest_sunday_feb1 <- function(year) {
465466
#' @returns DBI object
466467
#'
467468
#' @keywords internal
468-
create_ns_month <- function(.data,pattern){
469+
create_non_standard_month <- function(.data,pattern){
469470

470471

471472
valid_colnames <- c("week_ns","year_ns")
@@ -491,21 +492,21 @@ create_ns_month <- function(.data,pattern){
491492
out <-
492493
.data |>
493494
dplyr::mutate(
494-
.by=year_ns
495-
,month_ns=dplyr::case_when(
495+
.by=year
496+
,month=dplyr::case_when(
496497
# either framing it in advance or somehow passing a arg to it
497-
week_ns<=!!valid_cumulative_months[[1]]~1
498-
,week_ns<=!!valid_cumulative_months[[2]]~2
499-
,week_ns<=!!valid_cumulative_months[[3]]~3
500-
,week_ns<=!!valid_cumulative_months[[4]]~4
501-
,week_ns<=!!valid_cumulative_months[[5]]~5
502-
,week_ns<=!!valid_cumulative_months[[6]]~6
503-
,week_ns<=!!valid_cumulative_months[[7]]~7
504-
,week_ns<=!!valid_cumulative_months[[8]]~8
505-
,week_ns<=!!valid_cumulative_months[[9]]~9
506-
,week_ns<=!!valid_cumulative_months[[10]]~10
507-
,week_ns<=!!valid_cumulative_months[[11]]~11
508-
,week_ns<=!!valid_cumulative_months[[12]]~12
498+
week<=!!valid_cumulative_months[[1]]~1
499+
,week<=!!valid_cumulative_months[[2]]~2
500+
,week<=!!valid_cumulative_months[[3]]~3
501+
,week<=!!valid_cumulative_months[[4]]~4
502+
,week<=!!valid_cumulative_months[[5]]~5
503+
,week<=!!valid_cumulative_months[[6]]~6
504+
,week<=!!valid_cumulative_months[[7]]~7
505+
,week<=!!valid_cumulative_months[[8]]~8
506+
,week<=!!valid_cumulative_months[[9]]~9
507+
,week<=!!valid_cumulative_months[[10]]~10
508+
,week<=!!valid_cumulative_months[[11]]~11
509+
,week<=!!valid_cumulative_months[[12]]~12
509510
,.default=13
510511
)
511512
)
@@ -517,21 +518,21 @@ create_ns_month <- function(.data,pattern){
517518

518519
out <- .data |>
519520
dplyr::mutate(
520-
.by=year_ns
521-
,month_ns=dplyr::case_when(
521+
.by=year
522+
,month=dplyr::case_when(
522523
# either framing it in advance or somehow passing a arg to it
523-
week_ns<=!!valid_cumulative_months[[1]]~1
524-
,week_ns<=!!valid_cumulative_months[[2]]~2
525-
,week_ns<=!!valid_cumulative_months[[3]]~3
526-
,week_ns<=!!valid_cumulative_months[[4]]~4
527-
,week_ns<=!!valid_cumulative_months[[5]]~5
528-
,week_ns<=!!valid_cumulative_months[[6]]~6
529-
,week_ns<=!!valid_cumulative_months[[7]]~7
530-
,week_ns<=!!valid_cumulative_months[[8]]~8
531-
,week_ns<=!!valid_cumulative_months[[9]]~9
532-
,week_ns<=!!valid_cumulative_months[[10]]~10
533-
,week_ns<=!!valid_cumulative_months[[11]]~11
534-
,week_ns<=!!valid_cumulative_months[[12]]~12
524+
week<=!!valid_cumulative_months[[1]]~1
525+
,week<=!!valid_cumulative_months[[2]]~2
526+
,week<=!!valid_cumulative_months[[3]]~3
527+
,week<=!!valid_cumulative_months[[4]]~4
528+
,week<=!!valid_cumulative_months[[5]]~5
529+
,week<=!!valid_cumulative_months[[6]]~6
530+
,week<=!!valid_cumulative_months[[7]]~7
531+
,week<=!!valid_cumulative_months[[8]]~8
532+
,week<=!!valid_cumulative_months[[9]]~9
533+
,week<=!!valid_cumulative_months[[10]]~10
534+
,week<=!!valid_cumulative_months[[11]]~11
535+
,week<=!!valid_cumulative_months[[12]]~12
535536
,.default=13
536537
)
537538
)
@@ -544,21 +545,21 @@ create_ns_month <- function(.data,pattern){
544545

545546
out <- .data |>
546547
dplyr::mutate(
547-
.by=year_ns
548-
,month_ns=dplyr::case_when(
548+
.by=year
549+
,month=dplyr::case_when(
549550
# either framing it in advance or somehow passing a arg to it
550-
week_ns<=!!valid_cumulative_months[[1]]~1
551-
,week_ns<=!!valid_cumulative_months[[2]]~2
552-
,week_ns<=!!valid_cumulative_months[[3]]~3
553-
,week_ns<=!!valid_cumulative_months[[4]]~4
554-
,week_ns<=!!valid_cumulative_months[[5]]~5
555-
,week_ns<=!!valid_cumulative_months[[6]]~6
556-
,week_ns<=!!valid_cumulative_months[[7]]~7
557-
,week_ns<=!!valid_cumulative_months[[8]]~8
558-
,week_ns<=!!valid_cumulative_months[[9]]~9
559-
,week_ns<=!!valid_cumulative_months[[10]]~10
560-
,week_ns<=!!valid_cumulative_months[[11]]~11
561-
,week_ns<=!!valid_cumulative_months[[12]]~12
551+
week<=!!valid_cumulative_months[[1]]~1
552+
,week<=!!valid_cumulative_months[[2]]~2
553+
,week<=!!valid_cumulative_months[[3]]~3
554+
,week<=!!valid_cumulative_months[[4]]~4
555+
,week<=!!valid_cumulative_months[[5]]~5
556+
,week<=!!valid_cumulative_months[[6]]~6
557+
,week<=!!valid_cumulative_months[[7]]~7
558+
,week<=!!valid_cumulative_months[[8]]~8
559+
,week<=!!valid_cumulative_months[[9]]~9
560+
,week<=!!valid_cumulative_months[[10]]~10
561+
,week<=!!valid_cumulative_months[[11]]~11
562+
,week<=!!valid_cumulative_months[[12]]~12
562563
,.default=13
563564
)
564565
)
@@ -572,6 +573,64 @@ create_ns_month <- function(.data,pattern){
572573

573574

574575

576+
#' Augment non-standard calendar
577+
#'
578+
#' @param .data data
579+
#' @param pattern 554,445 or 545
580+
#'
581+
#' @returns DBI object
582+
#'
583+
augment_non_standard_calendar <- function(.data,pattern){
584+
585+
#test inputs
586+
# pattern <- "544"
587+
588+
# assign variables
589+
days_in_week=7
590+
weeks_in_quarter=13
591+
quarters_in_year=4
592+
# start_year <- closest_sunday_feb1(min_year)
593+
594+
595+
#
596+
# new_cal <- seq_date_sql(start_date = start_year,end_date=x@datum@max_date,time_unit = "day",con =con ) |>
597+
# augment_standard_calendar(.date = date) |>
598+
# dplyr::select(date,day_of_week)
599+
600+
out <-
601+
.data |>
602+
dbplyr::window_order(date) |>
603+
dplyr::mutate(
604+
year_index=dplyr::if_else(dplyr::row_number()%%(days_in_week*weeks_in_quarter*quarters_in_year)==1,1,0)
605+
,year=cumsum(year_index)
606+
) |>
607+
dplyr::mutate(
608+
week_index=dplyr::if_else(dplyr::row_number()%%7==1,1,0)
609+
,week=cumsum(week_index)
610+
,.by=year
611+
) |>
612+
create_non_standard_month(pattern=pattern) |>
613+
dplyr::mutate(
614+
quarter=dplyr::case_when(
615+
month<=3~1
616+
,month<=6~2
617+
,month<=9~3
618+
,.default=4
619+
)
620+
) |>
621+
dplyr::mutate(
622+
day=lubridate::day(date)
623+
) |>
624+
dplyr::select(
625+
date,day,week,month,quarter,year
626+
)
627+
628+
return(out)
629+
630+
}
631+
632+
633+
575634
utils::globalVariables(
576635
c(
577636
"desc",

man/augment_non_standard_calendar.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)