Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Ali5457
Frequent Visitor

Calculating time duration between two times with the whole number and 24 hr formats

Hi,

 

I would like to calculate the time duration between the following two fields as hh:mm or mm. These two columnes are stored as a whole number, 24hr format. Also, as you could see not all of them are 4 digits. I have tried different solutions like converting them to the text as 4 digists like 1056 or 0856 (adding 0 to the beginning of those cells that their lenghts are less than 4) as stored them as the text, or changing them to 10:56 or 08:56 format and changed the type to the text, or even changing their data type to the time but none of them worked. In the latter case, all cells are changed to "12:00:00 am". In the other cases, I get all the errors related to data type that I cannot convert the value of type text to type number.

 

I have looked at all the post related to time function and text to time functions but nothing worked.

 

 

Ali5457_0-1665632377996.png

Thanks.

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @Ali5457 ,
please check the following solution.
I am using the #time-function in there to construct the result:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText("i45WsjA0U9JRsjQ1U4rViVYyNDa1BHINTUwMlGJjAQ==", BinaryEncoding.Base64),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [LogTime = _t, tmAtScene = _t]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {{"LogTime", Int64.Type}, {"tmAtScene", Int64.Type}}
    ),
    #"Added Custom" = Table.AddColumn(
        #"Changed Type",
        "DiffInTime",
        each #time(Number.IntegerDivide([tmAtScene], 100), Number.Mod([tmAtScene], 100), 0)
            - #time(Number.IntegerDivide([LogTime], 100), Number.Mod([LogTime], 100), 0)
    ),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom", {{"DiffInTime", type duration}}),
    #"Inserted Total Minutes" = Table.AddColumn(
        #"Changed Type1",
        "Total Minutes",
        each Duration.TotalMinutes([DiffInTime]),
        type number
    )
in
    #"Inserted Total Minutes"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
Ali5457
Frequent Visitor

Yes, that worked. Thanks @ImkeF .

ImkeF
Super User
Super User

Hi @Ali5457 ,
please check the following solution.
I am using the #time-function in there to construct the result:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText("i45WsjA0U9JRsjQ1U4rViVYyNDa1BHINTUwMlGJjAQ==", BinaryEncoding.Base64),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [LogTime = _t, tmAtScene = _t]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        Source,
        {{"LogTime", Int64.Type}, {"tmAtScene", Int64.Type}}
    ),
    #"Added Custom" = Table.AddColumn(
        #"Changed Type",
        "DiffInTime",
        each #time(Number.IntegerDivide([tmAtScene], 100), Number.Mod([tmAtScene], 100), 0)
            - #time(Number.IntegerDivide([LogTime], 100), Number.Mod([LogTime], 100), 0)
    ),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom", {{"DiffInTime", type duration}}),
    #"Inserted Total Minutes" = Table.AddColumn(
        #"Changed Type1",
        "Total Minutes",
        each Duration.TotalMinutes([DiffInTime]),
        type number
    )
in
    #"Inserted Total Minutes"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors