[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.#185–186

PuzzlesAuthor: 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] TRUEall.equal(result2, test)# [1] TRUEall.equal(result3, test)# [1] TRUE Puzzle #186Sometimes 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.


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.Continue reading: PowerQuery Puzzle solved with R