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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Replace comma separated IDs in a column from table 1 with values from table 2

I have two tables where table 1 acts as a fact table with a column that contains the IDs from another table. Each row can contain no ID, one ID, or more than one ID separated by a comma. The second table acts as a dimension table where column 1 is the unique ID and column 2 is the unique value for that ID. This table is dynamic as rows can be added should new values be needed. There are around 20 or so unique IDs/Labels. At this point, the most lables placed in a single row is five, but that could change based on business need. 

 

Help:

What I am looking for is how to stand up a column in table 1 that returns the values for the ID(s) in that row's column. 

 

What I have tried:

When I manage the relationships between the two tables, joining on ID columns, this works only for those rows where there is one ID in table 1. Anytime there is more than one ID in a field, nothing gets returned. I have tried using some DAX formulas as well as power query but can't seem to return values where there is more than one ID.

 

Here are three tables to show what I have and what I would like to accomplish:

 

Table 1

Card_IDCard_Label_ID
abc123a1s2d3f4
def456b5n6n7m8, a1s2d3f4
ghi789 

 

Table 2

Label_IDLabel_Value
a1s2d3f4Bug
b5n6n7m8Maintenance
c0v9b8n7Enhancement

 

Table 3 - Desired Output for Table 1

Card_IDCard_Label_IDCard_Label_Value
abc123a1s2d3f4Bug
def456b5n6n7m8, a1s2d3f4Maintenance, Bug
ghi789  

 

Any help would be greatly appreciated!

 

Thanks

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@Anonymous See what you think of this solution. PBIX is attached after sig. Table (4) and Table (4a):

Column = 
VAR __Table =
    ADDCOLUMNS(
        GENERATE(
            { [Card_ID] },
            'Table (4a)'
        ),
        "Replace",IF(FIND([Label_ID],[Card_Label_ID],,0)>0,[Label_Value],BLANK())
    )
RETURN
    CONCATENATEX(FILTER(__Table,NOT(ISBLANK([Replace]))),[Replace],", ")

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
sanalytics
Solution Supplier
Solution Supplier

@sam123,

Since Greg sir has already posted DAX solution..So,you can get gelp from that..Although below is the Power query solution

https://www.dropbox.com/s/oxag35s267yn01p/Replace%20comma%20separated%20IDs%20in%20a%20column%20from...

 

downlaod the PBI file..
Regards,

sanalytics

Greg_Deckler
Super User
Super User

@Anonymous See what you think of this solution. PBIX is attached after sig. Table (4) and Table (4a):

Column = 
VAR __Table =
    ADDCOLUMNS(
        GENERATE(
            { [Card_ID] },
            'Table (4a)'
        ),
        "Replace",IF(FIND([Label_ID],[Card_Label_ID],,0)>0,[Label_Value],BLANK())
    )
RETURN
    CONCATENATEX(FILTER(__Table,NOT(ISBLANK([Replace]))),[Replace],", ")

@ 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!:
The Definitive Guide to Power Query (M)

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

Hey Greg,

 

Thank you so much! This works perfectly. Providing the file was incredibly helpful to understanding the logic (which is gonna take a me little to work through despite yee ole copy and paste working like a charm).

 

Best,

 

Sam

@Anonymous 

So, create a table variable called __Table. Use the current row's value of the column Card_ID, treating it as a table ( {   } ) and then using this generate a table that contains what is essentially a cartesian product between this and Table (4a) so you end up with a table that includes the current row's Card_ID value along with each row from Table (4a). Another way to say this is that you are taking Table (4a) and essentially tacking on the current row's Card_ID value.

 

To this table you then add a column, Replace, where you try to FIND if the current row's Label_ID is in that same row's Card_ID. If so, return the equivalent Label_Value. If not, return BLANK.

 

To RETURN, filter out the blanks in Replace and concatenate the values in the Replace column.

Column = 
VAR __Table =
    ADDCOLUMNS(
        GENERATE(
            { [Card_ID] },
            'Table (4a)'
        ),
        "Replace",IF(FIND([Label_ID],[Card_Label_ID],,0)>0,[Label_Value],BLANK())
    )
RETURN
    CONCATENATEX(FILTER(__Table,NOT(ISBLANK([Replace]))),[Replace],", ")

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.