Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Duration with format H S

Hi   I have a huge excel file with incidences and durations, and the durations are for example 24H 53S , this format   if i change to duration it gives me an error, what i have to do to create a ...
  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Anonymous ,

     

    According to my understanding, you want to convert the text in format of (xxHxxM) to Duration type, right?

    Please try to change the original value to seconds(Hour *3600 + Minute *60) ,the whole M syntax is shown below:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsshQitWJVjI0zTC1yAUzjYwzTE0RTHME0wTBNIAyTTJgQoYGcFml2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Duration = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source,"h"," ",Replacer.ReplaceText,{"Duration"}),
        #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","m","",Replacer.ReplaceText,{"Duration"}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Value1", "Duration", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Duration.1", "Duration.2"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Duration.1", Int64.Type}, {"Duration.2", Int64.Type}}),
        #"Replaced Value2" = Table.ReplaceValue(#"Changed Type",null,0,Replacer.ReplaceValue,{"Duration.2"}),
        #"Renamed Columns" = Table.RenameColumns(#"Replaced Value2",{{"Duration.1", "Hour"}, {"Duration.2", "Minute"}}),
        #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Duration", each #duration(0,0,0,[Hour]*3600 +([Minute]+0)*60)),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Hour", "Minute"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Duration", type duration}})
    in
        #"Changed Type1"

    Best Regards,
    Eyelyn Qin
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.