PowerQuery Puzzle solved with R

Numbers around us
Numbers around us
Published in
3 min readApr 9, 2024

#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 #171

Power Query puzzles are very often focused on transformation of tables in very different ways. And we have it today as well. But I cannot represent it by any other metaphor than shuffling cards. Let me show you original task.

Transpose given problem table into result table. Basically, it is taking 1st and 4th columns stacked on 2nd and 5th columns stacked on 3rd and 6th column. Repeat the same for all rows. If both column pairs are blanks/null, then skip that.

Load libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_171.xlsx", range = "A1:F7")
test = read_excel("Power Query/PQ_Challenge_171.xlsx", range = "H1:I15")

Transformation (version 1 — base::Map)

result = Map(function(c1, c4, c2, c5, c3, c6) list(c(c1, c4), c(c2, c5), c(c3, c6)), 
input$Col1, input$Col4, input$Col2, input$Col5, input$Col3, input$Col6) %>%
unlist(recursive = F) %>%
Map(function(x) list(x[1], x[2]), .) %>%
tibble(Col = .) %>%
unnest_wider(Col, names_sep = "") %>%
filter(!(is.na(Col1) & is.na(Col2)))

Transformation (version 2 — purrr::pmap)

r1 = input %>%
transmute(
Col = pmap(
list(Col1, Col4, Col2, Col5, Col3, Col6),
~list(c(..1, ..2), c(..3, ..4), c(..5, ..6))
)
) %>%
unnest(cols = Col) %>%
unnest_wider(Col, names_sep = "") %>%
filter(!(is.na(Col1) & is.na(Col2)))

Validation

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

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

Puzzle #172

Salespeople can be compensated in various ways; some have high salaries with no commission, while others have a low base salary but earn high commissions from sales. Puzzle we have to solve today are about the second group. But with little bit complicated situation. Sometimes there are contracts where not one but two salespeople participated, but one transaction has only one row of data with some data nested in strings and so on. Let’s find how much commission each person earned.

Loading libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_172.xlsx", range = "A1:F10") %>%
janitor::clean_names()
test = read_excel("Power Query/PQ_Challenge_172.xlsx", range = "H1:I6")

Transformation

r1 = input %>%
mutate(share_percent = ifelse(is.na(share_percent), "100", share_percent)) %>%
separate_rows(share_percent, sep = ", ") %>%
group_by(item) %>%
mutate(nr = row_number(),
Agent = ifelse(nr == 1, agent1, agent2)) %>%
ungroup() %>%
mutate(Commission = amount * as.numeric(share_percent) / 100 * commission_percent / 100)

top = r1 %>%
group_by(Agent) %>%
summarise(Commission = sum(Commission) %>% round(0))

total = top %>%
summarise(Commission = sum(Commission))

total$Agent = "Total"
result = select(total, Agent, Commission) %>%
bind_rows(top) %>%
arrange(Agent)

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.