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
padfootkk
Employee
Employee

How to split a column and only keep specific content

Hi everyone,

 

I have a column that contain several values in it, the values are seperated by a simicolon and a space. Now I just want to create another column that only keep the numbers that after the word "item". For those rows that have more than one item numbers, keep the first one; for those rows that don't have a item number, then fill the new column as "not applicable".

 

Example:

Table

IDColumn1Column2
123text; item 123456789123456789
234word 2.3; item 398475921398475921
345item 123345204; item 102948329123345204
456text; word 2.3Not Applicable
567textNot Applicable
678item 104924203104924203

 

 

I know the table is kind of messy, does anyone have ideas about this?

 

Thanks!

2 ACCEPTED SOLUTIONS
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Column1", type text}}),
    #"Inserted Text After Delimiter" = Table.AddColumn(#"Changed Type", "Text After Delimiter", each Text.AfterDelimiter([Column1], "item "), type text),
    #"Replaced Value" = Table.ReplaceValue(#"Inserted Text After Delimiter","","Not Applicable",Replacer.ReplaceValue,{"Text After Delimiter"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Value", "Text After Delimiter", Splitter.SplitTextByEachDelimiter({";"}, QuoteStyle.Csv, false), {"Text After Delimiter.1", "Text After Delimiter.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Text After Delimiter.2"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

v-frfei-msft
Community Support
Community Support

Hi @padfootkk ,

 

Please check the M code as below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bU5LDoIwEL1K0zUhZTpIJ668gBdoukDtggQDMU30+D6wpRt37zfznve6I6sbneInndWU4lNB4P40OIFccWi8BoH2Xl4PRa3NcSuOh16og1XxFscptPITjAyXDkPCzlLu+Hn7EeqOOaUJwnVJ6rKu83Qfb3Pck9iVk/99zD7qDQsxme1TxSF8AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Column1", type text}, {"Column2", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByDelimiter("item", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3"}),
    #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Custom", each Text.Select([Column1.2],{"0".."9"})),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", Int64.Type}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Custom.1", each if [Custom] = null then "Not Applicable" else [Custom]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Column1.2", "Column1.3", "Column1.1"})
in
    #"Removed Columns"

Capture.PNG

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

4 REPLIES 4
v-frfei-msft
Community Support
Community Support

Hi @padfootkk ,

 

Please check the M code as below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bU5LDoIwEL1K0zUhZTpIJ668gBdoukDtggQDMU30+D6wpRt37zfznve6I6sbneInndWU4lNB4P40OIFccWi8BoH2Xl4PRa3NcSuOh16og1XxFscptPITjAyXDkPCzlLu+Hn7EeqOOaUJwnVJ6rKu83Qfb3Pck9iVk/99zD7qDQsxme1TxSF8AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Column1", type text}, {"Column2", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByDelimiter("item", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3"}),
    #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Custom", each Text.Select([Column1.2],{"0".."9"})),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", Int64.Type}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Custom.1", each if [Custom] = null then "Not Applicable" else [Custom]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Column1.2", "Column1.3", "Column1.1"})
in
    #"Removed Columns"

Capture.PNG

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Column1", type text}}),
    #"Inserted Text After Delimiter" = Table.AddColumn(#"Changed Type", "Text After Delimiter", each Text.AfterDelimiter([Column1], "item "), type text),
    #"Replaced Value" = Table.ReplaceValue(#"Inserted Text After Delimiter","","Not Applicable",Replacer.ReplaceValue,{"Text After Delimiter"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Value", "Text After Delimiter", Splitter.SplitTextByEachDelimiter({";"}, QuoteStyle.Csv, false), {"Text After Delimiter.1", "Text After Delimiter.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Text After Delimiter.2"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
HotChilli
Super User
Super User

In Power Query, you could split the column on "item" by right-clicking on the column, choose "Split column"-> by delimiter, Choose Custom and enter the word item.

You'll get something similar to this in the advanced editor:

= Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByEachDelimiter({"item"}, QuoteStyle.Csv, true), {"Column1.1", "Column1.2"})

and you can change the null values in your data by using 'Replace Values' to put 'Not applicable'

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.