Site icon R-bloggers

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.

#209–210

Puzzles

Author: ExcelBI

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

Puzzle #209

Today we have task related to project management. We have project that consists of processes that consists of tasks. Each task has owner and duration. We need to find out when each person will end task for specific process. Complex processes that consists of multiple tasks, need to be assigned to maximal time per process. I hope that I didn’t mess up with explanation. So let’s try to solve it, instead of talking.

Loading libraries and data

library(tidyverse)
library(readxl)

path = 'Power Query/PQ_Challenge_209.xlsx'
input1 = read_excel(path, range = "A2:C10")
input2 = read_excel(path, range = "A13:C17")
test  = read_excel(path, range = "F1:J5") %>%
  mutate(across(-1, as.Date))

Transformation

i1 = input1 %>%
  mutate(process_part = row_number(), .by = Process) %>%
  separate_rows(Task, sep = ", ") %>%
  left_join(input2, by = c("Task")) %>%
  mutate(max_dur = max(`Duration Days`, na.rm = T),
         end_date = as.Date(`Start Date`) + max_dur, 
         .by = c(Process, process_part)) %>%
  select(Owner, Process, end_date) %>%
  pivot_wider(names_from = Owner, values_from = end_date) %>%
  select(Process, Anne, Lisa, Nathan, Robert)

Validation

identical(i1, test)
# [1] TRUE

Puzzle #210

We have August, at least in western culture, at least at northern hemisphere it is peak of vacation time. So we have vacation related task. Maybe not only vacation but leave days of any kind. We need to check how long each person have been on certain type of leave in continuity. We don’t need to add weekends, but it have to be considered calculating continuity. Let’s go.

Loading libraries and data

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_210.xlsx"
input = read_xlsx(path, range = "A1:C17")
test  = read_xlsx(path, range = "E1:H10")

Transformation

r1 = input %>%
  select(Name, Date) %>%
  group_by(Name) %>%
  summarise(Date = list(seq(min(Date), max(Date), by = "day"))) %>%
  unnest(Date) %>%
  left_join(input, by = c("Name", "Date")) %>%
  mutate(wday = wday(Date, week_start = 1),
         Type = case_when(
           wday == 6 ~ lag(Type, 1),
           wday == 7 ~ lag(Type, 2),
           TRUE ~ Type
         )) %>%
  mutate(cons = consecutive_id(Type), .by = "Name") %>%
  filter(!is.na(Type), 
         wday %in% 1:5) %>%
  summarise(`From Date` = min(Date), 
            `To Date` = max(Date), 
            .by = c(Name, Type, cons)) %>%
  select(Name, `From Date`, `To Date`, Type) %>%
  arrange(desc(Name))

Validation

identical(r1, 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.


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.
Exit mobile version