6

I'm trying to Query data from another data source in Excel, and am receiving parsing errors for any durations larger than 24:00:00.

Expression.Error: We couldn't parse the Duration literal. Details: 51:33:08

How do I correctly have excel parse durations such as 128:22:13 as a duration?

Edit:

The data is currently text in HH:MM:SS format. I am using the Excel Query Tools to import data from an external source. There is no code, and I am not trying to convert cells in a worksheet. These conversions are part of the Query Tools:

enter image description here

7
  • Can you post the parsing code that's producing the error?
    – xidgel
    Aug 11, 2016 at 22:32
  • For someone with 1K+ reputation, I would expect you would know how to ask a question on stackoverflow better than that by now even if you are in a hurry. You can convert it to text by adding ' in front or convert it to a number in the query.
    – Slai
    Aug 11, 2016 at 22:40
  • @xidgel There is no parsing code, I would post it if there was. It's "automatic" parsing using the Excel Query tools. Aug 11, 2016 at 22:42
  • 1
    @Slai Please don't be unnecessarily rude. It is currently text, in HH:MM:SS format, that needs to be parsed into a duration with the Query Tools. These are not cells I am editing. I have added more details. Aug 11, 2016 at 22:44
  • 1
    @xidgel Thanks, I didn't even know this was Power Query. I found it from "Microsoft Query" Aug 11, 2016 at 23:33

2 Answers 2

11

We have a Duration.FromText library function but that's documented to only allow between 0 and 23 hours. This function is what's getting applied under the hood when you change type from text to duration.

Luckily the #duration constructor has no such restriction, so here's a simple little parsing function you can use.

let
    DurationFromYourText = (text as text) as duration => let
        Split = Text.Split(text, ":"),
        AsNumbers = List.Transform(Split, Number.FromText),
        Duration = #duration(0, AsNumbers{0}, AsNumbers{1}, AsNumbers{2})
    in
        Duration,
    Invoked = DurationFromYourText("128:22:13")
in
    Invoked
3
  • Thanks Carl. I'm new to Excel, but not to coding, where would I actually put this code in Excel to be able to use it in Power Query? Or where would I go to add a new "type" to the Power query. Aug 12, 2016 at 17:42
  • 2
    @DouglasGaskell You can paste in this function into the Advanced Editor (be careful about commas as we have a strict syntax). Then you could click Add Custom Column with = DurationFromYourText([Total Duration]). If you google how to create custom Power Query / M functions there's plenty of blog posts that walk through in more detail than I can. Maybe mattmasson.com/2014/11/… is a good one to start with?
    – Carl Walsh
    Aug 12, 2016 at 18:22
  • @DouglasGaskell here's a quick-start of M that might be more helpful to learn the "M" query language than the Excel-focused blog posts: msdn.microsoft.com/en-us/library/mt270235.aspx
    – Carl Walsh
    Aug 12, 2016 at 18:24
0

You can split the text into two or more columns https://support.office.com/en-us/article/Split-a-column-of-text-Power-Query-5282d425-6dd0-46ca-95bf-8e0da9539662#__toc354843579

After that you can divide the hours column by 24 to get the days, and modulus 24 to get the remaining hours. For the mod, you might have to change some of the Power Query Formula Language M https://msdn.microsoft.com/en-us/library/mt253344.aspx

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.