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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Leading and Trailing Spaces

Hi,

I feel like Power BI is automatically trimming my data. Is there a way to not have that happen?

I am importing txt files with | as the delimeter. Lets say "21B", "21B " and " 21B" are the data values for one of the fields, Power BI will automatically see them as "21B" without the leading and trailing spaces which is not what I want. I need to detect the spaces in my work process. 

I don't see any options to have this undone (transform -> format, only has trim which its already doing for unknown reason), and I haven't applied any steps to trim it. I also don't see any helpful option in the data source setting. 

Please assist.

2.PNG

1.PNG

3.PNG

8 REPLIES 8
HotChilli
Super User
Super User

My experience is as-per this article

https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-data-types#leading-and-trailing-spac... 

 

powerbi engine removes trailing spaces but not leading spaces. This is not Power Query (which leaves them in).

 

Anonymous
Not applicable

This is what i have in the 'transform data' section, as you can see i did not trim anything. 

ZMT19_0-1723215157751.png

Here is my txt file:

ZMT19_1-1723215307579.png

 

As you can see there are about 16 cases where there are spaces in them, thus the length is >3.

but with this code:  "RouteIDError = IF(NOT(LEN('Duplicate'[Route ID]) = 3), "Route ID """)", I only get 5 RouteIDError, it does not count the trailing spaces. 

ZMT19_2-1723215407318.png

 

How do I have powerbi include the leading and trailing spaces? Please assist.

 

 

That is not your M code. Open the advanced editor, found in the view menu and copy paste.

The problem may be in the change type step. If it changes to numeric, it will definitely remove spaces....

Anonymous
Not applicable

m code.PNG

 This is what i have

PwerQueryKees
Super User
Super User

Can you share your M code? All steps from loading the file to where your query fails?

Anonymous
Not applicable

This is what i have in the 'transform data' section, as you can see i did not trim anything. 

ZMT19_0-1723215157751.png

Here is my txt file:

ZMT19_1-1723215307579.png

 

As you can see there are about 16 cases where there are spaces in them, thus the length is >3.

but with this code:  "RouteIDError = IF(NOT(LEN('Duplicate'[Route ID]) = 3), "Route ID """)", I only get 5 RouteIDError, it does not count the trailing spaces. 

ZMT19_2-1723215407318.png

 

How do I have powerbi include the leading and trailing spaces? Please assist.

 

 

HotChilli
Super User
Super User

"I feel like Power BI is automatically trimming my data" - Where are you seeing that happening? Can you show us please?

Anonymous
Not applicable

So I have this "RouteIDError = IF(NOT(LEN('Duplicate'[Route ID]) = 3), "Route ID ", "")", which checks that Route ID (text format), and if it is not a len of 3, it would return "Route ID ", otherwise blank. But it does not count leading and trailing spaces. Such that " 21B" and "21B ", should be counted as a len of 4 including the space but it does not. But if there is a space in between the text, like "21 B" or "2 1B", it would count it as len of 4. So it does automatically trims it and ignores the leading and trailing spaces. So I'd assume it's an issue with the data source settings, but I can't find any option that deals with this. 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors