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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
IgorDo
New Member

Megre Query with Condition

Hi all,

 

I have a table A with say two columns: "Unique ID" and "Type". And I have a table B with another three columns: "Unique ID", "Type", and "Value".

 

I would like to merge them in such fashion, that when "Type"=1, then the corresponding value from table B is taken, and only when there is no entry with "Type"=1, I would take other valuee from table B. (dont bother about multiple entries and so on... I solved it).

 

In SQL I would solve it with two consequtive joins and a where condition. How would I do it in PowerBI Query Editor?

 

Many thanks in advance,

 

Igor.

1 REPLY 1
MarcelBeug
Community Champion
Community Champion

You can do something similar like in the query below.

 

This will merge on ID and Type and merge again only on ID.

Then a column is added that picks one of the 2 nested Tables B.

It may not be the correct interpretation (as I'm confused as where the type 1 should be found: in table A or table B), but this would be the direction:

 

let
    Source = Table.NestedJoin(TableA,{"Unique ID", "Type"},TableB,{"Unique ID", "Type"},"TableB_Types1",JoinKind.LeftOuter),
    #"Merged Queries" = Table.NestedJoin(Source,{"Unique ID"},TableB,{"Unique ID"},"TableB_Other",JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(#"Merged Queries", "TableB", each if [Type] = 1 then [TableB_Types1] else [TableB_Other]),
    #"Expanded TableB" = Table.ExpandTableColumn(#"Added Custom", "TableB", {"Type", "Value"}, {"TableB.Type", "TableB.Value"})
in
    #"Expanded TableB"
Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.