Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Convert hr/min/s to seconds

I have this data set where the numbers have been input as mostly minutes and seconds, with some having hours, mins and seconds (highlighted).   I need to convert it to a usable format so I can comp...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous ,

     

    Add a new column then change type to Whole Number

    [
          c = List.Count(Text.PositionOf([Total Talk Time], ":", Occurrence.All)),  // how many :             
          t = Time.FromText(
            if c = 1 then
              "00: " & [Total Talk Time]
            else
              [Total Talk Time]), // convert to Time type
          s = Time.Hour(t) * 3600 + Time.Minute(t) * 60 + Time.Second(t)
        ][s]

    Output:

     

    Or do it using DAX:

    Column = 
    var _replace=SUBSTITUTE([Total Talk Time],":","")
    var _count= LEN([Total Talk Time]) - LEN(_replace) 
    var _time= CONVERT(IF(_count=1, "00: "&[Total Talk Time],[Total Talk Time]),DATETIME)
    return  HOUR(_time) *3600+ MINUTE(_time) *60+SECOND(_time)

     

    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.