Site icon R-bloggers

PowerQuery Puzzle solved with R

[This article was first published on Numbers around us - Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

#171–172

Puzzles

Author: ExcelBI

All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.

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.


PowerQuery Puzzle solved with R was originally published in Numbers around us on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: Numbers around us - Medium.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version