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
arbelyoav
Frequent Visitor

DAX for splitting value in a single column to rows in a new table

Hi,

I have a calculated table (table 1) that has a single column which contain values with ";" delimiter. 
I would like to create a new table (table 2), based on table 1, that will split the delimited values into rows per each delimited value.

-Final result is a table which contain the same columns as table 1 of course with more rows.
-Need to be in DAX (not Power Query)


Example:
Table 1:

Part numberPCB PNPart Number description
SFG000123PCB000555;PCB000987;PCB000123Disk

 
Table 2:

Part NumberPCB PNPart Number description

SFG000123

PCB000555Disk
SFG000123PCB000987Disk
SFG000123PCB000123Disk
1 ACCEPTED SOLUTION
kameronyork
Resolver I
Resolver I

I hope this suggestion is helpful for you 🙂

I first recreated the data you provided.  In the example you gave there were three PCB PNs in the column.  I split the column (using text functions) into three columns.
1.png

Here is the code I used for each column:

PCB PN 1 =   LEFT( [PCB PN], FIND(";", [PCB PN]) - 1)
PCB PN 2 = 

IF( LEN([PCB PN]) - LEN( SUBSTITUTE([PCB PN], ";", "")) >= 1,
    MID( [PCB PN], FIND(";", [PCB PN], 1) + 1, LEN([PCB PN 1])),
    BLANK())

// The condition will remove all ;'s and compare to the original.
// This will ensure there is at least one semicolon and thus two values available.
PCB PN 3 = 

IF( LEN([PCB PN]) - LEN( SUBSTITUTE([PCB PN], ";", "")) = 2,
    RIGHT( [PCB PN], LEN([PCB PN 1])),
    BLANK())

// The condition will remove all ;'s and compare to the original.
// This will ensure there are two semicolons and thus three values.

You should create enough columns to satisfy the most values that will be delimited.  If there will never be more than three then this amount will work for you.

2.png
I then created a custom table with the number of values that will be pulled from the delimited column.  This is to allow me to use the CROSSJOIN function to manually create a pivoted version of the table.  Using CROSSJOIN will result in a table that looks like this:
3.png
Then I used lookup functions to switch which column from the original table is being returned, resulting in a table that looks like this:
4.png
Here is the code I used:

PCB PN Lookup = 

SWITCH( [Value],
    1, LOOKUPVALUE( 'Table'[PCB PN 1], 'Table'[Part Number], [Part Number]),
    2, LOOKUPVALUE( 'Table'[PCB PN 2], 'Table'[Part Number], [Part Number]),
    3, LOOKUPVALUE( 'Table'[PCB PN 3], 'Table'[Part Number], [Part Number])
)

// The switch function will change which column will be returned.
Part Number Desc Lookup = 

SWITCH( [Value],
    1, LOOKUPVALUE( 'Table'[Part Number Description], 'Table'[Part Number], [Part Number]),
    2, LOOKUPVALUE( 'Table'[Part Number Description], 'Table'[Part Number], [Part Number]),
    3, LOOKUPVALUE( 'Table'[Part Number Description], 'Table'[Part Number], [Part Number])
)

// This continues to use switch but can be done in one line using lookup if the Description will always be the same.


I tested the solution with more rows to make sure it will work and it seems it will!
5.png6.png

I hope this suggestion helps!  If it does, please mark this as the solution 🙂

View solution in original post

2 REPLIES 2
arbelyoav
Frequent Visitor

Works!!
Thanks

kameronyork
Resolver I
Resolver I

I hope this suggestion is helpful for you 🙂

I first recreated the data you provided.  In the example you gave there were three PCB PNs in the column.  I split the column (using text functions) into three columns.
1.png

Here is the code I used for each column:

PCB PN 1 =   LEFT( [PCB PN], FIND(";", [PCB PN]) - 1)
PCB PN 2 = 

IF( LEN([PCB PN]) - LEN( SUBSTITUTE([PCB PN], ";", "")) >= 1,
    MID( [PCB PN], FIND(";", [PCB PN], 1) + 1, LEN([PCB PN 1])),
    BLANK())

// The condition will remove all ;'s and compare to the original.
// This will ensure there is at least one semicolon and thus two values available.
PCB PN 3 = 

IF( LEN([PCB PN]) - LEN( SUBSTITUTE([PCB PN], ";", "")) = 2,
    RIGHT( [PCB PN], LEN([PCB PN 1])),
    BLANK())

// The condition will remove all ;'s and compare to the original.
// This will ensure there are two semicolons and thus three values.

You should create enough columns to satisfy the most values that will be delimited.  If there will never be more than three then this amount will work for you.

2.png
I then created a custom table with the number of values that will be pulled from the delimited column.  This is to allow me to use the CROSSJOIN function to manually create a pivoted version of the table.  Using CROSSJOIN will result in a table that looks like this:
3.png
Then I used lookup functions to switch which column from the original table is being returned, resulting in a table that looks like this:
4.png
Here is the code I used:

PCB PN Lookup = 

SWITCH( [Value],
    1, LOOKUPVALUE( 'Table'[PCB PN 1], 'Table'[Part Number], [Part Number]),
    2, LOOKUPVALUE( 'Table'[PCB PN 2], 'Table'[Part Number], [Part Number]),
    3, LOOKUPVALUE( 'Table'[PCB PN 3], 'Table'[Part Number], [Part Number])
)

// The switch function will change which column will be returned.
Part Number Desc Lookup = 

SWITCH( [Value],
    1, LOOKUPVALUE( 'Table'[Part Number Description], 'Table'[Part Number], [Part Number]),
    2, LOOKUPVALUE( 'Table'[Part Number Description], 'Table'[Part Number], [Part Number]),
    3, LOOKUPVALUE( 'Table'[Part Number Description], 'Table'[Part Number], [Part Number])
)

// This continues to use switch but can be done in one line using lookup if the Description will always be the same.


I tested the solution with more rows to make sure it will work and it seems it will!
5.png6.png

I hope this suggestion helps!  If it does, please mark this as the solution 🙂

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.