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

Get Fabric certified for FREE! Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Power BI Conditional Merge

Hi all,

 

I need to join 2 tables based on a column that is not always the same in both tables

 

The problem:

Table 1 (Book100):

Book100.PNG

Table 2 (BookSummary):

Booksummary.PNG

 

Now I want to join these tables based on the column named 'Booktype', but as you can see in Table 2 every 'Bookcode' has a 'Typecode' (Bookcode_Typecode). But in Table 1 it can be that some rows only have a Bookcode without having a Typcode.

 

What I want is, that if 'Booktype' in Table 1 has both 'Bookcode' as 'Typecode' it should merge with the 'Booktype' of Table 2. But if 'Booktype' in Table 1 does not have a 'Typecode' it should merge to all 'Bookcodes' that are in the 'Booktype' column of Table 2 (so only the part before the underscore _).

 

In a IF statement it would be:
IF([Book100]Typecode = ""; [BookSummary]Booktype ONLY THE PART BEFORE THE _ ; [BookSummary]Booktype)

 

Advanced editor:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY3BDcAwCAN3yTsPYqjUrtBmgyhi/y0CTsMH31mCMQqaiLRSyydNYm120qzRqzwW/sWNrBOdwBZQDZ0NY0pTwy933FIt73R+IXk/P3DFnDPMcy4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Bookcode = _t, Typecode = _t, BookType = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Bookcode", Int64.Type}, {"Typecode", type text}, {"BookType", type text}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"BookType"}, BookSummary, {"BookType"}, "BookSummary", JoinKind.LeftOuter),
#"Expanded BookSummary" = Table.ExpandTableColumn(#"Merged Queries", "BookSummary", {"Author", "Year"}, {"BookSummary.Author", "BookSummary.Year"})
in
#"Expanded BookSummary"

 

Hopefully someone can help me change the current merge to a conditional merge, I would appreciate it so much!

 

Thanks in advance!

1 ACCEPTED SOLUTION
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

Try this script, or see the attached for details

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI0MDAwVNJR8jYwNFCK1QGKGBtYmgAFvIwsjCACRkbGxkABMMfE2MQIiWNsAlLqA9NqZAoEYNlYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Bookcode = _t, Typecode = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Bookcode", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", 
        (a) => Table.SelectRows( 
            BookSummary, 
            (b) => 
                ( a[Bookcode] = b[Bookcode.1] ) and 
                ( if a[Typecode] <> "" then a[Typecode] = b[Typecode.1] else true ) 
            ) 
    )
in
    #"Added Custom"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

View solution in original post

6 REPLIES 6
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

Try this script, or see the attached for details

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI0MDAwVNJR8jYwNFCK1QGKGBtYmgAFvIwsjCACRkbGxkABMMfE2MQIiWNsAlLqA9NqZAoEYNlYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Bookcode = _t, Typecode = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Bookcode", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", 
        (a) => Table.SelectRows( 
            BookSummary, 
            (b) => 
                ( a[Bookcode] = b[Bookcode.1] ) and 
                ( if a[Typecode] <> "" then a[Typecode] = b[Typecode.1] else true ) 
            ) 
    )
in
    #"Added Custom"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Anonymous
Not applicable

@MariuszThank you so much! Looking at your PBI file I have managed it to work, greatly appreciated!

Greg_Deckler
Community Champion
Community Champion

I'm not entirely sure how you would do that exactly. Maybe use the Fuzzy merge options? @ImkeF @edhans ?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thanks for sharing it with others!

 

I was hoping that something can be added inside the Advanced Editor, but couldn't figure it out...

If anyone will know it is the folks that I referenced. But I was thinking this:

 

image.png



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

I believe that fuzzy matching is only useful when the format is the same for all rows and there are some minor changes/typos in some rows. As for me, the format isn't the same for all rows I am afraid that this won't work in my case.

Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

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.