Forum Discussion

FabryZ's avatar
FabryZ
Frequent Visitor
4 years ago
Solved

How to relate two tables without a common key (easy in SQL, but in Power Query?)

Hi all, I'm new to Power query (so, I apologize for stupid questions...), but I'm trying to compare sql query and power query, and I'm not able to merge these two tables. table1: id_emp, name, sa...
  • KT_Bsmart2gethe's avatar
    KT_Bsmart2gethe
    4 years ago

    Hi FabryZ ,

     

    I downloaded the example file.

     

    I updated the code provided by Anonymous and removed "each" from the syntax.

     

    Copy the code below to a blank query to the example file and you know how it works.

     

    Code:

    let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id_emp", Int64.Type}, {"name", type text}, {"salary", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Add Table 2", (x)=> Table.SelectRows( Table2, each x[salary]>[min_sal] and x[salary]<[max_sal])),
    #"Expanded Add Table 2" = Table.ExpandTableColumn(#"Added Custom", "Add Table 2", {"id_cat", "min_sal", "max_sal"}, {"id_cat", "min_sal", "max_sal"})
    in
    #"Expanded Add Table 2"

     

     Regards

    KT