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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Digger
Post Patron
Post Patron

Replace character in list of lists, between first comma and specific string

I have bad csv files, and need first replace commas in peoples names

so i do

= Table.AddColumn(#"Added Custom1", "Custom", each Lines.FromBinary([Content]) )

And geting Column of list values

Digger_0-1666039889533.png

Now in each List i need to replace or delete comma between each line first comma and ",SB"

To remove commas from customers names to get good csv

Digger_1-1666040034602.png

 

So how to do that in PBI?

 

Closest expected result is like

List.ReplaceMatchingItems(

{Source List}, { {OldValue1, NewValue1}, {OldValue2, New Value2} }

)

But it need manual intervention each time if new weird name appears again.

 

 

 

 

1 ACCEPTED SOLUTION
Syndicate_Admin
Administrator
Administrator

Solved by

Table.PromoteHeaders(
	Table.FromList(
		List.Transform(
			Lines.FromBinary([Content])
				,each 
					try
						Text.Combine({
							Text.BeforeDelimiter(_,","), ",", 							// text till first comma
							Text.Replace(Text.BetweenDelimiters(_,",",",SB"),",","."),	// replacing comma to dot only in text between first comma and string ",SB"
							Text.Middle(_,Text.PositionOf(_,",SB"),Text.Length(_))		// Text from ,SB till end
                        })
					otherwise _															// ignore headers error
       )
	, Splitter.SplitTextByDelimiter(","), null, null, ExtraValues.Error					// split csv by columns
	)
, [PromoteAllScalars=true]																// promote headers
)

View solution in original post

4 REPLIES 4
Syndicate_Admin
Administrator
Administrator

Solved by

Table.PromoteHeaders(
	Table.FromList(
		List.Transform(
			Lines.FromBinary([Content])
				,each 
					try
						Text.Combine({
							Text.BeforeDelimiter(_,","), ",", 							// text till first comma
							Text.Replace(Text.BetweenDelimiters(_,",",",SB"),",","."),	// replacing comma to dot only in text between first comma and string ",SB"
							Text.Middle(_,Text.PositionOf(_,",SB"),Text.Length(_))		// Text from ,SB till end
                        })
					otherwise _															// ignore headers error
       )
	, Splitter.SplitTextByDelimiter(","), null, null, ExtraValues.Error					// split csv by columns
	)
, [PromoteAllScalars=true]																// promote headers
)
Shaurya
Memorable Member
Memorable Member

Hi @Digger,

 

I suggest that you replace the commas with a space and then TRIM the text to remove any leading or trailing spaces.

 

#"Replaced Value" = Table.ReplaceValue(#"Changed Type",","," ",Replacer.ReplaceText,{"Name"})

 

Did I answer your question? Mark this post as a solution if I did!
Consider taking a look at my blog: Forecast Period - Previous Forecasts

@Shaurya  it will not works, as first i have lists not table, second i need replace commas between strings ,not leading or trailing spaces

Hi @Digger ,

Please review the following links and check if they can help you solve the problem. Or could you please share that csv file with us and your expected result with some special examples? Later we can give you a suitable solution base on your provided info. Thank you.

Power Query - Replace multiple substrings in one list column
//Do the actual replacements using the TransformColumns method
    #"Replace Multiple" = Table.TransformColumns(#"Previous Step", 
    {"Column1", (s)=> 
        Text.Combine(
            List.ReplaceMatchingItems(
                List.Transform(Text.Split(s,","), Text.Trim),
                #"Replacement List"),
            ",")
    })
Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.