Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Intersect and create a new table.

I have two tables.

Table 1

idtime_stampproduct_name
   

 

Table 2

idtime_stampproduct_name
   

 

I would like to create a new table

 

where id column should contain table2[id] where table2[id] is present in table1[id],

 

and table1[time_Stamp] > table2[time_stamp]

 

result calculated table

idtable1[time_stamp]table2[time_stamp]diff[table2[time_stamp] - table1[time_stamp]]table1[product_name]table2[product_name]
      

 

 

Can anyone please give me some idea where should I start?

 

I know how to get table2[id]s present in table1[id] using intersect. But don't know to proceed next. 

 

1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

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

Table1:

i1.png

Table2:

i2.png

 

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:

i3.png

 

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:

i4.png

 

Best Regards

Allan

 

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

View solution in original post

3 REPLIES 3
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

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

Table1:

i1.png

Table2:

i2.png

 

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:

i3.png

 

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:

i4.png

 

Best Regards

Allan

 

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

Fowmy
Super User
Super User

@Anonymous 

If you need to implement a DAX solution, create a new table with the following code:

New Table = 

SELECTCOLUMNS(
    FILTER(
        GENERATE( 
            Table1, 
            SELECTCOLUMNS(
                Table2,
                "_T2 ID", Table2[id],
                "_T2 Time", Table2[time_stamp],
                "_T2 Product Name", Table2[product_name]
            )
        ),
        Table1[id] = [_T2 ID] && Table1[time_stamp] > [_T2 Time]
    ),
    "T1 ID", Table1[id], 
    "T1 Time", Table1[time_stamp],
    "T2 Time", [_T2 Time],
    "Time Diff",DATEDIFF(Table1[time_stamp] , [_T2 Time],MINUTE),
    "T1 Product Name",Table1[product_name],
    "T2 Product Name",[_T2 Product Name]
)

 

These are the two tables I made for demo:

Fowmy_0-1611411607889.pngFowmy_1-1611411618802.png

 

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

ebeery
Solution Sage
Solution Sage

@Anonymous Maybe this is just my personal preference, but I would use Power Query for this, unless there's a specific reason not to.

With Table 1 and 2 like you've described them, basic steps would be to inner join Table 2 with Table 1, expand table 1, and filter rows based on Table1.time_stamp > Table2.time_stamp.

Power Query code would look something like below.

let
  Source = Table.NestedJoin(Table2, {"id"}, Table1, {"id"}, "Table1", JoinKind.Inner),
  #"Expanded Table1" = Table.ExpandTableColumn(
    Source,
    "Table1",
    {"id", "Table1.time_stamp", "Table1.product_name"},
    {"id.1", "Table1.time_stamp", "Table1.product_name"}
  ),
  #"Filtered Rows" = Table.SelectRows(
    #"Expanded Table1",
    each [Table1.time_stamp] > [Table2.time_stamp]
  )
in
  #"Filtered Rows"

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors