Forum Discussion
Split Text into multiple columns
- 5 years ago
Please see the M code below for how to do this by extracting the numbers (from end of input since not all rows have Days value). To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below. It results in columns for Days, Hours, and Minutes, along with a Total Duration column, and a total minutes column.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlNwSawsVjA0V/DILy0qVjAyUPDNzCstSS1WitUhKG2EJm1oQYq0BVTaFCptYoZN2tgUm4vMoHpMsbrXyBhmowmKNNwqQ0sSxU2JEjdC85Eh9uDCIW0IkkbIWpAgSZowefbEAgA=", 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}}), #"Inserted Text Between Delimiters" = Table.AddColumn(#"Changed Type", "Minutes", each Text.BetweenDelimiters([Column1], " ", " ", {1, RelativePosition.FromEnd}, 0), type text), #"Inserted Text Between Delimiters1" = Table.AddColumn(#"Inserted Text Between Delimiters", "Hours", each Text.BetweenDelimiters([Column1], " ", " ", {3, RelativePosition.FromEnd}, 0), type text), #"Inserted Text Before Delimiter" = Table.AddColumn(#"Inserted Text Between Delimiters1", "Days", each Text.BeforeDelimiter([Column1], " ", {4, RelativePosition.FromEnd}), type text), #"Changed Type1" = Table.TransformColumnTypes(#"Inserted Text Before Delimiter",{{"Minutes", Int64.Type}, {"Hours", Int64.Type}, {"Days", Int64.Type}}), #"Replaced Value" = Table.ReplaceValue(#"Changed Type1",null,0,Replacer.ReplaceValue,{"Days"}), #"Added Custom" = Table.AddColumn(#"Replaced Value", "Duration", each #duration([Days]+0,[Hours],[Minutes],0)), #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"Duration", type duration}}), #"Added Custom1" = Table.AddColumn(#"Changed Type2", "Total Minutes", each Duration.TotalMinutes([Duration])), #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom1",{{"Total Minutes", Int64.Type}}) in #"Changed Type3"If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
- 5 years ago
My logic is intuitive, first substitute Days/Hours/Minutes with 1440/60/1 respectively; then sum it up.
Pls refer to the following M code,
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Replaced Value" = Table.ReplaceValue(Source,"Days","1440",Replacer.ReplaceText,{"Text"}), #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Hours","60",Replacer.ReplaceText,{"Text"}), #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Minutes","1",Replacer.ReplaceText,{"Text"}), Custom1 = Table.TransformColumns(#"Replaced Value2", {"Text", each List.Transform(Text.Split(_, " "), Number.From)}), Custom2 = Table.TransformColumns(Custom1, {"Text", each let D = _{0}*_{1}, h = _{2}?*_{3}?, H = if h=null then 0 else h, m = _{4}?*_{5}?, M = if m=null then 0 else m, span = D+H+M in span }) in Custom2
My logic is intuitive, first substitute Days/Hours/Minutes with 1440/60/1 respectively; then sum it up.
Pls refer to the following M code,
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Replaced Value" = Table.ReplaceValue(Source,"Days","1440",Replacer.ReplaceText,{"Text"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Hours","60",Replacer.ReplaceText,{"Text"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Minutes","1",Replacer.ReplaceText,{"Text"}),
Custom1 = Table.TransformColumns(#"Replaced Value2", {"Text", each List.Transform(Text.Split(_, " "), Number.From)}),
Custom2 = Table.TransformColumns(Custom1, {"Text", each
let
D = _{0}*_{1},
h = _{2}?*_{3}?,
H = if h=null then 0 else h,
m = _{4}?*_{5}?,
M = if m=null then 0 else m,
span = D+H+M
in
span
})
in
Custom2