Forum Discussion

jerryr125's avatar
jerryr125
Helper IV
1 year ago
Solved

TIme Interval Calculation

Hi - I am looking to determine the time interview between records per LocationCode and Date

 

Example Data:

 

LocationCodeDate-Time-Stamp
1234-244/24/2025 10:00 am
1234-244/24/2025 11:00 am
1234-254/25/2025 1:00 pm
1234-254/25/2025 3:00 pm
4567-244/24/2025 10:00am
4567-244/24/2025 10:45 am
4567-244/24/2025 2:00 pm
4567-254/25/2025 8:00am
4567-254/25/2025 9:00am

Output:

LocationCodeDate-Time-StampTimeInterval-Minutes
1234-244/24/2025 10:00 am 
1234-244/24/2025 11:00 am60
1234-254/25/2025 1:00 pm 
1234-254/25/2025 3:00 pm120
4567-244/24/2025 10:00am 
4567-244/24/2025 10:45 am45
4567-244:24/2025 2:00 pm195
4567-25 4/25/2025 8:00am 
4567-25 4/25/2025 9:00am60



Any ideas ? Jerry

  • Here's one way:

      (Paste the code below into the Advanced Editor and explore the Applied Steps to understand.

       Then adapt to your actual data)

    • Group by LocationCode
    • Sort the subgroups (might not be necessary)
    • Add an Index column to each subgroup to enable 
      • Subtract each time stamp from the next one, UNLESS it is the first (Index = 0)
    • Expand the subtable
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNtE1MlHSUTLRNwIiAyNTBUMDKwMDhcRcpVgdHAoMMRSYQhSYQhWA5AtwyxsjyZuYmpljdUEiXnkTUwU8CowwLEB1gAW6+ajSllDpWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [LocationCode = _t, #"Date-Time-Stamp" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"LocationCode", type text}, {"Date-Time-Stamp", type datetime}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"LocationCode"}, {
            {"Intervals", (t)=>
                [a=Table.Sort(t, each [#"Date-Time-Stamp"]), //Sort may not be necessary
                 b=Table.AddIndexColumn(a,"Index",0,1,Int64.Type),
                 c=Table.AddColumn(b,"TimeInterval-Minutes", each 
                        if [Index] = 0 then null else Duration.TotalMinutes([#"Date-Time-Stamp"] - b[#"Date-Time-Stamp"]{[Index]-1})  )
                ][c], 
            type table [LocationCode=nullable text, #"Date-Time-Stamp"=nullable datetime, #"TimeInterval-Minutes"=Int64.Type]}}),
        
        #"Expanded Intervals" = Table.ExpandTableColumn(#"Grouped Rows", "Intervals",
                                    {"Date-Time-Stamp", "TimeInterval-Minutes"})
    in
        #"Expanded Intervals"

     

    Source

     

    Results

     

     

     

4 Replies

  • Here's one way:

      (Paste the code below into the Advanced Editor and explore the Applied Steps to understand.

       Then adapt to your actual data)

    • Group by LocationCode
    • Sort the subgroups (might not be necessary)
    • Add an Index column to each subgroup to enable 
      • Subtract each time stamp from the next one, UNLESS it is the first (Index = 0)
    • Expand the subtable
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNtE1MlHSUTLRNwIiAyNTBUMDKwMDhcRcpVgdHAoMMRSYQhSYQhWA5AtwyxsjyZuYmpljdUEiXnkTUwU8CowwLEB1gAW6+ajSllDpWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [LocationCode = _t, #"Date-Time-Stamp" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"LocationCode", type text}, {"Date-Time-Stamp", type datetime}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"LocationCode"}, {
            {"Intervals", (t)=>
                [a=Table.Sort(t, each [#"Date-Time-Stamp"]), //Sort may not be necessary
                 b=Table.AddIndexColumn(a,"Index",0,1,Int64.Type),
                 c=Table.AddColumn(b,"TimeInterval-Minutes", each 
                        if [Index] = 0 then null else Duration.TotalMinutes([#"Date-Time-Stamp"] - b[#"Date-Time-Stamp"]{[Index]-1})  )
                ][c], 
            type table [LocationCode=nullable text, #"Date-Time-Stamp"=nullable datetime, #"TimeInterval-Minutes"=Int64.Type]}}),
        
        #"Expanded Intervals" = Table.ExpandTableColumn(#"Grouped Rows", "Intervals",
                                    {"Date-Time-Stamp", "TimeInterval-Minutes"})
    in
        #"Expanded Intervals"

     

    Source

     

    Results

     

     

     

  • v-csrikanth's avatar
    v-csrikanth
    Community Support

    Hi jerryr125 

    Thank you for being part of the Microsoft Fabric Community.

    As highlighted by ronrsnfld, the proposed approach appears to effectively address your requirements. Could you please confirm if your issue has been resolved?
    If you are still facing any challenges, kindly provide further details, and we will be happy to assist you.

    Best Regards,
    Cheri Srikanth

  • v-csrikanth's avatar
    v-csrikanth
    Community Support

    Hi jerryr125 

    We haven't heard from you since last response and just wanted to check whether the solution provided has worked for you. If yes, please Accept as Solution to help others benefit in the community.

    If the above information is helpful, please give us Kudos and mark the response as Accepted as solution.
    Best Regards,
    Community Support Team _ C Srikanth.

  • v-csrikanth's avatar
    v-csrikanth
    Community Support

    Hi jerryr125 

    I wanted to follow up since I haven't heard from you in a while. Have you had a chance to try the suggested solutions?
    If your issue is resolved, please consider marking the post as solved. However, if you're still facing challenges, feel free to share the details, and we'll be happy to assist you further.
    Looking forward to your response!


    Best Regards,
    Community Support Team _ C Srikanth.