Power Query Data types and excel time format issue

indrajeet_rajput

New Member
Joined
Sep 7, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
So, I have a column for time duration in my data from different excel files and tables which gets consolidated in power query...

But the data in the time duration column from these different files/tables is not the same, it's in these below 3 example formats:

00:24:02
00h 24m 02s
24m 02s

01h 24m 02s
01:24:02
01h 24m 02s


The first three examples represent 24 minutes and 2 seconds
The last three examples represent 1 hour, 24 minutes and 2 seconds.

The data is always in these three formats representing the time duration. And I have to do some conditional calculations and add measures using this time duration column but I can't do that unless I am able to convert it into a standard/default time format for my calculations.

Is there a way we can convert all three types in to one single time format

1)Either in excel before the data gets loaded in power query for consolidation?

2)Or in power query after its loaded for consolidation?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
There might be different and better ways, but this one is "easy"

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Times"]}[Content],
    Fn = Table.AddColumn(Source, "Time", each [
      IsText = Value.Type([Timvalues]) = Text.Type
    , TimeIn = if IsText then null else Time.From([Timvalues])
    , ValIn = Text.Split([Timvalues], " ")
    , Hr = if IsText then Text.Remove(List.Select(ValIn, each Text.Contains(_, "h")){0}, "h") else Time.Hour(TimeIn)
    , Min = if IsText then Text.Remove(List.Select(ValIn, each Text.Contains(_, "m")){0}, "m") else Time.Minute(TimeIn)
    , Sec = if IsText then Text.Remove(List.Select(ValIn, each Text.Contains(_, "s")){0}, "s") else Time.Second(TimeIn)
    , ValOut = #time(try Number.From(Hr) otherwise 0, try Number.From(Min)otherwise 0, try Number.From(Sec) otherwise 0)
][ValOut], type time)
in
    Fn

Book1
AB
1TimvaluesTime
20,0166898150:24:02
300h 24m 02s0:24:02
424m 02s0:24:02
501h 24m 02s1:24:02
60,0583564811:24:02
701h 24m 02s1:24:02
80,0002430560:00:21
90h 00m 21s0:00:21
1021s0:00:21
110,58395833314:00:54
1214h 00m 54s14:00:54
1314h 54s14:00:54
Times
 
Upvote 1
Assuming Table Name is "Table1" and Column Name is "Column1", try:

Power Query:
let
    fx = (x) => let a = List.Reverse (Splitter.SplitTextByAnyDelimiter({"d","h", "m","s"})(Text.Replace(x, "s", "")))
                in let b = List.Accumulate({0..3}, {}, (s,c)=> s & { try  Number.FromText(a{c}) otherwise 0 })
                in #duration(b{3}, b{2},b{1},b{0}),
    #"Duration" = Table.TransformColumnTypes(Table.TransformColumns(Excel.CurrentWorkbook(){[Name="Table1"]}[Content], {"Column1",
                  each try Duration.From(_) otherwise fx(_)}),{{"Column1", type duration}})
in
    #"Duration"

Regards,
 
Last edited:
Upvote 0
Do not consider my previous script: it is wrong - Sorry
 
Upvote 0
Updated function to convert in duration

Power Query:
let   
    fx_duration = (SourceTable as text, ColName as text) as table =>
      let x = Excel.CurrentWorkbook(){[Name=SourceTable]}[Content],
         fx = (x) =>
           let
            a = List.RemoveLastN(Splitter.SplitTextByAnyDelimiter({"d","h", "m","s"})(x),1),
            b = List.Select(Text.ToList(x), each List.ContainsAny({_}, {"a".."z"})),
            c = Table.FromRows(List.Zip({b,a}) &
                List.Transform(List.Difference({"d","h", "m","s"} , b), each {_, "0"})
                ),
            d = List.Transform(
                Record.FieldValues(
                Record.ReorderFields(Record.FromList(c[Column2], c[Column1]), {"d","h", "m","s"})
                ), Number.FromText),
            e = List.Accumulate({0..3}, {}, (s,c)=> s & {d{c}})
          in #duration(d{0}, d{1},d{2},d{3})
       in Table.TransformColumnTypes(
          Table.TransformColumns(x, {ColName, each try fx(_) otherwise _}), {{ColName, type duration}}
          ),
    Result = fx_duration("Table1", "Column1")
in
    Result
 
Upvote 0
So, I have a column for time duration in my data from different excel files and tables which gets consolidated in power query...

But the data in the time duration column from these different files/tables is not the same, it's in these below 3 example formats:

00:24:02
00h 24m 02s
24m 02s

01h 24m 02s
01:24:02
01h 24m 02s


The first three examples represent 24 minutes and 2 seconds
The last three examples represent 1 hour, 24 minutes and 2 seconds.

The data is always in these three formats representing the time duration. And I have to do some conditional calculations and add measures using this time duration column but I can't do that unless I am able to convert it into a standard/default time format for my calculations.

Is there a way we can convert all three types in to one single time format

1)Either in excel before the data gets loaded in power query for consolidation?

2)Or in power query after its loaded for consolidation?
Inder,
If you can share a sample data I can attempt and come back to you with solution.
The time conversion is simple but needs a bit of attention while playing around.

Anand
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top