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
Hamidrezaei
Frequent Visitor

White spaces

Hi Guys, 

 

Is there any way to change colums with space to null? some cells contains only one or multiple spaces inside. 

The function Trim seems to remove only beggining and the end of a cell with spaces, it dosen't remove spaces if a cell only contains space in it.

 

I appreciate your help

Hamid

 

1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @Hamidrezaei 

 

Based on your descirption, I created data to reproduce your scenario. The pbix file is attached in the end.

Test(row 4 and row 6 both have spaces):

d1.PNG

 

You may add steps in Power Query as below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlRwVHBWitWJVjJScFFwA7OMFVwV3MEsBTBpquCh4AnhAwViAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    Custom1 = Table.ReplaceValue(#"Changed Type"," ","",Replacer.ReplaceText,{"Column1"}),
    Custom2 = Table.AddColumn(Custom1,"Custom2",each if Text.Length([Column1])=0 then null else [Column1]),
    #"Removed Columns" = Table.RemoveColumns(Custom2,{"Column1"})
in
    #"Removed Columns"

 

Result:

d2.PNG

 

Best Regards

Allan

 

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

5 REPLIES 5
v-alq-msft
Community Support
Community Support

Hi, @Hamidrezaei 

 

Based on your descirption, I created data to reproduce your scenario. The pbix file is attached in the end.

Test(row 4 and row 6 both have spaces):

d1.PNG

 

You may add steps in Power Query as below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlRwVHBWitWJVjJScFFwA7OMFVwV3MEsBTBpquCh4AnhAwViAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    Custom1 = Table.ReplaceValue(#"Changed Type"," ","",Replacer.ReplaceText,{"Column1"}),
    Custom2 = Table.AddColumn(Custom1,"Custom2",each if Text.Length([Column1])=0 then null else [Column1]),
    #"Removed Columns" = Table.RemoveColumns(Custom2,{"Column1"})
in
    #"Removed Columns"

 

Result:

d2.PNG

 

Best Regards

Allan

 

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

 

 

 

Thankn you this is very helpfull. 

Is there anyway to touch only cells with spaces only (Row 4 and 6) ?

 

Thank you 

Hamid

Hi, @Hamidrezaei 

 

You may try the following codes in 'Advanced Editor'. The pbix file is attached in the end.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlRwVHBWitWJVjJScFFwA7OMFVwV3MEsBTBpquCh4AnhAwViAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    Custom1 = Table.AddColumn(#"Changed Type","New Column",each if Text.Length(Text.Replace([Column1]," ",""))=0 then null else [Column1]),
    #"Removed Columns" = Table.RemoveColumns(Custom1,{"Column1"})
in
    #"Removed Columns"

 

Result:

f1.PNG

 

Best Regards

Allan

 

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

Thank you so much @v-alq-msft , its great!

mahoneypat
Employee
Employee

In the query editor, choose Replace Values and then in the popup, type nothing (i.e., type no characters) into the first box and null into the second.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.

Top Solution Authors
Top Kudoed Authors