Forum Discussion
Add dot after each character in string
- 3 years ago
This will not change data that has dots. If this isn't what you need, provide a complete sample of data and what should be modified. See directions below.
let Source = Excel.Workbook(File.Contents("C:\Users\Dante\Desktop\Monthly Country Reports\XXX\test.xlsx"), null, true), __activityqty_Sheet = Source{[Item="__activityqty",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(__activityqty_Sheet, [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"rel_region", type text}, {"rel_subregion", type text}, {"client", type text}, {"period", Int64.Type}, {"country", type text}, {"xcountry", type text}, {"activity", type text}, {"xactivity", type text},}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"period", "Date"}}), #"Duplicated Column" = Table.DuplicateColumn(#"Renamed Columns", "Date", "Date - Copy"), #"Reordered Columns" = Table.ReorderColumns(#"Duplicated Column",{"Date", "Date - Copy", "country", "activity", "xactivity",}), #"Removed Columns1" = Table.RemoveColumns(#"Reordered Columns",{"Date - Copy"}), #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns1",{{"Date", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each Date.FromText( Text.Range([Date], 0,4) & "-" & Text.Range([Date], 4,2) )), #"Changed Type2" = Table.TransformColumnTypes(#"Reordered Columns1",{{"Custom", type date}}), #"Removed Columns2" = Table.RemoveColumns(#"Changed Type2",{"Date"}), #"Renamed Columns1" = Table.RenameColumns(#"Removed Columns2",{{"country", "CountryCode"}, {"Custom", "Date"},}), #"Changed Type3" = Table.TransformColumnTypes(#"Renamed Columns1",{{"CountryCode", type text},}), #"Inserted Text Between Delimiters" = Table.AddColumn(#"Changed Type3", "Text Between Delimiters", each Text.BetweenDelimiters([xsip], " ", " ", 1, 3), type text), #"Reordered Columns2" = Table.ReorderColumns(#"Inserted Text Between Delimiters",{"Date", "CountryCode", "activity", "xactivity",}), #"Removed Columns3" = Table.RemoveColumns(#"Reordered Columns2",{"Text Between Delimiters"}), #"Inserted Text After Delimiter" = Table.AddColumn(#"Removed Columns3", "Text After Delimiter", each Text.AfterDelimiter([xsip], " ", 1), type text), #"Reordered Columns3" = Table.ReorderColumns(#"Inserted Text After Delimiter",{"Date", "CountryCode", "activity", "xactivity",}), #"Removed Columns4" = Table.RemoveColumns(#"Reordered Columns3",{"Text After Delimiter"}), #"Reordered Columns4" = Table.ReorderColumns(#"Renamed Columns5",{"Date", "CountryCode", "activity", "xactivity",}), #"Renamed Columns6" = Table.RenameColumns(#"Reordered Columns4",{{"activity", "Activity_Code"}}), #"Split Column by Positions3" = Table.SplitColumn(#"Renamed Columns6", "xactivity", Splitter.SplitTextByPositions({0, 7}), {"xactivity.1", "xactivity.2"}), #"Changed Type7" = Table.TransformColumnTypes(#"Split Column by Positions3",{{"xactivity.1", type text}, {"xactivity.2", type text}}), #"Replaced Value2" = Table.ReplaceValue(#"Changed Type7","018","(2018",Replacer.ReplaceText,{"xactivity.2"}), #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2",")D",") D",Replacer.ReplaceText,{"xactivity.2"}), #"Replaced Value4" = Table.ReplaceValue(#"Replaced Value3",")T",") T",Replacer.ReplaceText,{"xactivity.2"}), #"Replaced Value5" = Table.ReplaceValue(#"Replaced Value4","019","(2019",Replacer.ReplaceText,{"xactivity.2"}), #"Filtered Rows1" = Table.SelectRows(#"Replaced Value5", each true), #"Replaced Value6" = Table.ReplaceValue(#"Filtered Rows1"," #(tab)Leadership","Leadership",Replacer.ReplaceText,{"xactivity.2"}), #"Replaced Value7" = Table.ReplaceValue(#"Replaced Value6"," Crow","Crow",Replacer.ReplaceText,{"xactivity.2"}), #"Replaced Value13" = Table.ReplaceValue(#"Replaced Value12"," (2","",Replacer.ReplaceText,{"xactivity.1"}), #"Filtered Rows2" = Table.SelectRows(#"Replaced Value13", each true), #"Trimmed Text" = Table.TransformColumns(#"Filtered Rows2",{{"xactivity.2", Text.Trim, type text}}), #"Renamed Columns8" = Table.RenameColumns(#"Changed Type8",{{"xactivity.1", "ActivityID"}}), #"Filtered Rows4" = Table.SelectRows(#"Trimmed Text1", each true), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows4",{"rel_region", "rel_subregion", "client", "xcountry"}), #"Added DotValues" = Table.AddColumn( #"Removed Columns", "With Dots", each let varLength = Text.Length([Data]), varData = [Data] in if Text.Contains(varData, ".") then varData else Text.BeforeDelimiter( Text.Combine( List.Generate( ()=> [x=0], each [x] < varLength, each [x = [x]+1], each Text.Middle(varData, [x], 1) & "." ) ), ".", varLength-1 ), type text ) in #"Added DotValues"How to get good help fast. Help us help you.
How To Ask A Technical Question If you Really Want An Answer
How to Get Your Question Answered Quickly - Give us a good and concise explanation
How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.
Sorry if i wasn't being clear enough.
I have a table called ABC with a column called ActivityID, and in it there's data like below
1111
1231
4321
1.2.3.4
5.3.2.1
5.3.9.3
I would like to convert those numbers without dots, e.g. 1111 to show as 1.1.1.1
I followed your instructions above and created a blank query and copied the code in it.
Then i went back to the ABC table and created a custom column called 'Custom' and changed the DATA table to ActivityID as shown below:-
Table.AddColumn(
Source,
"With Dots",
each
let
varLength = Text.Length([ActivityID]),
varData = [ActivityID]
in
Text.BeforeDelimiter(
Text.Combine(
List.Generate(
()=> [x=0],
each [x] < varLength,
each [x = [x]+1],
each Text.Middle(varData, [x], 1) & "."
)
),
".", varLength-1
),
type text
)
and the result is as below:-
Did i do it wrongly ?
If what you show is what you have, a simple one line algorithm suggests itself
Before
- Transform the table column
- create a List of the characters in the cell
- remove any existing dots
- Recombine the text using the dot as the delimeter
#"Dotted" = Table.TransformColumns(#"Previous Step",
{"ActivityID", each Text.Combine(List.RemoveMatchingItems(Text.ToList(_),{"."}),".") })To Reproduce, place the following into a blank query:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMgQCpVgdIMPI2ATMMDE2goroGekZ60EETYEsIz1DONtSz1gpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ActivityID = _t]),
#"Previous Step" = Table.TransformColumnTypes(Source,{{"ActivityID", type text}}),
#"Dotted" = Table.TransformColumns(#"Previous Step",
{"ActivityID", each Text.Combine(List.RemoveMatchingItems(Text.ToList(_),{"."}),".") })
in
#"Dotted"After