Puzzle #173
One of contestants said: Why should we break such a nice table? Because indeed it is what we have to do: break table into cross-tab which is cross-tab only from appearance. We need to generate some empty cells to achieve it, and then get some summaries from the data
Loading libraries and data
library(tidyverse)
library(readxl)
library(glue)
input = read_excel("Power Query/PQ_Challenge_173.xlsx", range = "A1:B731")
test = read_excel("Power Query/PQ_Challenge_173.xlsx", range = "D1:H27")
Transformation
result1 = input %>%
mutate(quarter = quarter(Date),
year = year(Date),
month = month(Date, label = TRUE, locale = "en"),
month_num = month(Date)) %>%
summarise(`Total Sale` = sum(Sale), .by = c("year", "quarter", "month", "month_num")) %>%
mutate(years_row = row_number(),
sales_perc = `Total Sale` / sum(`Total Sale`),
.by = "year") %>%
mutate(quarter_row = row_number(), .by = c("year","quarter")) %>%
mutate(display_year = ifelse(years_row == 1, year, NA_character_),
display_quarter = ifelse(quarter_row == 1, quarter, NA_integer_)) %>%
select(year, Year = display_year, Quarter = display_quarter, Month = month, month_num, `Total Sale`, `Sale %` = sales_perc)
totals = result1 %>%
summarise(`Total Sale` = sum(`Total Sale`), `Sale %` = sum(`Sale %`), .by = "year") %>%
mutate(Year = glue("{year} Total") %>% as.character(),
Quarter = NA_integer_,
Month = NA_character_,
month_num = NA_integer_) %>%
select(year, Year, Quarter, Month, `Total Sale`, `Sale %`)
result = bind_rows(result1, totals) %>%
arrange(year, month_num) %>%
select(-c(year, month_num))
Validation
all.equal(result, test, check.attributes = FALSE)
# [1] TRUE
Puzzle #174
Power Query challenges are always about solving problems, usually even real-life ones. Today we have people that sold something, but somebody just written down how much they get for full period of engagement. But we need data in month granulation. And one more note, we need to differentiate months by number of days. We need to calculate monthly sales and running sum resetting itself every year.
Loading libraries and data
library(tidyverse)
library(readxl)
library(padr)
input = read_excel("Power Query/PQ_Challenge_174.xlsx", range = "A1:D5")
test = read_excel("Power Query/PQ_Challenge_174.xlsx", range = "F1:J20")
Transformation
result = input %>%
pivot_longer(cols = -c(1, 4), names_to = "date", values_to = "value") %>%
select(-date) %>%
group_by(Emp) %>%
pad() %>%
fill(Sales, .direction = "down") %>%
mutate(days = n(),
daily_sales = Sales / days,
month = floor_date(value, "month"),
year = year(value)) %>%
ungroup() %>%
summarise(`Monthly Sales` = sum(daily_sales),
`From Date` = min(value),
`To Date` = max(value),
.by = c("Emp", "month", "year")) %>%
mutate(`Running Total` = cumsum(`Monthly Sales`), .by = c("Emp", "year")) %>%
select(Emp, `From Date`, `To Date`, `Monthly Sales`, `Running Total`) %>%
mutate(across(c(4:5), ~round(., digits = 2)))
Validation
# not all results match because of floating point precision
# structure achieved
Feel free to comment, share and contact me with advices, questions and your ideas how to improve anything. Contact me on Linkedin if you wish as well.