Excel: Combine data from 2 tables - excel

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"

Related

Excel Power Query - add conditional column by looking up data from another Table

I am looking to add a conditional column in power query by looking up data from another table.
As an example, my lookup data is as follows:
My data is as follows:
Now I want to check if the number in my data is between Begin & End of my Master data & if the condition is satisfied, then I want to add a column by coping the corresponding "Digits" Cell to my Data. The result should look like this:
Can someone point me on how to do this?
Any help is greatly appreciated.
Thanks in Advance
Load your your top table into powerquery with the query name LookupQuery and file ... close and load. It should have the column names you show
Then load the middle source table into powerquery and Add column ... custom column... with formula
= Table.SelectRows(LookupQuery,(x) => [Data] >= x[Begin] and [Data] <=x[End] )[Digits]{0}
full code:
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.SelectRows(LookupQuery,(x) => [Data] >= x[Begin] and [Data] <=x[End] )[Digits]{0})
in #"Added Custom"
another way is to do a match based on length of the digits. This would work better for large data sets Load your middle source data into powerquery and
add column ... custom column ... with formula
= Text.Length(Text.From([Data]))
merge your data with with lookup query and replace the formula in the formula bar with :
= Table.NestedJoin(#"Added Custom", {"Custom"}, Table.AddColumn(LookupQuery, "Custom", each Text.Length(Text.From([Begin]))), {"Custom"}, "LookupQuery", JoinKind.LeftOuter)
expand the Digits column
full code for the 2nd method:
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Length(Text.From([Data]))),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Custom"}, Table.AddColumn(LookupQuery, "Custom", each Text.Length(Text.From([Begin]))), {"Custom"}, "LookupQuery", JoinKind.LeftOuter),
#"Expanded LookupQuery" = Table.ExpandTableColumn(#"Merged Queries", "LookupQuery", {"Digits"}, {"Digits"})
in #"Expanded LookupQuery"

Merging tables in excel using power query

im using a certain config of tables to generate a cascading dropdown list that looks as such:
I wanna merge these tables to look as such using excels powerquery:
Since each dropdown list is a different table, you have to create a list of all the tables.
Creating a list of tables for PQ to operate on can be tricky.
In my example, they were named Table19 - Table26 so I used the List.Generate function to do that. But there are other ways to generate such a list, including just hard-coding it.
Then it's just a matter of reading in all the tables, unpivoting and expanding.
See the comments in the code for better understanding.
M Code
let
//create a list of the relevant table names
// and create a column of the names
//many ways to do this
//Since mine are table19-26, I do it like this:
tableNames = List.Generate(
()=>[Table Name = "Table" & "19", IDX=20],
each [IDX] < 28,
each [Table Name = "Table" & Text.From([IDX]), IDX = [IDX]+1],
each [Table Name]
),
#"Converted to Table" = Table.FromList(tableNames, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
//Add custom column containing the actual table referred to in column1
//then delete the table names column
#"Added Custom" = Table.AddColumn(#"Converted to Table", "Tables", each Excel.CurrentWorkbook(){[Name=[Column1]]}[Content]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column1"}),
//add custom column to unpivot each of the tables
//then remove the original tables column and expand the unpivoted column
#"Added Custom1" = Table.AddColumn(#"Removed Columns", "unPivot", each
Table.UnpivotOtherColumns([Tables],{},"Type","Subtype")),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Tables"}),
#"Expanded unPivot" = Table.ExpandTableColumn(#"Removed Columns1", "unPivot", {"Type", "Subtype"}, {"Type", "Subtype"})
in
#"Expanded unPivot"

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 to Merge Query for multiple column

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"

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