Skip to content

Commit

Permalink
minor clean up of working file
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrohagan committed Aug 11, 2024
1 parent d950f23 commit 46a0751
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 311 deletions.
4 changes: 2 additions & 2 deletions R/abc.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#' @param b incremental segment threshold expressed as a percentage eg .26
#' @param c final segment threshold expressed as a percentage eg .04
#' @param func either "sum" or "n"; sum to sum your dim or n for row count
#' @param ... columns to group by
#' @param ... columns to group by
#'
#' @return a tibble or DBI object
#' @export
#'
#' @examples
#'
#' customer_abc <- abc(fpaR::Sales,customer_key,dim=quantity,a=.7,b=.2,c=.1)
#' customer_abc <- abc(fpaR::sales,customer_key,dim=quantity,a=.7,b=.2,c=.1)
abc <- function(.data,...,dim,a=.7,b=.26,c=.04,func=c("sum")){

## function check
Expand Down
4 changes: 2 additions & 2 deletions R/cohort_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#'
#' @param .data Either a database or tibble object
#' @param id_var The variable that you want to track over time
#' @param time_unit The time unit as character eg('day','week','month',etc) to round the transaction date passed to lubridate::floor_date()
#' @param time_unit The time unit as string eg('day','week','month',etc) to round the transaction date passed to lubridate::floor_date()
#' @param date_var The date column representing the transaction date
#' @param period_label Logical value if you want the represent the dates as periods or Dates
#'
#' @return tibble or DBI object
#' @export
#'
#' @examples
#' fpaR::contoso_fact_sales |> make_cohort_tbl(id_var=ProductKey,date_var=DateKey,time_unit = 'week',period_label =TRUE)
#' fpaR::sales |> make_cohort_tbl(id_var=product_key,date_var=order_date,time_unit = 'week',period_label =TRUE)
#'
#'
make_cohort_tbl <- function(.data,id_var,date_var,time_unit="month",period_label=FALSE){
Expand Down
2 changes: 1 addition & 1 deletion R/show_in_excel.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @export
#'
#' @examples
#' ggplot2::diamonds |> show_in_excel()
#' mtcars |> show_in_excel()
show_in_excel <- function(.data){

# supporting arguments
Expand Down
63 changes: 63 additions & 0 deletions R/time_intelligence.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,69 @@ make_aggregation_tbl <- function(.data,...,date_var,value_var, time_unit) {

}




make_aggregation_sql <- function(.data,...,date_var,value_var, time_unit){

cols <- enquos(...)
group_cols <- deparse(cols)


summary_sql <- glue_sql("
WITH summary_tbl AS (
SELECT
DATE_TRUNC({time_unit}, {`date_var`}) AS date,
{`value_var`},
{`glue_sql_collapse(group_vars, sep = ', ')`}
FROM {`DBI::SQL(.data)`} -- Replace .data with your actual table
GROUP BY date, {`glue_sql_collapse(group_vars, sep = ', ')`}
),
calendar_tbl AS (
SELECT
generate_series(
DATE_TRUNC({time_unit}, MIN(date)),
DATE_TRUNC({time_unit}, MAX(date)),
INTERVAL '1 {time_unit}'
)::DATE AS date
FROM summary_tbl
),
crossing_tbl AS (
SELECT
calendar_tbl.date,
{`glue_sql_collapse(group_vars, sep = ', ')`}
FROM
calendar_tbl
CROSS JOIN (
SELECT DISTINCT {`glue_sql_collapse(group_vars, sep = ', ')`}
FROM summary_tbl
) AS distinct_groups
),
full_tbl AS (
SELECT
crossing_tbl.date,
{`glue_sql_collapse(group_vars, sep = ', ')'},
COALESCE(summary_tbl.{`value_var`}, 0) AS {`value_var`}
FROM
crossing_tbl
LEFT JOIN summary_tbl
ON crossing_tbl.date = summary_tbl.date
AND {`paste(glue_sql('{distinct_groups}', .con = DBI::SQL('')), collapse = ' AND ')`}
)
SELECT * FROM full_tbl
", .con = DBI::SQL("your_database_connection"))

}







#
# calculate <- function(.data,...,date_var,value_var,time_unit){
#
Expand Down
4 changes: 2 additions & 2 deletions man/abc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/make_cohort_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/show_in_excel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 46a0751

Please sign in to comment.