Forum Discussion
Anonymous
2 years agoNot applicable
converting text in duration
Hi guys! I do have some issues with converting text to time. our system gives me: 9m7s780ms 9m7s40ms 9m6s9ms 9m658ms 9m658ms sometimes even with hours which would be: 1h5min23s253ms i did ...
- 2 years ago
Here is a custom function that
- Takes as input the strings in the formats you show
- Splits them apart
- Determines the type of unit
- Combines them into a duration
(dur as text)=> let //split on letter to digit to create list of the units and the identifier split1 = Splitter.SplitTextByCharacterTransition((c)=>not List.Contains({"0".."9"},c),{"0".."9"})(dur), //split each of the above to create a list of lists split2 = List.Transform(split1, (sp)=>Splitter.SplitTextByCharacterTransition({"0".."9"}, (c)=>not List.Contains({"0".."9"},c))(sp)), //determine the type of unit, then convert the value to a Number //"Days" can be added if necessary. Assumed to be zero (0) hrs = try Number.From(List.Select(split2, each _{1} = "h"){0}{0}) otherwise 0, mins = try Number.From(List.Select(split2, each _{1} = "m" or _{1} = "min"){0}{0}) otherwise 0, sec = try Number.From(List.Select(split2, each _{1} = "s"){0}{0}) otherwise 0, msec = try Number.From(List.Select(split2, each _{1} = "ms"){0}{0}) otherwise 0 in #duration(0,hrs,mins,sec + msec/1000)Used in a Query:
let Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"Durations", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Duration Values", each fnDurationTextConv([Durations]), type duration) in #"Added Custom"Results
Please note that if you are going to be using Power BI Desktop (and not Excel), that the duration data type in Power BI may not support milliseconds. If that is the case in your usage, you may need to convert the durations into a plain numeric value.
slorin
2 years agoSuper User
Hi
another solution
Duration.From(
Expression.Evaluate(
Text.Replace(
Text.Replace(
Text.Replace(
Text.Replace(
[YourColumn],
"h","/24+"),
"min","/(24*60)+"),
"ms","/(24*60*60*1000)"),
"s","/(24*60*60)+")
)
)
Stéphane
- Anonymous2 years agoNot applicable
It seems as I Made a mistake