cancel
Showing results for 
Search instead for 
Did you mean: 
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
Microsoft
Microsoft

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
Microsoft
Microsoft

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

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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.