Intended to run before make_history
to ensure that
multi-year/lifetime sales are accounted for.
The default (and intended purpose) is to pick the maximum "duration" value
per customer-year. Optionally, it will also pick the minimum value of month
(intended for mid-year dashboards) if first_month = TRUE.
rank_sale(sale, rank_var = "duration", grp_var = c("cust_id", "year"), first_month = FALSE)
sale | data frame: Input sales data |
---|---|
rank_var | character: name of variable(s) to use for ranking |
grp_var | character: name of variable(s) used for grouping |
first_month | logical: If TRUE, also ensures the output contains the earliest month by grp_var (intended for mid-year dashboards) |
Salic Function Reference: salic
Other license history functions: history_check
,
make_history
#>sale_ranked <- rank_sale(sale_unranked) # check sale ranking - highest duration will always be picked left_join( count(sale_ranked, duration), distinct(sale_unranked, cust_id, year, duration) %>% count(duration), by = "duration", suffix = c(".ranked", ".unranked") )#> # A tibble: 3 x 3 #> duration n.ranked n.unranked #> <int> <int> <int> #> 1 1 76717 78923 #> 2 3 2706 2710 #> 3 99 1810 1810# with earliest month included sale_ranked <- rank_sale(sale_unranked, first_month = TRUE) left_join( count(sale_ranked, month), distinct(sale_unranked, cust_id, year, month) %>% count(month), by = "month", suffix = c(".ranked", ".unranked") )#> # A tibble: 12 x 3 #> month n.ranked n.unranked #> <int> <int> <int> #> 1 1 1582 1582 #> 2 2 2074 2093 #> 3 3 5536 5620 #> 4 4 6987 7138 #> 5 5 7465 7646 #> 6 6 9965 10543 #> 7 7 15876 17270 #> 8 8 10571 12285 #> 9 9 9102 12071 #> 10 10 6637 8981 #> 11 11 3682 5699 #> 12 12 1756 2730