Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
gavinashton
Regular Visitor

Split based on specifically formatted string

I have a column with a range of different strings in each row, but every one will include either L1, L2 or L3. So I want to create a custom column that just has that L1, L2 or L3 value. The standard split function doesn't quite seem to do the job. I guess this is basic stuff for the folks here 🙂

1 ACCEPTED SOLUTION

Yes, Forgot to mention it's Power Query (my signature indicates that most of my solutions are Power Query).

 

The entire code from the advanced query editor (I used Power Query in Excel, but with a different source it also works in Power BI):

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    PreviousStep = Table.TransformColumnTypes(Source,{{"String", type text}}),

    // Simple solution but not very flexible
    Simple = Table.AddColumn(PreviousStep, "IfThenElse", each if Text.Contains([String],"L1") then "L1" else if Text.Contains([String],"L2") then "L2" else "L3"),

    // More dynamic solution: returns the first element of the list that is found 
    StringsToBeFound={"L1","L2","L3"},
    Dynamic = Table.AddColumn(PreviousStep, "Lookup", (ThisRecord) => List.Select(StringsToBeFound, each Text.PositionOf(ThisRecord[String],_) >= 0){0})
in
    Dynamic
Specializing in Power Query Formula Language (M)

View solution in original post

3 REPLIES 3
MarcelBeug
Community Champion
Community Champion

Sounds like extraction rather than splitting.

 

Anyhow I have 2 solutions in the code part below:

 

    // Simple solution but not very flexible
    Simple = Table.AddColumn(PreviousStep, "IfThenElse", each if Text.Contains([String],"L1") then "L1" else if Text.Contains([String],"L2") then "L2" else "L3"),

    // More dynamic solution: returns the first element of the list that is found 
    StringsToBeFound={"L1","L2","L3"},
    Dynamic = Table.AddColumn(PreviousStep, "Lookup", (ThisRecord) => List.Select(StringsToBeFound, each Text.PositionOf(ThisRecord[String],_) >= 0){0})
Specializing in Power Query Formula Language (M)

Nice. So that would be under a new column in the query editor? Reason for asking is I'm getting 'Token Eof expected'.

Yes, Forgot to mention it's Power Query (my signature indicates that most of my solutions are Power Query).

 

The entire code from the advanced query editor (I used Power Query in Excel, but with a different source it also works in Power BI):

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    PreviousStep = Table.TransformColumnTypes(Source,{{"String", type text}}),

    // Simple solution but not very flexible
    Simple = Table.AddColumn(PreviousStep, "IfThenElse", each if Text.Contains([String],"L1") then "L1" else if Text.Contains([String],"L2") then "L2" else "L3"),

    // More dynamic solution: returns the first element of the list that is found 
    StringsToBeFound={"L1","L2","L3"},
    Dynamic = Table.AddColumn(PreviousStep, "Lookup", (ThisRecord) => List.Select(StringsToBeFound, each Text.PositionOf(ThisRecord[String],_) >= 0){0})
in
    Dynamic
Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.