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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
freidav1984
Frequent Visitor

How to left join / merge two tables but returning only the first matching record

Hi BI folks!

 

I am facing the following issue, could anybody help me?

 

I have two calculated table variables. Let's say:

 

Var VarTableA =

SELECTCOLUMNS(TableA,

"ID", TableA[ID],

"A", TableA[A],

"B", TableA[B]

)

 

Var VarTableB =

SELECTCOLUMNS(TableB,

"ID", TableB[ID],

"C", TableB[C],

"D", TableB[D]

)

 

I would like to merge VarTableB to VarTableA, but because, in VarTableB each ID record is duplicated, I would like to join each of the matching first ID records from VarTableB. 

Unfortunately, from technical issues, it is not an option for me to use power query in order to eliminate duplicates from VarTableB.

 

I would like to get this result by using DAX. Any thoughts on this, any help is really appreciated!

 

freidav1984_0-1662492775404.png

 

Thx!

David

 

1 ACCEPTED SOLUTION
truptis
Community Champion
Community Champion

Hi @freidav1984 ,
Please follow the below steps:
1- create a flag column. 

You can first create a calculate column in the normal table to mark whether there is a entry in your table, and then create a measure to get the first value or "NA".

Flag = IF('VarTableB'[ID] IN VALUES(VarProductB[ID]), 1, 0)

Result =
VAR _count =
CALCULATE (
COUNT ( ('VarTableB'[ID] ),
ALLEXCEPT ( 'VarTableB', ('VarTableB'[ID] , 'VarTableB'[C], 'VarTableB'[D] ),
'VarTableB'[Flag] = 1
)
VAR _firstValue =
CALCULATE (
FIRSTNONBLANK ( 'VarTableB'[ID] , 'VarTableB'[ID] ),
ALLEXCEPT ( 'VarTableB', 'VarTableB'[ID] , 'VarTableB'[C], 'VarTableB'[D]),
'VarTableB'[Flag] = 1
)
RETURN
IF ( ISBLANK ( _count ), "NA", _firstValue )


Later you can use this table to do your mapping with VarTableA

@freidav1984 -> Please hit the thumbs up if it helps you. Thanks.

View solution in original post

2 REPLIES 2
parry2k
Super User
Super User

@freidav1984 you can also create a calculated table using following DAX expression:

 

Table Result = 
SELECTCOLUMNS ( 
    NATURALINNERJOIN ( 
        DISTINCT ( TableB ),
        TableA
    ),
    "ID", TableA[ID],
    "A", TableA[A],
    "B", TableA[B],
    "C", TableB[C],
    "D", TableB[D]
)

 

parry2k_0-1662496318211.png

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make effort to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

truptis
Community Champion
Community Champion

Hi @freidav1984 ,
Please follow the below steps:
1- create a flag column. 

You can first create a calculate column in the normal table to mark whether there is a entry in your table, and then create a measure to get the first value or "NA".

Flag = IF('VarTableB'[ID] IN VALUES(VarProductB[ID]), 1, 0)

Result =
VAR _count =
CALCULATE (
COUNT ( ('VarTableB'[ID] ),
ALLEXCEPT ( 'VarTableB', ('VarTableB'[ID] , 'VarTableB'[C], 'VarTableB'[D] ),
'VarTableB'[Flag] = 1
)
VAR _firstValue =
CALCULATE (
FIRSTNONBLANK ( 'VarTableB'[ID] , 'VarTableB'[ID] ),
ALLEXCEPT ( 'VarTableB', 'VarTableB'[ID] , 'VarTableB'[C], 'VarTableB'[D]),
'VarTableB'[Flag] = 1
)
RETURN
IF ( ISBLANK ( _count ), "NA", _firstValue )


Later you can use this table to do your mapping with VarTableA

@freidav1984 -> Please hit the thumbs up if it helps you. Thanks.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.