PowerQuery Puzzle solved with R (2024)

[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.

#201–206

PowerQuery Puzzle solved with R (1)

Puzzles

Author: ExcelBI

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

Puzzle #201

PowerQuery Puzzle solved with R (2)
PowerQuery Puzzle solved with R (3)

We need to find out which customer had opportunity to buy specific product (and maybe bought). We receive two tables: one presenting time of customer activity and one presenting item availability. If in second one we meet empty cell then in start column it means that it was available even before, and in finish column that it is still on stock even after last customer ends his purchasing adventure. This task looks hard, but it really not. We need to make date sequences for each person and product, than find common dates and add some transformation to get result table. Check itout.

Loading libraries anddata

library(tidyverse)library(readxl)path = "Power Query/PQ_Challenge_201.xlsx"input1 = read_excel(path, range = "A2:C7")input2 = read_excel(path, range = "A10:C16")test = read_excel(path, range = "E1:K6")

Transformation

i1 = input1 %>% mutate(date = map2(`Buy Date From`, `Buy Date To`, seq, by = "day")) %>% unnest(date) %>% select(Buyer, date)i2 = input2 %>% mutate(`Stock Start Date` = replace_na(`Stock Start Date`, min(`Stock Start Date`, na.rm = TRUE)), `Stock Finish Date` = replace_na(`Stock Finish Date`, max(i1$date, na.rm = TRUE))) %>% mutate(date = map2(`Stock Start Date`, `Stock Finish Date`, seq, by = "day")) %>% unnest(date) %>% select(Items, date)result = i1 %>% inner_join(i2, by = c("date")) %>% pivot_wider(names_from = Items, values_from = date, values_fn = length) %>% select(`Buyer / Items` = 1, sort(colnames(.), decreasing = FALSE)) %>% mutate(across(-c(1), ~ifelse(is.na(.), ., "X")))
PowerQuery Puzzle solved with R (4)

Validation

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

Puzzle #202

PowerQuery Puzzle solved with R (5)
PowerQuery Puzzle solved with R (6)

Somebody make table that somehow represents organizational hierarchy, but like always we are assigned to clean this mess up. We need to find hierarchy level and subordinations (who reports to whom), and store it as Serial. That one was tricky to make, but let try to walk it together.

Loading libraries anddata

library(tidyverse)library(readxl)path = "Power Query/PQ_Challenge_202.xlsx"input = read_excel(path, range = "A1:C18")test = read_excel(path, range = "E1:F18")

Transformation

result = input %>% mutate(L1 = c*msum(!is.na(Name1))) %>% mutate(L2 = c*msum(!is.na(Name2)), .by = L1) %>% mutate(L3 = c*msum(!is.na(Name3)), .by = c(L1, L2)) %>% mutate(across(starts_with("L"), ~ ifelse(. == 0, NA, .))) %>% mutate(across(everything(), ~ as.character(.))) %>% rowwise() %>% mutate(Names = coalesce(Name3, Name2, Name1), Serial = case_when( !is.na(L3) ~ paste(L1, L2, L3, sep = "."), !is.na(L2) ~ paste(L1,L2, sep = "."), !is.na(L1) ~ L1 )) %>% ungroup() %>% select(Serial, Names) 
PowerQuery Puzzle solved with R (7)

Validation

identical(result, test)# [1] TRUE

Puzzle #203

PowerQuery Puzzle solved with R (8)
PowerQuery Puzzle solved with R (9)

Messy spreadsheets, chaos in a making. How many of us have seen at least one, and fixed at least one of them. What we have today. Base of spreadsheet were 3 groups that we see in first column separated with empty rows. But there are some cells with weird strings and some numbers outside of primarely chosen rows. So we need to summarise our groups of rows (to be specific find average of each group) and get every other cells with numbers all together to category “Remaining”. We need some serious toolshere.

Load libraries anddata

library(tidyverse)library(readxl)path = "Power Query/PQ_Challenge_203.xlsx"input = read_excel(path, range = "A1:C14")test = read_excel(path, range = "E1:F5")

Transformation

result = input %>% mutate(Text = as.numeric(Text), Group = consecutive_id(is.na(Amount1)) / 2 * !is.na(Amount1)) %>% mutate(Group = ifelse(is.na(Amount1), "Remaining", paste0("Group", Group))) %>% summarise(nmb = list(c(Amount1, Amount2, Text)), .by = Group) %>% mutate(nmb = map(nmb, ~.x[!is.na(.x)])) %>% mutate(avg = map_dbl(nmb, ~mean(.x, na.rm = TRUE)) %>% round()) %>% arrange(Group) %>% select(Group, `Avg Amount` = avg)
PowerQuery Puzzle solved with R (10)

There is pretty nice trick done in one of line. We are adding consective_id on column to distinguish groups, but empty rows shouldn’t be in those groups, so we do some magic: multiply groups assignment by 1 if there is value in first column, and by 0 if not, it makes our empty row group 0, which we at the end named “Remaining”.

Validation

identical(result, test)# [1] TRUE

Puzzle #204

PowerQuery Puzzle solved with R (11)
PowerQuery Puzzle solved with R (12)

We have table with lists of fruits (I want to think about it as fruit salad bowls:D). And we need to make cross check for them, to tell how they are similar to each other, how many fruits are common for pairs of salads (for example: first salad has 2 fruits common with second, 1 with third and 5 with fourth. Intersection is good concept and tool to usehere.

Loading libraries anddata

library(tidyverse)library(readxl)path = "Power Query/PQ_Challenge_204.xlsx"input = read_excel(path, range = "A1:D7")test = read_excel(path, range = "F1:I4")

Transformation

count_intersections <- function(col_name, df) { col = df[[col_name]] %>% na.omit() other_cols = df %>% select(-all_of(col_name)) %>% map(na.omit) intersection_counts = other_cols %>% map_int(~ length(intersect(col, .x))) filtered_counts = intersection_counts[intersection_counts > 0] filtered_names = names(filtered_counts) map2_chr(filtered_names, filtered_counts, ~ paste(.x, "-", .y)) %>% paste(collapse = ", ")}result = map_chr(names(input), ~ count_intersections(.x, input))result1 = tibble( Column = paste(names(input), "Match"), Intersections = result) %>% separate_rows(Intersections, sep = ", ") %>% mutate(nr = row_number(), .by = Column) %>% pivot_wider(names_from = Column, values_from = Intersections) %>% select(-nr)
PowerQuery Puzzle solved with R (13)

Validation

identical(result1, test)# [1] TRUE

Puzzle #205

PowerQuery Puzzle solved with R (14)
PowerQuery Puzzle solved with R (15)

We again received data in two separate parts. First table presents number of people with specific answer while second what was the answer. We need to join them and place it in some weird format our boss asked. Let’s doit.

Loading libraries anddata

library(tidyverse)library(readxl)path = "Power Query/PQ_Challenge_205.xlsx"input1 = read_excel(path, range = "A2:B13")input2 = read_excel(path, range = "D2:E13")test = read_excel(path, range = "H2:L8")

Transformation

input = left_join(input1, input2, by = "Item") result = input %>% arrange(desc(YesNo), Item) %>% mutate(nr = row_number(), .by = YesNo) %>% mutate(nr_rem = nr %% 2, nr_int = ifelse(nr_rem == 1, nr %/% 2 + 1, nr %/% 2)) %>% select(-nr) %>% pivot_wider(names_from = nr_rem, values_from = c(Item, Value), values_fill = list(Value = 0)) %>% mutate(Sum = Value_0 + Value_1) %>% select(YesNo, Item1 = Item_1, Item2 = Item_0, Sum) %>% mutate(`%age` = Sum/sum(Sum), .by = YesNo) 
PowerQuery Puzzle solved with R (16)

Validation

identical(result, test)# [1] TRUE

Puzzle #206

PowerQuery Puzzle solved with R (17)
PowerQuery Puzzle solved with R (18)

And here we are in world of fairytales, because I don’t know how to explain sense of this transformation. It looks like Big Bad Wolf comes up and blow away our data along the spreadsheet. And we need to find out how it is even possible. We need to unite, and separate again, pivot longer and back wider so many techniques are used to achieveit.

Loading libraries anddata

library(tidyverse)library(readxl)path = "Power Query/PQ_Challenge_206.xlsx"input = read_excel(path, range = "A1:D13")test = read_excel(path, range = "F1:K19")

Transformation

r1 = input %>% mutate(group = c*msum(is.na(Group1)) + 1) %>% filter(!is.na(Group1)) %>% mutate(nr = row_number(), .by = group) %>% unite("Group", Group1:Group2, sep = "-") %>% unite("Value", Value1:Value2, sep = "-") %>% pivot_longer(-c(nr, group), names_to = "Variable", values_to = "Value") %>% select(-Variable)rearrange_df <- function(df, part) { df %>% filter(group == part) %>% select(-group) %>% mutate(col = nr, row = row_number()) %>% pivot_wider(names_from = col, values_from = Value) %>% as.data.frame()}result = map_df(unique(r1$group), ~ rearrange_df(r1, .x)) %>% select(-c(1,2)) %>% separate_wider_delim(1:ncol(.), delim = "-", names_sep = "-") %>% mutate(across(everything(), ~ if_else(. == "NA", NA_character_, .)))names(result) = names(test)
PowerQuery Puzzle solved with R (19)

Validation

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

Remember, always if you have structure to compare which contains NA’s do not identical, but rather all.equals, that can check evenNA’s.

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 aswell.

PowerQuery Puzzle solved with R (20)

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.

Related

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.

PowerQuery Puzzle solved with R (2024)

FAQs

How do you solve difficult puzzles in reasoning? ›

Basic Steps to Solve Puzzles in Reasoning
  1. Go through the question. Reading and understanding the question correctly is more important to get an accurate result. ...
  2. Create an idea of the whole question. ...
  3. Focus on the required information needed and leave out unnecessary data. ...
  4. Problem-solving. ...
  5. Reasoning skill. ...
  6. Thinking skills.

Is there any trick to solve puzzles? ›

EXPERT TIP: Work on a small section at a time instead of trying to place pieces throughout the puzzle. Starting with an area that has a pattern or wording and working your way out, work your way toward the edge of the puzzle. If you get stuck, start on a new section.

What is the trick to solve reasoning questions? ›

Listed below are some of the steps to solve Logical Reasoning questions and answers:
  • Read and understand the information carefully.
  • Analyze critical logical information.
  • Think of all the possible solutions.
  • Compare the answer obtained with other possibilities.
  • Come to a correct logical conclusion.

How do people solve logic puzzles so fast? ›

You can greatly speed up your solving of logic puzzles if you can “see” the patterns on the grid as they start to form. Everything in a logic grid has to slot together so if there are Xs for particular events horizontally then they will need to follow vertically as well.

What reasoning is most used in solving logic puzzles? ›

A logical puzzle is a problem that can be solved through deductive reasoning. This page gives a summary of the types of logical puzzles one might come across and the problem-solving techniques used to solve them.

Is there a strategy for puzzles? ›

Start With The Edges

One of the oldest tricks in the book — start bringing your puzzle to life by finding all of the edge pieces and putting them together. It's okay if you don't build the full border at the beginning, though connecting as many of the edge pieces as possible gives you a solid framework.

How to solve a tricky maths puzzle? ›

First and foremost, read the entire problem carefully, because the introduction usually contains your first clues for the puzzle, or defines important parameters for the puzzle. Work out a particular structure of relationships. Organize the information in a schematic manner by using tables, symbols, and diagrams.

How to solve a puzzle step by step? ›

Jigsaw Puzzle Techniques: Fun & Helpful Ways to Solve a Puzzle
  1. Pick a puzzle. ...
  2. Figure out your puzzle workspace. ...
  3. Sort pieces with puzzle trays or boxes. ...
  4. Solve the edge first or last. ...
  5. Sort by piece shape. ...
  6. Start with smaller sections or solve in quadrants. ...
  7. Take your time and use natural light when possible.
Jan 20, 2023

What is the hardest chapter in reasoning? ›

The puzzle logical reasoning section is considered to be the most difficult part of reasoning, as there exists no set pattern or formulae to solve such problems.

How can I be good at puzzle solving? ›

Here is a great guide to jigsaw puzzling and how to complete your puzzles in the best ways with expert strategies and techniques.
  1. Pick a puzzle. ...
  2. Figure out your puzzle workspace. ...
  3. Sort pieces with puzzle trays or boxes. ...
  4. Solve the edge first or last. ...
  5. Sort by piece shape. ...
  6. Start with smaller sections or solve in quadrants.
Jan 20, 2023

Top Articles
The Animals in the Attic/Credits
Walt Disney Pictures/Closing Variants
Craigslist San Francisco Bay
Friskies Tender And Crunchy Recall
Calvert Er Wait Time
Www.craigslist Virginia
Metallica - Blackened Lyrics Meaning
Chicago Neighborhoods: Lincoln Square & Ravenswood - Chicago Moms
Craigslist Vans
Missed Connections Inland Empire
Napa Autocare Locator
Usborne Links
Wells Fargo Careers Log In
Lichtsignale | Spur H0 | Sortiment | Viessmann Modelltechnik GmbH
Paketshops | PAKET.net
My Vidant Chart
Regal Stone Pokemon Gaia
Shooting Games Multiplayer Unblocked
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
Bend Pets Craigslist
Dumb Money, la recensione: Paul Dano e quel film biografico sul caso GameStop
Purdue 247 Football
27 Paul Rudd Memes to Get You Through the Week
Aspenx2 Newburyport
Ltg Speech Copy Paste
Fiona Shaw on Ireland: ‘It is one of the most successful countries in the world. It wasn’t when I left it’
Margaret Shelton Jeopardy Age
Free T33N Leaks
Waters Funeral Home Vandalia Obituaries
Anesthesia Simstat Answers
Guinness World Record For Longest Imessage
Sacramento Craigslist Cars And Trucks - By Owner
Ixlggusd
In Branch Chase Atm Near Me
A Man Called Otto Showtimes Near Carolina Mall Cinema
All Things Algebra Unit 3 Homework 2 Answer Key
Craigs List Stockton
Bones And All Showtimes Near Johnstown Movieplex
Cranston Sewer Tax
Author's Purpose And Viewpoint In The Dark Game Part 3
Academic Notice and Subject to Dismissal
Love Words Starting with P (With Definition)
Gon Deer Forum
White County
Costco The Dalles Or
Backpage New York | massage in New York, New York
Ephesians 4 Niv
Dayton Overdrive
CPM Homework Help
Shiftselect Carolinas
Southwind Village, Southend Village, Southwood Village, Supervision Of Alcohol Sales In Church And Village Halls
Escape From Tarkov Supply Plans Therapist Quest Guide
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 6223

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.