Forum Discussion

TimR's avatar
TimR
New Member
5 years ago
Solved

How to group durations

Hi, 

 

I'm looking for a way to group time durations.

 

The raw data contains durations in format h:mm:ss:xxx :

Duration action
1:01:07.560
0:26:01.123
0:00:14.330
0:00:11.253
0:04:12.287

 

I want to add a grouped duration based on these groups:

0 - 30 sec
30s - 1m
1m - 2m
2m - 5m
5m - 15m
15m - 30m
30m - ∞

 

Desired result:

Duration actionGrouped duration
1:01:07.56030m - ∞
0:26:01.12315m - 30m
0:00:14.3300 - 30 sec
0:00:11.2530 - 30 sec
0:04:12.2872m - 5m

 

Hope that there is someone that could help..

Many thanks in advance!!

 

Cheers,

 

Tim

  • Hello TimR 

     

    you can ues nested if-statements like this

    if [Duration action]<#duration(0,0,0,31) then "0 - 30 sec" else if [Duration action]<#duration(0,0,1,1) then "30s - 1m" else "etc."

    here a complete example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TYnJCcBADMR68TsMM/Yewa0Y999G9hFCQB9JVabkYWMuWl9lTF8nQR6vk6mBCP5c8Pn9kXL4va37AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Duration action" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Duration action", type duration}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Grouped duration", each if [Duration action]<#duration(0,0,0,31) then "0 - 30 sec" else if [Duration action]<#duration(0,0,1,1) then "30s - 1m" else "etc.")
    in
        #"Added Custom"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • Hello TimR 

     

    use this code instead

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TYnJCcBADMR68TsMM/Yewa0Y999G9hFCQB9JVabkYWMuWl9lTF8nQR6vk6mBCP5c8Pn9kXL4va37AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Duration action" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Duration action", type duration}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Grouped duration", each try if [Duration action]<#duration(0,0,0,31) then "0 - 30 sec" else if [Duration action]<#duration(0,0,1,1) then "30s - 1m" else "etc." otherwise null)
    in
        #"Added Custom"

     

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

4 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello TimR 

     

    you can ues nested if-statements like this

    if [Duration action]<#duration(0,0,0,31) then "0 - 30 sec" else if [Duration action]<#duration(0,0,1,1) then "30s - 1m" else "etc."

    here a complete example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TYnJCcBADMR68TsMM/Yewa0Y999G9hFCQB9JVabkYWMuWl9lTF8nQR6vk6mBCP5c8Pn9kXL4va37AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Duration action" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Duration action", type duration}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Grouped duration", each if [Duration action]<#duration(0,0,0,31) then "0 - 30 sec" else if [Duration action]<#duration(0,0,1,1) then "30s - 1m" else "etc.")
    in
        #"Added Custom"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

    • TimR's avatar
      TimR
      New Member

      Awesome Jimmy. Many thanks for your prompt reply! This works great

    • TimR's avatar
      TimR
      New Member

      Final question 🙂
      There are cells which do not contain a duration. This results in an error. How can I prevent the error and keep the Grouped duration empty if the duration action is empty?

      • Jimmy801's avatar
        Jimmy801
        Community Champion

        Hello TimR 

         

        use this code instead

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TYnJCcBADMR68TsMM/Yewa0Y999G9hFCQB9JVabkYWMuWl9lTF8nQR6vk6mBCP5c8Pn9kXL4va37AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Duration action" = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Duration action", type duration}}),
            #"Added Custom" = Table.AddColumn(#"Changed Type", "Grouped duration", each try if [Duration action]<#duration(0,0,0,31) then "0 - 30 sec" else if [Duration action]<#duration(0,0,1,1) then "30s - 1m" else "etc." otherwise null)
        in
            #"Added Custom"

         

        If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
        Kudoes are nice too

        Have fun

        Jimmy