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
NatashaSchuster
Helper II
Helper II

[M Question]. Split one column into two. Various length space delimiter

Hello, 

 

What is the good M code to break this text column into two columns? 

I was experimenting with Splitter functions but no luck....

 

Thanks

 

 

image.png

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

@NatashaSchuster

 

You've got a couple of options. If you can guarantee a certain amount of spaces both leading and between your numbers, you can split by number of characters. For example, if there are 8 spaces, a number, and then 8 more spaces, you could split on position 14, assuming your number is never more than 6 digits long.

 

See screenshot:

2017-10-31_14-38-21.png

 

 

Otherwise, you can first trim the column, then split on the first space (leftmost), then an auto-type-detect will get your your numbers:

 

OriginalOriginal

First TrimFirst Trim

Split on first spaceSplit on first space

Type detection resultType detection result

 

View solution in original post

It all depends on the possible formats of your data.

 

Example in he following code. Note: after splitting the column, a "Changed Tyoe"  step was generated, turning the data into numbers; I removed this step. On second thought, that might also be a possibility to change the numbers back to text. Then the spaces will be gone as well.

 

let
    Source = #table(1,{{"     3          0     "},{"     2          0     "}}),
    #"Split Column by Position" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByPositions({0, 10}, false), {"Column1.1", "Column1.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Position",{{"Column1.1", Text.Trim, type text}, {"Column1.2", Text.Trim, type text}})
in
    #"Trimmed Text"
Specializing in Power Query Formula Language (M)

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

@NatashaSchuster

 

You've got a couple of options. If you can guarantee a certain amount of spaces both leading and between your numbers, you can split by number of characters. For example, if there are 8 spaces, a number, and then 8 more spaces, you could split on position 14, assuming your number is never more than 6 digits long.

 

See screenshot:

2017-10-31_14-38-21.png

 

 

Otherwise, you can first trim the column, then split on the first space (leftmost), then an auto-type-detect will get your your numbers:

 

OriginalOriginal

First TrimFirst Trim

Split on first spaceSplit on first space

Type detection resultType detection result

 

MarcelBeug
Community Champion
Community Champion

So it looks like you have a bunch of leading, middle and trailing spaces.

 

Maybe you can split on position and trim the resulting columns?

Specializing in Power Query Formula Language (M)

@MarcelBeug

 

My idea was to extract first character in the first separate column and then second into a second column. 

Or delete leading and trailing spaces, split the by the 1st space and then clean all the spaces 

 

Does this sound like a good strategy ? 

 

Thanks

It all depends on the possible formats of your data.

 

Example in he following code. Note: after splitting the column, a "Changed Tyoe"  step was generated, turning the data into numbers; I removed this step. On second thought, that might also be a possibility to change the numbers back to text. Then the spaces will be gone as well.

 

let
    Source = #table(1,{{"     3          0     "},{"     2          0     "}}),
    #"Split Column by Position" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByPositions({0, 10}, false), {"Column1.1", "Column1.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Position",{{"Column1.1", Text.Trim, type text}, {"Column1.2", Text.Trim, type text}})
in
    #"Trimmed Text"
Specializing in Power Query Formula Language (M)

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.