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
gboldt
New Member

Help splitting a column based on the number of leading spaces

I am attempting to clean some data that has all the parent and child records in column A and the hierarchy is defined by the number of spaces. Example data:

 

gboldt_1-1652459874637.png

 

 

I want to transpose each value in the "zone" column to the appropriate column based on the number of leading spaces (note that the column names and actual data will not have anything in common like they do in the example):

 

  1. If 1 leading space, leave where it is (zone)
  2. If 4 leading spaces, transpose 1 column over (unit)
  3. If 7 leading spaces, transpose 2 columns over (category)
  4. If 10 leading spaces, transpose 3 columns over (status)

 

I also want to copy parent values down so that if it has a child record it is populated. Here is what the result would look like based on the example data above: 

 

gboldt_2-1652459940475.png

 

 

any help would be greatly appreciated, thanks in advance.

1 ACCEPTED SOLUTION
v-yalanwu-msft
Community Support
Community Support

Hi, @gboldt ;

You could complete it in power query.

1.add conditional formula.

= Table.AddColumn(Source, "zone1", each if not Text.StartsWith([zone], " ") then [zone] else null)
= Table.AddColumn(#"Added Conditional Column", "unit", each if Text.StartsWith([zone]," ") and not Text.StartsWith([zone],"  ") then [zone] else null)
= Table.AddColumn(#"Added Custom", "category", each if Text.StartsWith([zone],"  ") and not Text.StartsWith([zone],"   ") then [zone] else null)
= Table.AddColumn(#"Added Custom1", "Status", each if Text.StartsWith([zone],"   ") and not Text.StartsWith([zone],"     ") then [zone] else null)

 

vyalanwumsft_1-1652667016040.png

2.fill down every column.

= Table.FillDown(#"Added Custom2",{"zone1"})
= Table.Group(#"Filled Down", {"zone1"}, {{"a", each Table.FillDown(_,{"unit"}), type table [zone=nullable text, zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}})
= Table.Group(#"Expanded a", {"zone1", "unit"}, {{"category", each Table.FillDown(_,{"category"}), type table [zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}})

The final show as follow:

vyalanwumsft_2-1652667114853.png

vyalanwumsft_3-1652667229749.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WqsrPS1VIVIrViVZSKM3LLDGEMBWSE0tS0/OLKhVgAgoKxSWJJaXFCKVG2JWCTExCqDJGVwXUFgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [zone = _t]),
    #"Added Conditional Column" = Table.AddColumn(Source, "zone1", each if not Text.StartsWith([zone], " ") then [zone] else null),
    #"Added Custom" = Table.AddColumn(#"Added Conditional Column", "unit", each if Text.StartsWith([zone]," ") and not Text.StartsWith([zone],"  ") then [zone] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "category", each if Text.StartsWith([zone],"  ") and not Text.StartsWith([zone],"   ") then [zone] else null),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Status", each if Text.StartsWith([zone],"   ") and not Text.StartsWith([zone],"     ") then [zone] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom2",{"zone1"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"zone1"}, {{"a", each Table.FillDown(_,{"unit"}), type table [zone=nullable text, zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}}),
    #"Expanded a" = Table.ExpandTableColumn(#"Grouped Rows", "a", {"unit", "category", "Status"}, {"unit", "category", "Status"}),
    #"Grouped Rows1" = Table.Group(#"Expanded a", {"zone1", "unit"}, {{"category", each Table.FillDown(_,{"category"}), type table [zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}}),
    #"Expanded category" = Table.ExpandTableColumn(#"Grouped Rows1", "category", {"category", "Status"}, {"category", "Status"})
in
    #"Expanded category"


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yalanwu-msft
Community Support
Community Support

Hi, @gboldt ;

You could complete it in power query.

1.add conditional formula.

= Table.AddColumn(Source, "zone1", each if not Text.StartsWith([zone], " ") then [zone] else null)
= Table.AddColumn(#"Added Conditional Column", "unit", each if Text.StartsWith([zone]," ") and not Text.StartsWith([zone],"  ") then [zone] else null)
= Table.AddColumn(#"Added Custom", "category", each if Text.StartsWith([zone],"  ") and not Text.StartsWith([zone],"   ") then [zone] else null)
= Table.AddColumn(#"Added Custom1", "Status", each if Text.StartsWith([zone],"   ") and not Text.StartsWith([zone],"     ") then [zone] else null)

 

vyalanwumsft_1-1652667016040.png

2.fill down every column.

= Table.FillDown(#"Added Custom2",{"zone1"})
= Table.Group(#"Filled Down", {"zone1"}, {{"a", each Table.FillDown(_,{"unit"}), type table [zone=nullable text, zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}})
= Table.Group(#"Expanded a", {"zone1", "unit"}, {{"category", each Table.FillDown(_,{"category"}), type table [zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}})

The final show as follow:

vyalanwumsft_2-1652667114853.png

vyalanwumsft_3-1652667229749.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WqsrPS1VIVIrViVZSKM3LLDGEMBWSE0tS0/OLKhVgAgoKxSWJJaXFCKVG2JWCTExCqDJGVwXUFgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [zone = _t]),
    #"Added Conditional Column" = Table.AddColumn(Source, "zone1", each if not Text.StartsWith([zone], " ") then [zone] else null),
    #"Added Custom" = Table.AddColumn(#"Added Conditional Column", "unit", each if Text.StartsWith([zone]," ") and not Text.StartsWith([zone],"  ") then [zone] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "category", each if Text.StartsWith([zone],"  ") and not Text.StartsWith([zone],"   ") then [zone] else null),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Status", each if Text.StartsWith([zone],"   ") and not Text.StartsWith([zone],"     ") then [zone] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom2",{"zone1"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"zone1"}, {{"a", each Table.FillDown(_,{"unit"}), type table [zone=nullable text, zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}}),
    #"Expanded a" = Table.ExpandTableColumn(#"Grouped Rows", "a", {"unit", "category", "Status"}, {"unit", "category", "Status"}),
    #"Grouped Rows1" = Table.Group(#"Expanded a", {"zone1", "unit"}, {{"category", each Table.FillDown(_,{"category"}), type table [zone1=text, unit=nullable text, category=nullable text, Status=nullable text]}}),
    #"Expanded category" = Table.ExpandTableColumn(#"Grouped Rows1", "category", {"category", "Status"}, {"category", "Status"})
in
    #"Expanded category"


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.