Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

How to filter a column based on a column from another table?

I have Table 1 that contains a column of ALL of my employees:

Employee IDSales Value
125
250
375
4100
5100


I have Table 2 that contains a SUBSET of employees and their info:

Employee IDFirst NameLast Name
2JonDoe
4JaneDoe


I want to filter my Table 1, so that it only contains rows that show up in Table 2. Table 1 after the filter should look like this:

Employee IDSales Value
250
4100

 

How do I achieve this? Bonus: is there a way to achieve this without doing a Join? The tables are millions of rows long, so I don't know how join would impact performance.

  • Hi Anonymous 

    An inner join should do it and is quite probably the best performing option 

    Place the following M code in a blank query to see the steps.

    let
        Source = Table.NestedJoin(Table1, {"Employee ID"}, Table2, {"Employee ID"}, "Table2", JoinKind.Inner),
        #"Removed Columns" = Table.RemoveColumns(Source,{"Table2"})
    in
        #"Removed Columns"

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

     

2 Replies

  • AlB's avatar
    AlB
    Community Champion

    Hi Anonymous 

    An inner join should do it and is quite probably the best performing option 

    Place the following M code in a blank query to see the steps.

    let
        Source = Table.NestedJoin(Table1, {"Employee ID"}, Table2, {"Employee ID"}, "Table2", JoinKind.Inner),
        #"Removed Columns" = Table.RemoveColumns(Source,{"Table2"})
    in
        #"Removed Columns"

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

     

  • Icey's avatar
    Icey
    Community Support

    Hi Anonymous ,

     

    You can also try this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyVYrViVYyAjJNDcBMYyDTHCJqAmQaGkCETWHsWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Employee ID" = _t, #"Sales Value" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee ID", Int64.Type}, {"Sales Value", Int64.Type}}),
        #"Filtered Table1" = Table.SelectRows(#"Changed Type", each List.Contains(#"Table2"[Employee ID],[Employee ID]))
    in
        #"Filtered Table1"

     

     

    Best Regards,

    Icey

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.