Power Query split?

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,760
Office Version
  1. 365
Platform
  1. Windows
I have a table with a column that has full paths/categories on it:

BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1
OtherBaseCategory/SubCategoryB/SubCategoryB.1

How can I split the paths/categories using Power Query so that I can get all the previous categories as well:

Full CategoryCategories
BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory
BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory/SubCategoryA
BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory/SubCategoryA/SubCategoryA.1
BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1
OtherBaseCategory/SubCategoryB/SubCategoryB.1OtherBaseCategory
OtherBaseCategory/SubCategoryB/SubCategoryB.1OtherBaseCategory/SubCategoryB
OtherBaseCategory/SubCategoryB/SubCategoryB.1OtherBaseCategory/SubCategoryB/SubCategoryB.1
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Not sure how to do that in PQ. I'm interested to see if someone solves that. A VBA UDF can do it fairly easily. In 365 just enter the formula in a cell and press enter. In prior versions you would need to select a one column range long enough to hold the output and then press ctrl-shift-enter.

VBA Code:
Function SplitTable(InputRange As Range, Optional Delimiter As String = "/") As Variant
    For Each c In InputRange
        a = Split(c.Value, Delimiter)
        tot = tot + UBound(a) + 1
    Next
    ReDim NewTable(1 To tot, 1 To 1) As Variant
    j = 0
    For Each c In InputRange
        a = Split(c.Value, Delimiter)
        j = j + 1
        NewTable(j, 1) = a(0)
        For i = 1 To UBound(a)
            j = j + 1
            NewTable(j, 1) = NewTable(j - 1, 1) & Delimiter & a(i)
        Next
    Next
    SplitTable = NewTable
End Function

Book1
ABC
1BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory
2OtherBaseCategory/SubCategoryB/SubCategoryB.1BaseCategory/SubCategoryA
3BaseCategory/SubCategoryA/SubCategoryA.1
4BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1
5OtherBaseCategory
6OtherBaseCategory/SubCategoryB
7OtherBaseCategory/SubCategoryB/SubCategoryB.1
8
Sheet1
Cell Formulas
RangeFormula
B1:B7B1=SplitTable(A1:A2)
Dynamic array formulas.
 
Upvote 0
Thank you for the VBA solution. I guess I could make that work but I'd prefer a Power Query solution.

To me it looks like the solution could be related to the running totals but with a Text.Combine instead of Sum but I just can't make it work.
 
Last edited:
Upvote 0
工作簿1.xlsx
ABC
1Full Category
2BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1
3OtherBaseCategory/SubCategoryB/SubCategoryB.1
4
5
6Full CategoryCategories
7BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory
8BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory/SubCategoryA
9BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory/SubCategoryA/SubCategoryA.1
10BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1BaseCategory/SubCategoryA/SubCategoryA.1/SubCategoryA.1.1
11OtherBaseCategory/SubCategoryB/SubCategoryB.1OtherBaseCategory
12OtherBaseCategory/SubCategoryB/SubCategoryB.1OtherBaseCategory/SubCategoryB
13OtherBaseCategory/SubCategoryB/SubCategoryB.1OtherBaseCategory/SubCategoryB/SubCategoryB.1
14
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl"]}[Content],
    res = Table.ExpandListColumn(
            Table.AddColumn(
                Source,
                "Categories",
                each
                    let
                        b=Text.Split([Full Category],"/")
                    in
                        List.Transform({1..List.Count(b)},(x)=>Text.Combine(List.FirstN(b,x),"/"))),"Categories")
in
    res
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl"]}[Content],
    res = List.Combine(Table.ToList(Source,each List.Transform({0..List.Count(Text.PositionOf(_{0},"/",2))-1},(x)=>Text.BeforeDelimiter(_{0},"/",x))))
in
    res
1617719002366.png
 
Upvote 0
I was a bit more verbose in my approach:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ReplacementFunction = (InputText)=>
     let
       //Use List.Generate() to do the separation
       DoReplacement = List.Generate(()=>[Counter=List.Count(Text.Split(InputText, "/")), MyText=InputText], each [Counter]>0,
            each [Counter=[Counter]-1, MyText=Text.BeforeDelimiter([MyText], "/", {0, RelativePosition.FromEnd})], each [MyText])
     in
      DoReplacement,
    //Add a calculated column to call the function on every row in the table
    //containing the text to change
    Output = Table.AddColumn(Source, "Changed Text", each ReplacementFunction([Path])),
    #"Expanded Changed Text" = Table.ExpandListColumn(Output, "Changed Text")
in
    #"Expanded Changed Text"
 
Upvote 0
Solution
Thank you all!

I could've easily used any of the solutions but the ReplacementFunction by RoryA seemed to be the easiest for me to understand & use with my other queries.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top