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

Splitting column into multiple columns based on text value in second column

Hi, 

 

I am trying to build a Power BI dashboard based on SQL server data. My issue is that I have a table in which I need to split a single column (Component Type) into three separate columns (Bottle type, Cap type, Packaging type) based on text string values in another column (Item No) in the same table. This is to allow me to see e.g. which bottle and cap types are related (through the Item No) to a given packaging type and vice versa when I click on them in a table in report view. I have tried to illustrate what I seek to achieve in the figure snippet below. 

 

Basically what I want to achieve is to separate the component types into separate columns based on the two first letters in their Item No. This way I can use the production BOM No (not pictured - is in separate table) to see which Bottle type, cap type and packaging types are related depending on which drill down filter I apply. 

 

2019-07-18 11_25_46-Book1 - Excel.png

 

 

 

 

 

 

 

 

However, when trying to make calculated columns to achieve the above I get a circular reference error. I have tried multiple solutions such as trying to make calculated tables and even making duplicate SQL queries and filtering the tables in query editor, yet it does not seem to fix the issue. 

 

Any suggestions on how to achieve the above without getting a circular reference error? 

 

Thanks in advance, 

 

/Rasmus

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hello 

 

You can use Conditional column to achive this.

Please try with this code.

 

let
    Source = Excel.Workbook(File.Contents("C:\CommunityHelp.xlsx"), null, true),
    Item_Sheet = Source{[Item="Item",Kind="Sheet"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Item_Sheet,{{"Column1", type text}, {"Column2", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Item No", type text}, {"Component Type", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type1", "Bottle Type", each if Text.StartsWith([Item No], "EF") then [Component Type] else null),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Cap Type", each if Text.StartsWith([Item No], "EK") then [Component Type] else null),
    #"Added Conditional Column2" = Table.AddColumn(#"Added Conditional Column1", "Packaging Type", each if Text.StartsWith([Item No], "EY") then [Component Type] else null)
in
    #"Added Conditional Column2"

1.PNG

Note: I have used excel file as input. So alter the first few lines to match you SQL database.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hello 

 

You can use Conditional column to achive this.

Please try with this code.

 

let
    Source = Excel.Workbook(File.Contents("C:\CommunityHelp.xlsx"), null, true),
    Item_Sheet = Source{[Item="Item",Kind="Sheet"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Item_Sheet,{{"Column1", type text}, {"Column2", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Item No", type text}, {"Component Type", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type1", "Bottle Type", each if Text.StartsWith([Item No], "EF") then [Component Type] else null),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Cap Type", each if Text.StartsWith([Item No], "EK") then [Component Type] else null),
    #"Added Conditional Column2" = Table.AddColumn(#"Added Conditional Column1", "Packaging Type", each if Text.StartsWith([Item No], "EY") then [Component Type] else null)
in
    #"Added Conditional Column2"

1.PNG

Note: I have used excel file as input. So alter the first few lines to match you SQL database.

Thank you for the quick reply and delightfully simple 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.