PowerQuery Puzzle solved with R

Numbers around us
Numbers around us
Published in
3 min readJun 25, 2024

#193–194

Puzzles

Author: ExcelBI

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

Puzzle #193

Summary again? Yes, but this time not as usual. We have sales and bonus per salesperson per quarter, and as result we also need total per each of those partitions and cummulative summary down the table. Looks simple and if we would have only those things to do that I pointed before it would be really easy. But we have merged two-level headers and that is tricky to manage. We are lucky that there is unpivotr package for this purpose. If you’re going to check this code, focus on first 4 steps of pipe to know how we are helped by unpivotr.

Load libraries and data

library(tidyverse)
library(readxl)
library(unpivotr)

path = "Power Query/PQ_Challenge_193.xlsx"

input = read_xlsx(path, range = "A1:I6", col_names = FALSE)
test = read_xlsx(path, range = "A12:F24")

Transformation

result = input %>%
as_cells() %>%
behead("up-left", "Quarter") %>%
behead("up", "Category") %>%
behead("left", "Persons") %>%
select(Persons, Quarter, Category, chr) %>%
pivot_wider(names_from = Category, values_from = chr) %>%
mutate(across(c(Sales, Bonus), as.numeric),
Total = Sales + Bonus) %>%
pivot_longer(cols = Sales:Total, names_to = "Category", values_to = "Value") %>%
pivot_wider(names_from = Quarter, values_from = Value) %>%
mutate(across(c(Q1:Q4), cumsum), .by = Category) %>%
mutate(Persons = accumulate(Persons, ~ paste(.x, .y, sep = ", "))[match(Persons, unique(Persons))], .by = Category) %>%
mutate(Persons = ifelse(Category == "Sales", Persons, NA_character_))

Validation

identical(result, test)         
#> [1] TRUE

Puzzle #194

Our table looks like we have some kind of sales summary, but done by noting total in register everyday at 3 time points a day, without withdrawing it. But we are weird and we want to know something else. How much this salesperson earned between each check. So we have to “unsummarize” this sequence, which goes like Z along this table. It is pretty easy. Three main procedures: pivot_longer, mutate with lag and pivot_wider. Check it out.

Loading libraries and data

library(tidyverse)
library(readxl)

path = "Power Query/PQ Challenge_194.xlsx"
input = read_xlsx(path, range = "A1:D10")
test = read_xlsx(path, range = "F1:I10")

Transformation

result = input %>%
pivot_longer(cols = -c(1), names_to = "Amt", values_to = "Value") %>%
mutate(val = lag(Value, default = 0),
diff = Value - val) %>%
select(-c(Value, val)) %>%
pivot_wider(names_from = Amt, values_from = diff)

Validation

identical(result, test)  
# [1] TRUE

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.

--

--

Numbers around us
Numbers around us

Self developed analyst. BI Developer, R programmer. Delivers what you need, not what you asked for.