Split text by three characters before delimiter into rows

brawnystaff

Board Regular
Joined
Aug 9, 2012
Messages
109
Office Version
  1. 365
In Power Query, I am looking to split text in one cell to rows by a known delimiter (in this case, a hyphen -). However, I am looking to split three characters before the hyphen. Any ideas? Code I have so far is a follows:

Power Query:
let
    Source = Csv.Document(File.Contents("C:\Users\Smith\Downloads\PQtext.txt"),1,"0,10000",ExtraValues.Ignore,1252),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Column1", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column1")
in
    #"Split Column by Delimiter"


Thanks.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Please show some sample data 8-10 records using XL2BB. No pictures as we can not manipulate data in a picture.
 
Upvote 0
Was able to figure it out using Claude.ai. Solution is below.

Power Query:
let
    Source = Csv.Document(File.Contents("C:\Users\Smith\Downloads\PQtext.txt"),1,"0,10000",ExtraValues.Ignore,1252),
    
    // Define a custom splitting function
    SplitBeforeHyphen = (text as text) =>
        let
            SplitPositions = List.Transform(
                Text.PositionOf(text, "-", Occurrence.All),
                each _ - 3
            ),
            SplitText = List.Generate(
                () => [i = 0, part = Text.Start(text, List.First(SplitPositions))],
                each [i] < List.Count(SplitPositions),
                each [
                    i = [i] + 1, 
                    part = if [i] = List.Count(SplitPositions) then 
                               Text.Range(text, SplitPositions{[i]-1}, Text.Length(text) - SplitPositions{[i]-1})
                           else 
                               Text.Range(text, SplitPositions{[i]-1}, SplitPositions{[i]} - SplitPositions{[i]-1})
                ],
                each [part]
            )
        in
            SplitText,
    
    // Apply the custom splitting function
    SplitColumns = Table.TransformColumns(Source, {
        {"Column1", SplitBeforeHyphen, type list}
    }),
    
    // Expand the resulting list column
    ExpandedColumns = Table.ExpandListColumn(SplitColumns, "Column1")
in
    ExpandedColumns
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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