cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
misunika95
Frequent Visitor

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 🙂

1 ACCEPTED SOLUTION
artemus
Microsoft
Microsoft

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.

View solution in original post

7 REPLIES 7
artemus
Microsoft
Microsoft

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.

View solution in original post

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.

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

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})

 

Thanks for taking the time to reply. I am running into this error at the else part of your code. 

 

I am pretty new to M code, can you please help correct any syntax issues i might have overlooked?

 

= Table.TransformColumns(#"Reordered Columns",{{"Time to Accept", each let parsed = Text.Split(_, ":") in 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}), type duration}})

 

Error -

 

Thaks again for your time.

 
 

pbi_pquery_delete.png

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.


 

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.

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Kudoed Authors