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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
TimR
New Member

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

2 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
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"

Jimmy801_0-1612958835934.png

 

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

View solution in original post

Jimmy801
Community Champion
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

View solution in original post

4 REPLIES 4
Jimmy801
Community Champion
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"

Jimmy801_0-1612958835934.png

 

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

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
Community Champion
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

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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors