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
Anonymous
Not applicable

Split column by second space

In M Query, my dataset contains a column holding file names. 

 

The file name can have any number of words in it, but the last words are always the month and year. The month however can be truncated, or full e.g. Jan or January

 

So, I want to split the column by delimiter, from the right, but I want to ignore the first space. Which in theory, means I should have the month and year irrespective of the number of characters, in a new column and rest of the file name in the other. 

 

I can't find a way to do this. 

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Anonymous ,

Try this:

1. Duplicate column.

dup.PNG

2. Extracted Text Before Delimiter.

dup1.jpg

dup11.PNG

3. Extracted Text After Delimiter.

dup2.jpgdup22.PNG

Then, change column names.

dup3.PNG

Attach the complete code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsvMSTVUMDIwtFTwSsxTitWBCBlBhNxSk+BCxhAh38Si5Ay4oIlCcXExRMIlNTk1Nym1SCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Column1", "Column1 - Copy"),
    #"Duplicated Column1" = Table.DuplicateColumn(#"Duplicated Column", "Column1 - Copy", "Column1 - Copy - Copy"),
    #"Extracted Text Before Delimiter" = Table.TransformColumns(#"Duplicated Column1", {{"Column1 - Copy", each Text.BeforeDelimiter(_, " ", {1, RelativePosition.FromEnd}), type text}}),
    #"Extracted Text After Delimiter" = Table.TransformColumns(#"Extracted Text Before Delimiter", {{"Column1 - Copy - Copy", each Text.AfterDelimiter(_, " ", {1, RelativePosition.FromEnd}), type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Extracted Text After Delimiter",{{"Column1 - Copy", "File"}, {"Column1 - Copy - Copy", "Year Month"}})
in
    #"Renamed Columns"

 

Best Regards,

Icey

 

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

3 REPLIES 3
Icey
Community Support
Community Support

Hi @Anonymous ,

Try this:

1. Duplicate column.

dup.PNG

2. Extracted Text Before Delimiter.

dup1.jpg

dup11.PNG

3. Extracted Text After Delimiter.

dup2.jpgdup22.PNG

Then, change column names.

dup3.PNG

Attach the complete code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsvMSTVUMDIwtFTwSsxTitWBCBlBhNxSk+BCxhAh38Si5Ay4oIlCcXExRMIlNTk1Nym1SCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Column1", "Column1 - Copy"),
    #"Duplicated Column1" = Table.DuplicateColumn(#"Duplicated Column", "Column1 - Copy", "Column1 - Copy - Copy"),
    #"Extracted Text Before Delimiter" = Table.TransformColumns(#"Duplicated Column1", {{"Column1 - Copy", each Text.BeforeDelimiter(_, " ", {1, RelativePosition.FromEnd}), type text}}),
    #"Extracted Text After Delimiter" = Table.TransformColumns(#"Extracted Text Before Delimiter", {{"Column1 - Copy - Copy", each Text.AfterDelimiter(_, " ", {1, RelativePosition.FromEnd}), type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Extracted Text After Delimiter",{{"Column1 - Copy", "File"}, {"Column1 - Copy - Copy", "Year Month"}})
in
    #"Renamed Columns"

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Icey
Community Support
Community Support

Hi @Anonymous ,

Try this:

1. Add Column → Column From Examples.

custom.PNG

2. Enter sample value.

custom2.PNG

3. Modify the value that does not match.

custom3.PNG

4. OK.

custom4.PNG

custom5.PNG

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thanks Icy,

 

But I was really hoping not to have to add a column but to split the column because I also need the text before the date as well - as that is the Vendor name. 

Also, I tried your method but poor Power BI couldn't figure out that the last part of the text string was a date.

 

I am able to extract the date using 'Add Column' > Split by delimeter which successfully extracts just the date by using the below code, but I am still left with the original column [Claims Vendor] where I need to do all sorts of crap to get the entire name, because again, it's either a single word, or two words. 

 

If I could perform the below with a transform.column instead of a add.column it would be perfect but it doesn't allow it!!

 

= Table.AddColumn(PreviousStep, "Invoice Period", each Text.AfterDelimiter([Claims Vendor], " ", {1, RelativePosition.FromEnd}), type date)

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.