#' Find the financial year for a month and calendar year
#'
#' Shows the financial year (as a string) for a given month and
#' calendar year.
#'
#' A new financial year starts with April, so e.g.
#' 3, 2021 will return "2020-21", but
#' 4, 2021 returns "2021-22".
#'
#' @param month Month for which the FY is wanted.
#' @param calendar_year The calendar year.
#'
#' @return The financial year as a string
#' @export
#'
#' @examples find_financial_year(4, 2021)
#'
financial_year <- function(month, calendar_year) {
# Sort out inputs -----------------------------------------------------------
calendar_year <- as.numeric(calendar_year)
month <- as.numeric(month)
if (month %!in% 1:12) {
stop("Enter a month 1-12.")
}
## Helper function for end of financial year (last two digits of next year)
finyear_end <- function(x) {
x %>%
magrittr::add(1) %>%
stringr::str_sub(-2)
}
# Return financial year -----------------------------------------------------
dplyr::case_when(
month >= 4 ~ paste0(calendar_year, "-", finyear_end(calendar_year)),
TRUE ~ paste0(calendar_year - 1, "-", finyear_end(calendar_year - 1)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.