Forum Discussion
CCAd
3 years agoFrequent Visitor
Unstructured time duration values in column
Hi, I have a data dump in csv that has a number of columns but there is one which is rather tricky. It contains values that are not in a standard format and that is creating a major obstacle in ...
- 3 years ago
Hi CCAd
This code in Power Query will convert this column into Durations
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VZA7EsMgDESvoqFOEQnx8VkYdy7kIrl/GdkeFqXbxz4hhjHSZiTb5/ym/TWSvJ14kvaZ2KjBgc1ilEtwMiRWJ0AzKjV43P+uaKESnZDdQ9GNUMhBzIZYVyxP1KX6WI67WEAlLrsovMNJa1y+5u4vQvcc7z8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Time to first action" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time to first action", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Days", each if Text.Contains([Time to first action], "d") then Text.Start([Time to first action], Text.PositionOf([Time to first action] , "d")) else 0), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Hours", each if [Days] = 0 then if Text.Contains([Time to first action], "h") then Text.Start([Time to first action], Text.PositionOf([Time to first action] , "h")) else 0 else Text.Select(Text.Middle([Time to first action], Text.PositionOf([Time to first action] , "d")+1), {"0".."9"})), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Minutes", each if Text.Contains([Time to first action], "min") then if [Hours] = 0 then Text.Select( [Time to first action], {"0".."9"}) else Text.Select(Text.Middle([Time to first action] , Text.PositionOf([Time to first action] , " ")), {"0".."9"}) else 0), #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom2",{{"Days", Int64.Type}, {"Hours", Int64.Type}, {"Minutes", Int64.Type}}), #"Added Custom3" = Table.AddColumn(#"Changed Type1", "Duration", each #duration([Days],[Hours],[Minutes],0)) in #"Added Custom3"In my example file I've left the Days, Hours, Mins columns in place so you can see how the code works, but also in case you wanted to do something else with them.
Just delete them if you don't need them
Regards
Phil
PhilipTreacy
3 years agoSuper User
Hi CCAd
No worries.
If you paste your code here I'll try to integrate my code into it.
If you can also supply a sample of the full data that the query is working on that will help.
regards
Phil
CCAd
3 years agoFrequent Visitor
Mate, your code worked just awesome. I was able to integrate it with the pre-existing one. But again, thank you so much!