PowerQuery Puzzle solved with R

Numbers around us
Numbers around us
Published in
3 min readMay 28, 2024

#185–186

Puzzles

Author: ExcelBI

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

Today we need to add index for employees. De facto, we need to assign them their number in order of their occurance, not looking at fact how many times their appear in document. I can tell you that at the beginning it was looking hard, but I finished presenting 3 approaches, all of them really short in matter of code. Check it out.

Loading libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_185.xlsx", range = "A1:B13")
test = read_excel("Power Query/PQ_Challenge_185.xlsx", range = "D1:F13")

Transformation — approach 1 — position in vector

result = input %>%
mutate(Index = map_dbl(Emp, ~ which(unique(Emp) == .x)[1]), .by = Group)

Transformation — approach 2 — ranking with factorization

result2 = input %>%
mutate(Index = dense_rank(factor(Emp, levels = unique(Emp))), .by = Group)

Transformation — approach 3 — simple sorting of factors

result3 = input %>%
mutate(Index = as.integer(factor(Emp, levels = unique(Emp))), .by = Group)

Validation

all.equal(result, test)
# [1] TRUE

all.equal(result2, test)
# [1] TRUE

all.equal(result3, test)
# [1] TRUE

Puzzle #186

Sometimes in logistics there are unexpected events, and probably someone decided to be aware of delivery not only on exact day, but also one day earlier and one day later. And now we need to apply those notes into calendar, but we can put only one delivery for one day. So exact date is for us more important. Check how I managed to completed this task.

Loading libraries and data

library(tidyverse)
library(readxl)
library(janitor)

input1 = read_excel("Power Query/PQ_Challenge_186.xlsx", range = "A1:A30") %>% clean_names()
input2 = read_excel("Power Query/PQ_Challenge_186.xlsx", range = "C1:D7") %>% clean_names()
test = read_excel("Power Query/PQ_Challenge_186.xlsx", range = "F1:H30") %>% clean_names()

Transformation

marked_dates <- input2 %>%
mutate(
preceding_date = delivery_date - days(1),
following_date = delivery_date + days(1)
) %>%
pivot_longer(
cols = c(preceding_date, delivery_date, following_date),
names_to = "type",
values_to = "marked_date"
) %>%
mutate(type = factor(type, levels = c("preceding_date", "following_date","delivery_date"),
ordered = TRUE))

calendar_with_markings <- input1 %>%
left_join(marked_dates, by = c("calendar_date" = "marked_date")) %>%
mutate(marked = !is.na(vendor)) %>%
group_by(calendar_date) %>%
mutate(proper_type = max(type, na.rm = TRUE)) %>%
ungroup() %>%
filter(proper_type == type | is.na(proper_type)) %>%
mutate(delivery_date = case_when(
type == "delivery_date" ~ calendar_date,
type == "preceding_date" ~ calendar_date + days(1),
type == "following_date" ~ calendar_date - days(1)
)) %>%
select(calendar_date, delivery_date, vendor)

Validation

identical(test, calendar_with_markings)
# [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.