Decimal Number to Time

Hi there,

Please can someone help me. I have to do this in Power Query. I have a Decimal number say 7.30 (this means that the duration was 07:30). I can’t upload PBIX because of Company policy. If I convert to Duration, it reads the Integer portion as a whole day and the decimal portion as the hour which is wrong. Help Big Power Query Brains. I have looked everywhere on the Internet.

Hours Downtime
2
2
5
1,3
2,45
6
5
7,45
2,3
1
0
1,45
3
4
2
4
4
4
0
0
0
0
1,3
7
0
0
7,45
7,45
0
7,45
4,5
0
0
0
0
0
0
2
2
2
2
2
2
2
2
2,3
2
2
2
2,3
2
2,3
1
2
2
2,3
2
2
2
2,3
2
2
5
2,3
2,3
2
2
2
2
2
2
2
2
2,3
2
3
4,3
2,3
2,3
2,3
2,3
2
2
2
2
2
2,45
2,3
2
7
2
1,45
3
2
2,3
2
2
2
2
2
2
2,45
2,45
2,45
2,45
25
2
2
2
2,15
1,3
0,3
2,3
1
1,5
4
3
7,5
3
3,45
4
1
7,45
6,3
7,45
1
1
6,15
2
2
2,3
2,3
2,18
2,1
2
2
2
2
2
2
2
2
2
1
2,3
2,1
2,3
2,5
2,5
2,5
2,5
2
3
1
1,3
2,1
4
2,5
2,5
6
2
2
2
2
2
2
2,5
2
2
2
2
2
2
2
3
3
2,3
2,3
2,3
2,3
2,3
3
2,3
2,3
2,3
2,3
2,3
2,3
2
2
2
2
2
2
2
2
2
2
2
2
2
2,2
1
2
2,3
2
2,3
2
2,3
2,3
2
2
2
2
6
2
2,3
2
2,15
2,15
2,1
2,3
2,3
2
2
2,3
2
2
2,3
2
2
2,3
2
2
2
2
2
2
2
2,3
2
2,3
2,3
2
2
2,3
2
2,3
2,1
2,15
2,1
2,3
2
2
2
2
2
2
2
2
2
2,3
2
2
2
2
2,3
2,3
2
2
7
2
2,3
2,3
2
2
2
2
1
2
2
2,3
2,3
2
2,3
2,3
2
2,3
2
2
2
2
2,3
2
2,1
2,3
2,3
2
2
2
2
2
2
2
2
2
2,3
2
2
2,3
8
2
2
2
2
7
3,3
1
2
3
1,3
0,3
0,2
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2,3
2,3
2,3
3
3
3,3
3,3
3
3,3
3
3
3
2
3
3
3
3
8,3
2
5
2
2
5
3
2
2

Hi @SammiP,

You need to bring 7.3 to 07:30 to convert to time.

so little bit of transformation.

M code:

let 
// bring data, and in my Case Promoted Headers was the Step name and Time is Column name in decimal form
    ToText = Table.TransformColumnTypes(#"Promoted Headers",{{"Time", type text}}),
  NewTime = Table.AddColumn(ToText , "NewTime", each (Text.End( "0" & Text.From ( Text.BeforeDelimiter([Time],".") ), 2 ) &":"&Text.End( Text.From ( Text.AfterDelimiter([Time],".") ) &"0"  , 2 ))),
  ConvertNewTime = Table.TransformColumnTypes(NewTime ,{{"NewTime", type time}})
in
     ConvertNewTime

sample PBIX file :

DecimalToTime.pbix (25.9 KB)

You have Transformation step by step (so you can see method step by step)

and Shorten version

I use this Method / Shorten version approach after I saw Melissa’s great M code courses.
Of course when I find the method and get shorter version I don’t need both but I leave it so you can see approach.

Hope it helps.

2 Likes

Hi @SammiP,

Here’s an alternative.
Just copy the full script into a new blank query.

let
    fxDecimalTimeInMinToDuration = 
        ( DecimalTimeInMin ) as duration => 
            let
                t = Splitter.SplitTextByAnyDelimiter({",", "."})(Text.From( DecimalTimeInMin )),
                x = Number.From( t{0}? ?? "0" ),
                d = if x >1440 then x /1440 else 0, 
                h = if x >1440 then (Number.Mod( d, 1) *1440) /60 else if x >60 then x /60 else 0, 
                m = if x >60 then Number.Round(Number.Mod(h, 1) *60 ) else x,
                r = #duration( Number.RoundDown(d, 0), Number.RoundDown(h, 0), m, Number.From( Text.PadEnd( t{1}? ?? "0", 2, "0")) )
            in
        r,
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlWK1YlWMtQxhtB6ENpIxwQiYaQHZRhASB1DcwhDD8ow1DExhGqFMsxhWo0hegxNYAKWFiY6RkALYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [InMinutes = _t]),
    InvokeCustom = Table.AddColumn(Source, "fxDecimalTimeInMinToDuration", each fxDecimalTimeInMinToDuration([InMinutes]), type duration)
in
    InvokeCustom

I hope this is helpful

3 Likes

Hello @SammiP, good to see that you are having progress with your inquiry. Did the responses from @mspanic and @Melissa help you solve your inquiry?

If it does, kindly mark the answer that solved your query as the solution.

If not, how far did you get, and what kind of help do you need further?

Hi @SammiP, we noticed that no response was received from you with regard to our post last Aug 9th.

We just want to check if you still need further help with this post.

In case there won’t be any activity on it in the next few days, we’ll be tagging this post as Solved.

Hello @SammiP . Due to the length by which this post has been active, we are tagging it as Solved. Thanks to the contributors of this post. For further questions related to this post, please make a new thread. Feel free to reopen this thread if you anyone would like to answer the pending inquiry above.

1 Like