Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Intersect and create a new table.

I have two tables. Table 1 id time_stamp product_name         Table 2 id time_stamp product_name         I would like to create a new table   where id column should...
  • v-alq-msft's avatar
    5 years ago

    Hi, Anonymous 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table1:

    Table2:

     

    You may create a new query with the following m codes in 'Advanced Editor'.

    let
        Source = Table.AddColumn(Table1,"New",each Table.SelectRows(Table2,(x)=>x[id]=_[id] and x[time_stamp]<_[time_stamp])),
        #"Expanded New" = Table.ExpandTableColumn(Source, "New", {"id", "time_stamp", "product_name"}, {"T2.id", "T2.time_stamp", "T2.product_name"}),
        Custom1 = Table.SelectRows(#"Expanded New",each [T2.id]<>null)
    in
        Custom1

     

    Result:

     

    Or you may try creating a calculated table with following dax.

    Table = 
    SELECTCOLUMNS(
        FILTER(
            CROSSJOIN(
                Table1,
                Table2
            ),
            Table1[id]=Table2[id]&&Table1[time_stamp]>Table2[time_stamp]
        ),
        "T1_id",Table1[id],
        "T2_id",Table2[id],
        "T1_time",Table1[time_stamp],
        "T2_time",Table2[time_stamp],
        "T1_pname",Table1[product_name],
        "T2,pname",Table2[product_name]
    )

     

    Result:

     

    Best Regards

    Allan

     

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