How to Merge Query for multiple column - excel

I have a table of information like this:
And a lookup table for user names to IDs:
How do I do a Merge on each column to lookup the values from the other table so I get this result:
I do not want to manually apply an action to each role column, because the list of roles may grow or shrink. So the solution needs to all columns (except the first) in the table.
Can this be done?

Basically this calls for unpivot on the Project data, merge to the other table, then re-pivot to get back in proper order
Steps:
Load in the ID data; here I am assuming it is loaded in query ID_Table
Load in Project data; here I am assuming it is loaded in range Projects
In the project query, right-click the first (project) column, unpivot other columns
Home ... Merge queries...
Merge the two tables using the Value column in the project query and the Person column in the ID_Table query, and use Left Outer merge
Expand results using double arrows atop column and uncheck all except ID
Right-click the value column and remove
Click attribute column ... transform .. pivot column...
Use ID as value column ... advanced options ... dont aggregate
sample code
let Source = Excel.CurrentWorkbook(){[Name="Projects"]}[Content],
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Project"}, "Attribute", "Value"),
#"Merged Queries" = Table.NestedJoin(#"Unpivoted Other Columns",{"Value"},ID_Table,{"Person"},"ID_Table",JoinKind.LeftOuter),
#"Expanded ID_Table" = Table.ExpandTableColumn(#"Merged Queries", "ID_Table", {"ID"}, {"ID"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded ID_Table",{"Value"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "ID")
in #"Pivoted Column"

Related

Group by column A value, transpose column B, column C row values for each grouped column A value

This is in Excel 2016. I have a spreadsheet where each row represents a response to two questions "Qa" and "Qb" from a unique student. The spreadsheet columns are: "Section" (class section student is in), "Qa", and "Qb".
Thus, if three students answered from the same class section, that section will be listed three times under "Section", with each unique students answers in the other columns.
I want to group by section and spread the answers to each question across a single row in separate columns. The number of columns to create will default to the section with the most unique responses
In this case, 10003 has the greatest number of responses, so I want to get the following end result.
I am at a loss with how to get this going. Something like grouping by the section but transposing the rows within that group?
As #ScottCraner pointed out, you can obtain your desired output using Power Query, available in Windows Excel 2010+ and Office 365 Excel
Select some cell in your original table
Data => Get&Transform => From Table/Range
When the PQ UI opens, navigate to Home => Advanced Editor
Make note of the Table Name in Line 2 of the code.
Replace the existing code with the M-Code below
Change the table name in line 2 of the pasted code to your "real" table name
Examine any comments, and also the Applied Steps window, to better understand the algorithm and steps
M Code
let
//Change table name in next row to actual table name in workbook
Source = Excel.CurrentWorkbook(){[Name="Table20"]}[Content],
//set data type
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Section", Int64.Type}, {"Qa", type text}, {"Qb", type text}}),
//Group by Section
//Add a 1-based Index column to each Group
#"Grouped Rows" = Table.Group(#"Changed Type", {"Section"}, {
{"Row", each Table.AddIndexColumn(_,"Row",1,1)}}),
//Expand the grouped tables
#"Expanded Row" = Table.ExpandTableColumn(#"Grouped Rows", "Row", {"Qa", "Qb", "Row"}, {"Qa", "Qb", "Row"}),
//Unpivot
//Merge Row and Attribute columns to create the q-number headers
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Row", {"Section", "Row"}, "Attribute", "Value"),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Other Columns",
{{"Row", type text}}, "en-US"),{"Attribute", "Row"},
Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Merged"),
//Pivot on the Sorted Merged column with no aggregation
#"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Sort(List.Distinct(#"Merged Columns"[Merged])), "Merged", "Value")
in
#"Pivoted Column"
Note that there are no empty columns (iow, there is no Qa-4)
If you really need an empty column, insert a step at the beginning replacing nulls with a blank
let
//Change table name in next row to actual table name in workbook
Source = Excel.CurrentWorkbook(){[Name="Table20"]}[Content],
//set data type
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Section", Int64.Type}, {"Qa", type text}, {"Qb", type text}}),
//if you really need a blank Qa column since you have four distinct Qb rows but only 3 Qa rows,
// then we insert the next line
#"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,"",Replacer.ReplaceValue,{"Qa", "Qb"}),
//Group by Section
//Add a 1-based Index column to each Group
#"Grouped Rows" = Table.Group(#"Replaced Value", {"Section"}, {
{"Row", each Table.AddIndexColumn(_,"Row",1,1)}}),
//Expand the grouped tables
#"Expanded Row" = Table.ExpandTableColumn(#"Grouped Rows", "Row", {"Qa", "Qb", "Row"}, {"Qa", "Qb", "Row"}),
//Unpivot
//Merge Row and Attribute columns to create the q-number headers
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Row", {"Section", "Row"}, "Attribute", "Value"),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Other Columns",
{{"Row", type text}}, "en-US"),{"Attribute", "Row"},
Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Merged"),
//Pivot on the Sorted Merged column with no aggregation
#"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Sort(List.Distinct(#"Merged Columns"[Merged])), "Merged", "Value")
in
#"Pivoted Column"

Importing data to Power Query and filter if group of records has at least one conditional value

I hope I'm going to explain myself well enough. I'm importing spreadsheets from a folder into Power Query, but I want to filter out (drop) any groups of data (by a name column) where that group has at least one instance of a given value.
Example, I have a theoretical table with two columns. One has repeating names and the other has scattered values of A or B or C. I want the query to look for a group of names, then see if any of the records in that group has either and A, B or C in the second column. If found, it drops that entire group and if none, then it allows that entire group through.
I'm not sure if this is too complex for Power Query or whether I need to do this outside Excel. If the latter, what would the equivalent SQL statement be (for MS Access, as I'm trying to keep this as basic as possible)?
Theres about 500 ways to do this. One easy way is to filter Column2 for ABC, remove that column, then remove duplicates from Column1. That give you a unique list of groups to remove, and just merge that against the original data and remove those rows
sample:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
// all unique groups from Col1 that have a/b/c in Col2
#"Filtered Rows" = Table.SelectRows(Source, each ([Column2] = "a" or [Column2] = "b" or [Column2] = "c")),
#"Removed Columns"= Table.SelectColumns(#"Filtered Rows",{"Column1"})
#"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
// merge that into original table and filter
#"Merged Queries" = Table.NestedJoin(Source,{"Column1"},#"Removed Duplicates",{"Column1"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Column1"}, {"Column1.1"}),
#"Filtered Rows1" = Table.SelectRows(#"Expanded Table2", each ([Column1.1] <> null)),
#"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows1",{"Column1.1"})
in #"Removed Columns1"

Excel: Combine data from 2 tables

I am trying to achieve something I believe will be quite simple but just can't figure out on my own.
What I'm trying to achieve is a full list of product IDs + Filenames. All filenames are the same for each product ID.
Table 1: Product ID's
Table 2: File Names
Goal: Create a record for each product ID + Filename combination
Thanks in advance.
Assuming the file name table is loaded into PowerQuery and the table is named File_names_table, then
In the in the Product ID table, add column .. custom column ... and use formula:
= File_names_table
Then click the arrows atop the Custom column and expand to new rows
let Source = Excel.CurrentWorkbook(){[Name="Product_ID_Table"]}[Content],
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Added Custom" = Table.AddColumn(#"Promoted Headers", "Custom", each File_names_table),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"File names"}, {"File names"})
in #"Expanded Custom"

powerquery group by rows with formula on multiple columns

I am trying to group/merge two rows by dividing the values in each based on another column (Eligible) value.
From the initial raw data, I have reached this level with different steps (by unpivoting etc.) in power query.
Now I need to have a ratio per employee (eligible/not-eligible) for each month.
So for employee A, "Jan-14" will be -10/(-10 + -149) and so on. Any ideas will be appreciated. Thanks
Really appreciate the response. Interestingly, I have used your other answer to reach this stage from the raw data.
Since we are calculating how much time an employee worked on eligible activities each month so We will be grouping on the Employee. Employee name was just for reference which I took out and later will join with employee query to get the names if required. There was a typo in the image, the last row should also be an employee with id 2.
So now when there is a matching row, we use the formula to calculate the percentage of time spent on eligible activities but
If there isn't a matching row with eligible=1, then the outcome should be 0
if there isn't a matching row with eligible-0, then the outcome should be 1 (100%)
Try this and modify as needed. It assumes you are starting with all Eligible=0 and will only pick up matching Eligible=1. If there is a E=1 without E=0 it is removed. Also assumes we match on both Employee and EmployeeName
~ ~ ~ ~ ~
Click select the first three columns (Employee, EmployeeName, Eligible), right click .... Unpivot other other columns
Add custom column with name "One" and formula =1+[Eligible]
Merge the table onto itself, Home .. Merge Queries... with join kind Left Outer
Click to match Employee, EmployeeName and Attribute columns in the two boxes, and match One column in the top box to the Eligible Column in the bottom box
In the new column, use arrows atop the column to expand, choosing [x] onlt the Value column. Make the name of the column: Custom.Value
Add column .. custom column ... formula = [Custom.Value] / ( [Custom.Value] + [Value])
Filter Eligible to only pick up the zeroes using the arrow atop that column
Remove extra columns
Click select Attribute column, Transform ... pivot ... use custom as the values column
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Employee", "EmployeeName", "Eligible"}, "Attribute", "Value"),
#"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "One", each 1+[Eligible]),
#"Merged Queries" = Table.NestedJoin(#"Added Custom",{"Employee", "EmployeeName", "Attribute", "One"},#"Added Custom",{"Employee", "EmployeeName", "Attribute", "Eligible"},"Added Custom",JoinKind.LeftOuter),
#"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"Value"}, {"Custom.Value"}),
#"Added Custom1" = Table.AddColumn(#"Expanded Added Custom", "Custom", each [Custom.Value]/([Custom.Value]+[Value])),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Eligible] = 0)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Eligible", "Value", "One", "Custom.Value"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Custom", List.Sum)
in #"Pivoted Column"

How can you get Power Query/Excel to separate and associate two unique IDs from the same column?

I have a dataset that exports with a single column including personnel IDs and job IDs.
I want to use Power Query separate Person_ID into one column and Job_ID into another column. People are associated with the job that appears closest above them. Job IDs are a 6-character text string, Person IDs are 9 character. The same Job_ID can apply to multiple people, but Person_ID is unique (only one job per person, multiple people for some jobs).
Example data structure:
Hope someone's got something!
Step by step
Highlight input data
Data...From Table/Range... do not check [] my table has headers
Add Column...Custom Column... using column name Custom, with formula
Text.Length([Column1])
Add Column...Custom Column... using column name Custom.1, with formula
if [Custom]=6 then [Column1] else null
Click on Custom.1 column, right click and do fill...down...
Use arrow next to Custom column and uncheck [] 6 leaving just [x]11
Click column Custom, right click and choose remove columns
file...close and load
Code produced:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Length([Column1])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each if [Custom]=6 then [Column1] else null),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"Custom.1"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Custom] =11)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"})
in #"Removed Columns"

Resources