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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
purple_SP
Helper I
Helper I

Searching for matching elements with two comma-separated columns

Hi all,

 

I have two tables with a one-to-many relationship. Both have a column with a list of values seperated by a comma:

 

eg: Table 1 (One side)

Name Pets
Bob dog, fish
Sally cat, chicken
Andy fish, dog, cat

 

Table 2 (Many side)

Name Grocery Shopping
Bob chicken, beef
Sally beef, milk, chicken
Andy cheese, fish

 

I want to create a calculated column in Table 2 that returns 'Y' if one of the elements in grocery shopping are found in the pets column or "N" otherwise. (I'd prefer to do this in DAX)

 

eg: 

Name Grocery Shopping matching item
Bob chicken, beef N
Sally beef, milk, chicken Y
Andy cheese, fish Y

 

I feel like one way to do this would be to break the grocery shopping into elements separated by the comma and use CONTAINSSTRING() for each of the elements, but I am unsure how to achieve this in DAX/PBI.

 

Any help would be much appreciated!

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@purple_SP or you can add a new column in Table2 using the following expression, change the table name and column name as per your model.

 

Matching Item = 
VAR __shoppingValue = SUBSTITUTE (Table2[ Grocery Shopping],",","|" )
VAR __totalValues = PATHLENGTH (__shoppingValue )
VAR __petsList = RELATED (Table1[ Pets] )
VAR __matchingTable = 
    ADDCOLUMNS (
        GENERATESERIES (1, __totalValues ), 
        "@IsMatched", CONTAINSSTRING ( __petsList, PATHITEM ( __shoppingValue, [Value],TEXT ) ) + 0 
    )
VAR __matchCount = SUMX ( __matchingTable, [@IsMatched])
RETURN IF ( __matchCount > 0, "Yes", "No" )

👉 Learn Power BI Subscribe to our YT channel - @PowerBIHowTo



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.

View solution in original post

3 REPLIES 3
parry2k
Super User
Super User

@purple_SP or you can add a new column in Table2 using the following expression, change the table name and column name as per your model.

 

Matching Item = 
VAR __shoppingValue = SUBSTITUTE (Table2[ Grocery Shopping],",","|" )
VAR __totalValues = PATHLENGTH (__shoppingValue )
VAR __petsList = RELATED (Table1[ Pets] )
VAR __matchingTable = 
    ADDCOLUMNS (
        GENERATESERIES (1, __totalValues ), 
        "@IsMatched", CONTAINSSTRING ( __petsList, PATHITEM ( __shoppingValue, [Value],TEXT ) ) + 0 
    )
VAR __matchCount = SUMX ( __matchingTable, [@IsMatched])
RETURN IF ( __matchCount > 0, "Yes", "No" )

👉 Learn Power BI Subscribe to our YT channel - @PowerBIHowTo



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.

Thanks @parry2k this also worked for me and is cleaner than what I had.

purple_SP
Helper I
Helper I

I was able to find a solution to this problem (with many thanks to this post: Solved: DAX To Split into Rows on Delimeter - Microsoft Fabric Community ) by splitting the first list into rows by a delimiter, using CONTAINSSTRING to create a column that is 1 if the item in the first list exists in the second and 0 otherwise, and using SUMX with an if statement to get the final result.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.