Time Transformation in Power Query: From Conventional Time Format to Decimal Hours

Today, I want to share a simple yet practical solution from a client meeting. Perhaps you’ll face a similar challenge one day, and this solution could help you resolve it efficiently.

The Challenge

The task was to convert a time duration from the conventional time format (e.g., 25:15) into a decimal hour format. Decimal hours are particularly useful for calculations such as summations or averages involving time values.

The twist? The time value could exceed 24 hours, which meant that standard transformations in Power Query weren’t viable. Treating the value as either a „Time“ or „Duration“ data type led to errors or unusable results.

The Solution

The solution involved treating the time value as text, splitting it at the colon (:) to separate hours and minutes, and then converting the minutes into fractional hours before adding them to the hours.

Here’s the Power Query code for creating a custom column:

Number.From(Text.Split([TimeDuration], ":"){0}) + 
(Number.From(Text.Split([TimeDuration], ":"){1}) / 60)

Time Transformation in Power Query

How the Code Works

  1. Text.Split([TimeDuration], „:“)
    Splits the text value of the time at the colon, producing two parts:
    • {0} for the hours.
    • {1} for the minutes.
  2. Number.From(…)
    Converts the extracted text values into numeric values.
  3. Convert Minutes to Fractional Hours:
    Divides the minutes by 60 to convert them into a fractional part of an hour.
  4. Add the Values Together:
    Combines the numeric hour value with the fractional hour value to produce the total decimal hours.

Example

Suppose the [TimeDuration] column contains the value 254:15. The formula processes it as follows:

  • Hours: 25
  • Minutes: 15 / 60 = 0.25
  • Result: 25 + 0.25 = 25.25

Conclusion

With this straightforward approach, you can easily convert time values into decimal hours, even when they exceed the 24-hour mark. This solution is especially valuable when working in Power Query and needing time values in a format suitable for further analysis or calculations.

I hope this little tip simplifies your future Power Query challenges! 😊

Veröffentlicht von

Marcus Wegener

Marcus Wegener

Marcus Wegener ist Anwendungsentwickler für Business Intelligence und erstellt Lösungen, mit denen sich große Datenmengen schnell analysieren lassen. Kunden nutzen seine Lösungen, um die Vergangenheit zu analysieren, die Gegenwart zu steuern und die Zukunft zu planen, um damit mehr Erfolg zu generieren. Dabei ist seine einzigartige Kombination aus Wissen und Auffassungsgabe ein Garant für ihren Erfolg.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.