November 28, 2020
Hello All,
I am a novice power query user and am trying to automate a database of goalscorers for a football team by transferring one column with multiple names (each separated by a comma) into one column for each name (as per attachment #1). Now I can split column by each occurrence of the comma delimiter which gets me half way there (as per attachment #2). The bit I'm struggling with is if a player has scored 2 or more goals then my source data includes the number of goals scored in brackets after the players name and in this case I then need to duplicate the players name on another column.
Any ideas on how I can get power query to identify if the player has a number in brackets after it and, if so, duplicate that players name again in as many new columns as the number in the brackets. I can then easily count the players name to find out the list of highest goalscorers.
Any thoughts or assistance on this one would be hugely apprecited.
October 5, 2010
Hi Mike,
Please see attached file for solution. Looks like you are an Arsenal fan? They're doing well this season, big game coming this week against City.
To get the result you wanted these are the steps I took (you can see the code in the query editor in PQ)
- Split Scorer column by (delimiter) comma
- Add an Index column which allows you to :
- Unpivot the scorers columns into a single column
- Filter out any rows containing penalties
- Split the scorers column again by delimiter this time the delimiter is (
- Remove any ) from the new column containing the number of goals scored
- Replace og with null
- Add a custom column that uses the List.Repeat function to create a list containing the name of any player who has scored more than 1 goal
- Add a column that uses Text.Combine to create a comma delimited string from the lists created in Step 8
- Remove all columns except the one that now contains the names of scorers
- We now repeat Steps 1 to 3. So split the column by delimiter (comma)
- Add an Index column and ...
- Unpivot the columns to leave you with a column containing the names of the scorers
- Remove any unwanted columns
- Trim the text in the scorers column to tidy it up.
You will of course need to apply this code to your own data as I had to create my own test data. Please always supply an actual file if possible, rather than pictures.
Regards
Phil
November 28, 2020
Hi Phil,
Thank you so much for looking at this. I would never have got there but the way you’ve set it out is super helpful and very logical. I really appreciate your time.
I see you’ve correctly identified the team. I’m actually practising my power bi skills by making a dashboard for my son who supports them. I’m a Plymouth Argyle fan who are ( for once!) doing quite well but much lower down the league pyramid!!
All the best, and do please keep up the great work on your various video channels and website. You’re my ‘go to’ source for any excel related problems. Your knowledge and ability to communicate in simple terms is just so helpful.
thanks again,
Mike
1 Guest(s)