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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Comparing two tables - individual rows in direct query

Hello Gurus,

 

          I have 2 tables in direct query (Table A & Table B).  Table is joined with Id, Now I need to compare 2 tables based of date and show that lastest date row as output.

Table A

IdNameSalDate
1A608.1.2019
2B808.1.2019
3C507.1.2019

 

Table B

IdNameSalDate
1A709.1.2019
2B507.1.2019
3C709.1.2019

 

Output : Based on the max date compared between two tables, I need to show the records

 

IdNameSalDate
1A709.1.2019
2B808.1.2019
3C709.1.2019

 

Thanks in Advance,

Shamar

1 ACCEPTED SOLUTION
v-cherch-msft
Employee
Employee

Hi @Anonymous

 

You may use 'Merge queries' and then add condition column to get the latest date.Attached the sample file for your reference.

let
    Source = Table.NestedJoin(TableA,{"Id"},TableB,{"Id"},"TableB",JoinKind.LeftOuter),
    #"Expanded TableB" = Table.ExpandTableColumn(Source, "TableB", {"Sal", "Date"}, {"TableB.Sal", "TableB.Date"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded TableB", "Custom", each if [Date] > [TableB.Date] then [Date] else [TableB.Date]),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Custom.1", each if [Custom] = [Date] then [Sal] else if [Custom] = [TableB.Date] then [TableB.Sal] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column1",{"Sal", "Date", "TableB.Sal", "TableB.Date"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}, {"Custom.1", "Sales"}})
in
    #"Renamed Columns"

Regards,

Cherie

Community Support Team _ Cherie Chen
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-cherch-msft
Employee
Employee

Hi @Anonymous

 

You may use 'Merge queries' and then add condition column to get the latest date.Attached the sample file for your reference.

let
    Source = Table.NestedJoin(TableA,{"Id"},TableB,{"Id"},"TableB",JoinKind.LeftOuter),
    #"Expanded TableB" = Table.ExpandTableColumn(Source, "TableB", {"Sal", "Date"}, {"TableB.Sal", "TableB.Date"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded TableB", "Custom", each if [Date] > [TableB.Date] then [Date] else [TableB.Date]),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Custom.1", each if [Custom] = [Date] then [Sal] else if [Custom] = [TableB.Date] then [TableB.Sal] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column1",{"Sal", "Date", "TableB.Sal", "TableB.Date"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}, {"Custom.1", "Sales"}})
in
    #"Renamed Columns"

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Hi V-chech-msft,

 

       Thanks  for your solution, There is other part in my scenario is 

 

if ( TableA. Date > TableB.Date )

then  - > show all columns from TableA

else  -> show all columns from Table B

 

How to achieve this?

 

Thanks,

Shamar

Hi @Anonymous

 

I would suggest you create a new thread on forum so that more community members can see it and provide advice. Please remember to post dummy data and desired result.

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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