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.

#165–166

Puzzles

Author: ExcelBI

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

Puzzle #165

Today we get table with data which we suppose to transform into shape of Excel Pivot Tables (aka Crosstabs). Between grouped data rows we have to input group summaries and at very end — total summary for this data. I really care to do exactly what is in provided solution (shape, formatting and so on), so I had to split data into groups, make summary rows and bind it all back together. Not bad, but simplest way to achieve it. Let’s see how I did it.

Load libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_165.xlsx", range = "A1:C11")
test  = read_excel("Power Query/PQ_Challenge_165.xlsx", range = "F1:I15")

Transformation

r1 = input %>%
  mutate(`Max Bonus` = Salary * 0.1,
         group = cumsum(!is.na(Dept)))

make_summary = function(df, gr) {
  data <- df %>%
    filter(group == gr) 
  
  summary <- data %>%
    mutate(Dept = "Total") %>%
    summarise(Dept = first(Dept),
              Emp = as.character(n()),
              Salary = sum(Salary),
              `Max Bonus` = sum(`Max Bonus`))
  result = bind_rows(data, summary)
  return(result)
}

groups = unique(r1$group)
r2 = map_dfr(groups, ~make_summary(r1, .x))

grand_total = r2 %>%
  filter(!is.na(group)) %>%
  summarise(Dept = "Grand Total",
            Emp = as.character(n()),
            Salary = sum(Salary),
            `Max Bonus` = sum(`Max Bonus`))

result = bind_rows(r2, grand_total) %>%
  select(-group)

Validation

identical(result, test)
# [1] TRUE

But we do not end in this place this time. 🙂 One of competitors said, that he didn’t like those PQ solutions when he could do it in few clicks in Excel. So I decided that I can do it in Excel as well, … but in R.

Excel approach

library(tidyverse)
library(readxl)
library(openxlsx2)

input = read_excel("Power Query/PQ_Challenge_165.xlsx", range = "A1:C11") %>%
  fill(everything(), .direction = "down") %>%
  mutate(`Max Bonus` = Salary * 0.1)

wb = wb_workbook() %>%
  wb_add_worksheet(name = "Sheet1") %>%
  wb_add_data(x = input)

df <- wb_data(wb, sheet = 1)

wb = wb %>%
  wb_add_pivot_table(
    df, 
    dims = "F1",
    rows = c("Dept", "Emp"),
    data = c("Emp", "Salary", "Max Bonus" ),
    fun  = c("count", "sum", "sum")
  )

wb_open(wb)

And this comes up.

But this one is just for fun, and show. Tel me if you like it.

Puzzle #166

Now we have to summarize sales from different stores, but as usually real world examples shows that some data are missing. Fortunatelly it was easy to handle. Interesting manouver was needed to get company name, because it was in the same column as tracking number, but not in the same cell. Check it out.

Loading libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_166.xlsx", range = "A1:C14")
test  = read_excel("Power Query/PQ_Challenge_166.xlsx", range = "E1:H5")

Transformation

result = input %>%
  fill(`Tracking No`, .direction = "down") %>%
  mutate(group = cumsum(str_starts(`Tracking No`, pattern = "[A-Z]"))) %>%
  group_by(group) %>%
  summarise(`Tracking No` = paste0(unique(`Tracking No`), collapse = ", "),
            `Item Count` = n_distinct(`Items`, na.rm = TRUE) %>% as.numeric(),
            `Total Amount` = sum(`Amount`, na.rm = TRUE)) %>%
  select(-group) %>%
  separate(`Tracking No`, into = c("Company", "Trackng No"), sep = ", ") %>%
  mutate(`Trackng No` = as.numeric(`Trackng No`)) %>%
  ungroup() %>%
  arrange(Company)

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)