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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Converting 1h 30m etc into time in PowerQuery

Hellooo!

 

The dataset I'm currently using exports time such as 10m or 1h 30m or 2d 5h 45m etc... 

 

I'm trying to convert this to a time type in PowerQuery but keep getting errors. Does anyone know how I can get Power BI to read this as 00:00:10 or 00:01:30 or 02:05:45 etc?

1 ACCEPTED SOLUTION

I think I got this working

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMsxQMDbIVYrVATLRaNMMBRNTqBCUBiuNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Time Spent" = _t]),
#"Lowercased Text" = Table.TransformColumns(Source,{{"Time Spent", Text.Lower, type text}}),
#"Inserted Text Before Delimiter" = Table.AddColumn(#"Lowercased Text", "Text Before Delimiter", each Text.BeforeDelimiter([Time Spent], "h ", {0, RelativePosition.FromEnd}), type text),
#"Inserted Last Characters" = Table.AddColumn(#"Inserted Text Before Delimiter", "Last Characters", each Text.End([Time Spent], 3), type text),
#"Replaced Value" = Table.ReplaceValue(#"Inserted Last Characters","m","",Replacer.ReplaceText,{"Last Characters"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","","0",Replacer.ReplaceValue,{"Text Before Delimiter"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","","0",Replacer.ReplaceValue,{"Last Characters"}),
#"Inserted Merged Column" = Table.AddColumn(#"Replaced Value2", "Duration", each Text.Combine({[Text Before Delimiter], [Last Characters]}, ":"), type text),
#"Changed Type" = Table.TransformColumnTypes(#"Inserted Merged Column",{{"Duration", type duration}})
in
#"Changed Type"





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

8 REPLIES 8
Daviejoe
Memorable Member
Memorable Member

This is an interesting one @Anonymous 

 

Can you display a small table to data please so we can see more examples of the data you are trying to convert?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

@Daviejoe 

Time Spent
1h 30m
10m
10m
5h 45m
15m
30m

I think I got this working

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMsxQMDbIVYrVATLRaNMMBRNTqBCUBiuNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Time Spent" = _t]),
#"Lowercased Text" = Table.TransformColumns(Source,{{"Time Spent", Text.Lower, type text}}),
#"Inserted Text Before Delimiter" = Table.AddColumn(#"Lowercased Text", "Text Before Delimiter", each Text.BeforeDelimiter([Time Spent], "h ", {0, RelativePosition.FromEnd}), type text),
#"Inserted Last Characters" = Table.AddColumn(#"Inserted Text Before Delimiter", "Last Characters", each Text.End([Time Spent], 3), type text),
#"Replaced Value" = Table.ReplaceValue(#"Inserted Last Characters","m","",Replacer.ReplaceText,{"Last Characters"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","","0",Replacer.ReplaceValue,{"Text Before Delimiter"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","","0",Replacer.ReplaceValue,{"Last Characters"}),
#"Inserted Merged Column" = Table.AddColumn(#"Replaced Value2", "Duration", each Text.Combine({[Text Before Delimiter], [Last Characters]}, ":"), type text),
#"Changed Type" = Table.TransformColumnTypes(#"Inserted Merged Column",{{"Duration", type duration}})
in
#"Changed Type"





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




This was also a useful article I found

 

https://www.excelcampus.com/powerquery/convert-text-to-time-values-power-query/





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Thank you!!

No worries, I do love Power Query.  I still can't believe I was using Excel for years without knowing about it haha!

 

Have a great day @Anonymous 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Do you really want time type or do you want duration?

Anonymous
Not applicable

@HotChilli  Oh yeah duration, so I can total up the total time spent 

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.