Forum Discussion

alpb's avatar
alpb
Frequent Visitor
4 years ago
Solved

Remove rows which column value does not exist in another table

I have two tables.
bigger_table contains data such as
customer_id sales           email               address           phone_number
a1                 100             
b2                  50
c3                   20
 
smaller_table contains data such as
customer_id     sales
a1                      100
b2                       50
 
In the bigger table, I would like to delete rows which customer id is not found in smaller_table. 
I have tried the two formulas below, but none works.
 
exists in smaller table = CONTAINS('bigger_table', 'bigger_table'[customer_id], 'smaller_table'[customer_id])
exists in smaller table = 
New Column = CALCULATE(COUNTROWS(bigger_table), FILTER(bigger_table, 'bigger_table'[customer_id]='smaller_table'[customer_id])) > 0

 

Expected Output:

customer_id sales           email               address           phone_number
a1                 100             
b2                  50
  • Hi alpb 

     

    Try this code to add a new table to your report:

    New Table =
    CALCULATETABLE (
        bigger_table,
        INTERSECT (
            VALUES ( bigger_table[customer_id] ),
            VALUES ( smaller_table[customer_id] )
        )
    )

     

    bigger_table:
    smaller_table:

     

    Output [New Table]:

     

     

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

    Appreciate your Kudos  !!

     

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi alpb ,

     

    You could refer to VahidDM 's method which used DAX function: INTERSECT()

     

    Or use merge in Power Query,below are the steps:

    1.Select Merge Queries > Merge Queries/Merge Queries as New from the Home tab on the ribbon.

     

    2.Select Customer_id column in these two tables, choose Inner join kind

    3.Remove the new added column:

     

    Here is the whole M code in Advanced Editor dialog:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjRU0lEyNDAAkhUViUqxOtFKSUZAjilEJAkskmwM5BhBRJKVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer_id = _t, Sales = _t, Email = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer_id", type text}, {"Sales", Int64.Type}, {"Email", type text}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Customer_id"}, smaller_table, {"Customer_id"}, "smaller_table", JoinKind.Inner),
        #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"smaller_table"})
    in
        #"Removed Columns"

     

    Refer to:

    https://www.sqlbi.com/articles/set-functions-in-dax-union-intersect-and-except/

     

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

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi alpb ,

     

    You could refer to VahidDM 's method which used DAX function: INTERSECT()

     

    Or use merge in Power Query,below are the steps:

    1.Select Merge Queries > Merge Queries/Merge Queries as New from the Home tab on the ribbon.

     

    2.Select Customer_id column in these two tables, choose Inner join kind

    3.Remove the new added column:

     

    Here is the whole M code in Advanced Editor dialog:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjRU0lEyNDAAkhUViUqxOtFKSUZAjilEJAkskmwM5BhBRJKVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer_id = _t, Sales = _t, Email = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer_id", type text}, {"Sales", Int64.Type}, {"Email", type text}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Customer_id"}, smaller_table, {"Customer_id"}, "smaller_table", JoinKind.Inner),
        #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"smaller_table"})
    in
        #"Removed Columns"

     

    Refer to:

    https://www.sqlbi.com/articles/set-functions-in-dax-union-intersect-and-except/

     

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

  • Hi alpb 

     

    Try this code to add a new table to your report:

    New Table =
    CALCULATETABLE (
        bigger_table,
        INTERSECT (
            VALUES ( bigger_table[customer_id] ),
            VALUES ( smaller_table[customer_id] )
        )
    )

     

    bigger_table:
    smaller_table:

     

    Output [New Table]:

     

     

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

    Appreciate your Kudos  !!