Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Hello,
I am looking to have power query find a value from one table that matches a value from another table, and draw up a specific text from another column.
For example:
Table 1:
Selection Name | Code |
A | Code A |
B | Code B |
C | Code C |
D | Code D |
E | Code E |
F | Code F |
G | Code G |
H | Code H |
I | Code I |
J | Code J |
Table 2:
Selection 1 | Selection 2 | Selection 3 | Selection 4 | Selection 5 | Selection 6 |
A | C | D | G | F | I |
B | G | C | A | I | J |
C | D | E | F | G | H |
Desired output:
Selection 1 | Selection 2 | Selection 3 | Selection 4 | Selection 5 | Selection 6 |
Code A | Code C | Code D | Code G | Code F | Code I |
Code B | Code G | Code C | Code A | Code I | Code J |
Code C | Code D | Code E | Code F | Code G | Code H |
I feel like the process should be rather simple, but everything I have tried so far doesn't seem to work.
Thanks!
Solved! Go to Solution.
In Power Query
I used an Excel worksheet for my data source. You can change those first four lines to reflect your actual data sources
let
//Change next four lines to reflect your actual data sources
Source = Excel.CurrentWorkbook(){[Name="tbl_1"]}[Content],
#"Table 1" = Table.TransformColumnTypes(Source,{{"Selection Name", type text}, {"Code", type text}}),
Source2 = Excel.CurrentWorkbook(){[Name="tbl_2"]}[Content],
#"Table 2" =Table.TransformColumnTypes(Source2, List.Transform(Table.ColumnNames(Source2), each {_, type text})),
//add index column to maintain original row order
//then unpivot
#"Added Index" = Table.AddIndexColumn(#"Table 2", "Index", 0, 1, Int64.Type),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
//Join the tables to do the "lookup"
Join = Table.Join(#"Unpivoted Other Columns",{"Value"}, #"Table 1",{"Selection Name"}),
#"Removed Columns" = Table.RemoveColumns(Join,{"Value", "Selection Name"}),
//Sort back to original order
//Then Pivot with no aggregation and delete the Index column
#"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Attribute", Order.Ascending}, {"Index", Order.Ascending}}),
#"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute]), "Attribute", "Code"),
#"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
#"Removed Columns1"
Original Tables
Results
if selection names in table 1 is unique, try this code
NewStep=Table.TransformColumns(Table2,{},each Table1{[Selection Name=_]}?[Code]?)
In Power Query
I used an Excel worksheet for my data source. You can change those first four lines to reflect your actual data sources
let
//Change next four lines to reflect your actual data sources
Source = Excel.CurrentWorkbook(){[Name="tbl_1"]}[Content],
#"Table 1" = Table.TransformColumnTypes(Source,{{"Selection Name", type text}, {"Code", type text}}),
Source2 = Excel.CurrentWorkbook(){[Name="tbl_2"]}[Content],
#"Table 2" =Table.TransformColumnTypes(Source2, List.Transform(Table.ColumnNames(Source2), each {_, type text})),
//add index column to maintain original row order
//then unpivot
#"Added Index" = Table.AddIndexColumn(#"Table 2", "Index", 0, 1, Int64.Type),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
//Join the tables to do the "lookup"
Join = Table.Join(#"Unpivoted Other Columns",{"Value"}, #"Table 1",{"Selection Name"}),
#"Removed Columns" = Table.RemoveColumns(Join,{"Value", "Selection Name"}),
//Sort back to original order
//Then Pivot with no aggregation and delete the Index column
#"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Attribute", Order.Ascending}, {"Index", Order.Ascending}}),
#"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute]), "Attribute", "Code"),
#"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
#"Removed Columns1"
Original Tables
Results
Thank you so much!!
I was able to get it to work I think, but it is only showing the first column, Selection 1. It is not populating any columns or codes for Selections 2-6. Any ideas on what I could be doing wrong?
Probably something in your actual data or how you adapted the code I provided to your exact situation. Clearly, from the screenshots I provided, the code works for two tables identical to what you provided with an Excel Source. But without seeing exactly what you have and what you did, I don't have a clue as to what changes you might need to make.
Ah, nevermind. I realized that some of the data in the Selection Name column of Table 1 did not match what was in Table 2. Got it fixed and my desired output table. Thank you sooo much for this! 🙂
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
13 | |
13 | |
10 | |
8 | |
7 |
User | Count |
---|---|
17 | |
10 | |
7 | |
6 | |
6 |