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 ...
  • Jimmy801's avatar
    5 years ago

    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

  • Jimmy801's avatar
    Jimmy801
    5 years ago

    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