Forum Discussion

misunika95's avatar
misunika95
Frequent Visitor
6 years ago
Solved

Duration conversion issues

Hi Everyone!

 

I have some data that reflects how long a person took to resolve a dispute. The format is hh:mm (for example 1328:45) and when I try to change the data type to duration the following error appears in Power Query:

 

Expression.Error: We couldn't parse the Duration literal.
Details:
1328:45

 

Does anyone know how to fix this problem? Thanks in advance guys :)

  • You can use the following custom step:

    = Table.TransformColumns(PreviousStep,{{"ColumnName", each let parsed = Text.Split(_, ":") in #duration(0, 0, Int64.From(parsed{0}), Int8.From(parsed{1})), type duration}})

    Just change PreviousStep and "ColumnName" to appropiate values.

12 Replies

  • artemus's avatar
    artemus
    Microsoft Employee

    You can use the following custom step:

    = Table.TransformColumns(PreviousStep,{{"ColumnName", each let parsed = Text.Split(_, ":") in #duration(0, 0, Int64.From(parsed{0}), Int8.From(parsed{1})), type duration}})

    Just change PreviousStep and "ColumnName" to appropiate values.

    • misunika95's avatar
      misunika95
      Frequent Visitor

      It worked, thanks a lot! Do you happen to know how can I display the data? At the visualization level I get very low number values.

      Thank you!!


      artemus wrote:

      You can use the following custom step:

      = Table.TransformColumns(PreviousStep,{{"ColumnName", each let parsed = Text.Split(_, ":") in #duration(0, 0, Int64.From(parsed{0}), Int8.From(parsed{1})), type duration}})

      Just change PreviousStep and "ColumnName" to appropiate values.


       

      • artemus's avatar
        artemus
        Microsoft Employee

        As part of your final step you can change the column type back to text. Note that if you do this, it will display it in [dd.][hh:][mm:]ss format (as seen in the previewer.

         

        However, if you do this it will not sort correctly in the visualizer. To get around this, duplicate the duration column and then change the one you want to display to text. After loading it into the report view, select the text field, then select Modeling from the top ribbon and Sort by Column and choose the duration column that wasn't changed to text.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi artemus ,

       

      Thank you for the solution, it helped fix a similar issue i had with my numbers. I do have a quick question about the logic.

       

      So, using your formula, i see a cell which was previously text(hh:mm:ss) 1594:44:28 changes to a duration of 1.02:34:44 (ddd.hh:mm:ss) post applying this logic. I am a bit confused, i was expecting to see around 66 days instead of the 1.02 like above.

       

      It'd greatly help if you can explain the logic of the custom step. 

       

      Thanks for your time.

       

      -K

      • artemus's avatar
        artemus
        Microsoft Employee

        The formula I gave only works for mm:ss, not hh:mm:ss. It treated your 1594:44:28 as 1594:44

         

        If you had that format you could use:

        #duration(0, Int64.From(parsed{0}), Int8.From(parsed{1}, Int8.From(parsed{2})

         

        This would only work if you always had the hour component (event if it was 0). If you wanted to support both formats would you need to:

         

        if parsed{2}? = null then #duration(0, 0, Int64.From(parsed{0}), Int8.From(parsed{2})) else #duration(0, Int64.From(parsed{0}), Int8.From(parsed{1}), Int8.From(parsed{2}))

         

         

    • ddavil8tx's avatar
      ddavil8tx
      Regular Visitor

      Just to clarify to other PBI users. I insert this code as a custom step where I would have changed the data type to Duration. And it worked perfectly for me. Thank you.