Skip to content

Commit

Permalink
fixed *td functions for dbi
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrohagan committed Sep 16, 2024
1 parent 64a72e1 commit bb58f59
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 41 deletions.
17 changes: 11 additions & 6 deletions R/time_intelligence.R
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ totalytd <- function(.data,...,date_var,value_var){

out_tbl <- full_tbl |>
dplyr::group_by(year,...) |>
dplyr::arrange(date,.by_group = TRUE) |>
dplyr::mutate(
ytd=base::cumsum({{value_var}})
) |>
Expand Down Expand Up @@ -411,7 +412,8 @@ totalytd_dbi <- function(.data,...,date_var,value_var){


out_dbi <- full_dbi |>
dbplyr::window_order(year,...) |>
dplyr::group_by(...,year) |>
dbplyr::window_order(date) |>
dplyr::mutate(
ytd=base::cumsum({{value_var}})
) |>
Expand Down Expand Up @@ -492,7 +494,8 @@ totalqtd_dbi <- function(.data,...,date_var,value_var){


out_dbi <- full_dbi |>
dbplyr::window_order(year,quarter,...) |>
dplyr::group_by(year,quarter,...) |>
dbplyr::window_order(date) |>
dplyr::mutate(
qtd=base::cumsum({{value_var}})
) |>
Expand Down Expand Up @@ -576,11 +579,11 @@ totalmtd_dbi <- function(.data,...,date_var,value_var){
,month=lubridate::month(date)
)

print("full_dbi completed")


out_dbi <- full_dbi |>
dbplyr::window_order(year,month,...) |>
dplyr::group_by(year,month,...) |>
dbplyr::window_order(date) |>
dplyr::mutate(
mtd=base::cumsum({{value_var}})
) |>
Expand Down Expand Up @@ -666,7 +669,8 @@ totalwtd_dbi <- function(.data,...,date_var,value_var){


out_dbi <- full_dbi |>
dbplyr::window_order(year,month,week,...) |>
dplyr::group_by(year,month,week,...) |>
dbplyr::window_order(date) |>
dplyr::mutate(
wtd=base::cumsum({{value_var}})
) |>
Expand Down Expand Up @@ -742,7 +746,8 @@ totalatd_dbi <- function(.data,...,date_var,value_var){


out_dbi <- full_dbi |>
dbplyr::window_order(...) |>
dplyr::group_by(...) |>
dbplyr::window_order(date) |>
dplyr::mutate(
atd=base::cumsum({{value_var}})
) |>
Expand Down
5 changes: 4 additions & 1 deletion README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ format: gfm
- solved logic for one group var vs. multiple group var
- created dbi equivalents all w,m,y,a todate functions
- remaining functions are wow,mom, and yoy


2024-09-15
- cleaned up window_order / group_by for dbi *td expressions

## to do list

- replicate time intelligence functions
Expand Down
93 changes: 59 additions & 34 deletions glue.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,79 @@ sales_db <- db$sales

con <- dbplyr::remote_con(sales_db)

.data <- sales_db
.data <- sales_db |>
filter(
year(order_date)<=2016
)

dbi_tbl <- make_aggregation_dbi(.data,customer_key,date_var=order_date,value_var =quantity,time_unit="day") |> pluck("dbi")
df_tbl <- make_aggregation_tbl(sales |> filter(year(order_date)<=2016),customer_key,date_var=order_date,value_var =quantity,time_unit="day")

totalatd_dbi(sales_db,date_var=order_date,value_var=quantity)

totalatd(sales,date_var=order_date,value_var=quantity)
totalytd_dbi(.data,customer_key,date_var=order_date,value_var=quantity) |>
filter(
customer_key=="210071"
# customer_key=="1979682"
) |> view()
group_by(customer_key,year) |>
summarise(
max=max(ytd)
,sum=sum(ytd)
)

full_dbi <- sales_db |>
filter(year(order_date) %in% c("2015","2016")) |>
make_aggregation_dbi(customer_key,date_var=order_date,value_var=quantity,time_unit="day") |>
pluck("dbi")

full_dbi <- dbi_tbl |>
dplyr::mutate(
year=lubridate::year(date)
,.before = 1
)

value_var <- "quantity"

full_dbi |>
dbplyr::window_order(customer_key,date) |>
arrange(customer_key,date) |>
dplyr::mutate(
date_lag=dplyr::sql("date + INTERVAL 1 WEEK")
,"{{noquote(value_var)}}_wow":=quantity
)
dplyr::select(-c(date,{{value_var}})) |>
dplyr::ungroup()


out_tbl <- dplyr::left_join(
full_tbl
,lag_table
,by=dplyr::join_by(date==date_lag,...)
) |>
mutate(
"{{value_var}}_wow" := dplyr::coalesce(.data[[rlang::englue("{{value_var}}_wow")]],0)
)
ytd=base::cumsum(quantity)
) |>

dplyr::ungroup()


all.equal(dbi_tbl,df_tbl)

cout |>
pluck("dbi") |>
dplyr::mutate(
year=lubridate::year(date)
,month=lubridate::quarter(date)

full_join(
dbi_tbl
,df_tbl
,by=join_by(date,customer_key)
) |>
group_by(
customer_key
) |>
dbplyr::window_order(year,month) |>
dplyr::mutate(
mtd=base::cumsum(quantity)
summarise(
dbi=sum(quantity.x)
,df=sum(quantity.y)
,n=n()
,.groups = "drop"
) |>
dplyr::ungroup()


mutate(
delta=dbi-df
) |>
summarise(
sum_delta=sum(delta)
)
# filter(customer_key=="2")

count(customer_key,wt="quantity.x")
totalytd_dbi(.data,customer_key,date_var=order_date,value_var=quantity)
totalatd_dbi(.data,customer_key,date_var=order_date,value_var=quantity)
totalmtd_dbi(.data,customer_key,date_var=order_date,value_var=quantity)
totalqtd_dbi(.data,customer_key,date_var=order_date,value_var=quantity) |>
group_by(customer_key,quarter,year) |>
summarise(
qtd=max(qtd)
,sum=sum(quantity)
) |> view()
totalwtd_dbi(.data,customer_key,date_var=order_date,value_var=quantity)


0 comments on commit bb58f59

Please sign in to comment.