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
MarkRS
Helper I
Helper I

Grouping rows with similar, but not identical time values

Hi Everybody, I've got a query where I group rows which have similar values in several columns, including one coulmn which contains a time value. I need to expand this to also group rows where the time values are within a certain tolerance of one another - for example 15 or 30 minutes. It would also be good to aggregate the min and max time values into new columns.

 

This is a bit more complicated than the standard group by functionality, so I wondered how to achieve this?

 

Many thanks

2 REPLIES 2
amitchandak
Super User
Super User

Can you share sample data and sample output. If possible please share a sample pbix file after removing sensitive information.Thanks.

My Recent Blog -

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

Hi,

 

Thank you for helping  - Here's a simplified example of the input:

Client IDPlanned Start DatePlanned Start TimePlanned Duration
120/07/202009:0030
121/07/202009:0030
122/07/202009:0030
123/07/202009:0030
124/07/202009:0030
125/07/202008:4530
126/07/202009:1530
120/07/202012:0030
121/07/202012:0030
122/07/202012:0030
123/07/202012:0030
124/07/202011:3030
125/07/202012:3030
126/07/202012:3030

 

I can use Table.Group to group all rows which have the same Client ID, Planned Start Time and Planned Duration:

 

= Table.Group(#"Changed Type", {"Client ID", "Planned Start Time", "Planned Duration"}, {{"data", each _, type table [Client ID=nullable number, Planned Start Date=nullable date, Planned Start Time=nullable time, Planned Duration=nullable number]}, {"Count", each Table.RowCount(_), Int64.Type}, {"Median Start Time", each List.Median([Planned Start Time]), type nullable time}})

 

Which gives this output:

 

Client IDPlanned Start TimePlanned DurationdataCountMedian Start Time
109:00:0030table509:00:00
108:45:0030table108:45:00
109:15:0030table109:15:00
112:00:0030table412:00:00
111:30:0030table111:30:00
112:30:0030table212:30:00

 

This is working as expected, but I would like to group rows where the Planned Start Time is within 30 minutes.

This would result in the first three rows being grouped into one: Planned Start Time 09:00, 08:45, 09:15.

The Median Start Time aggregation would then show the median of these times.

 

The last three rows are a bit more complicated since the start times are 12:00, 11:30 and 12:30.

The first two rows (12:00, 11:30) are within 30 minutes of each other, so they could be grouped to form a group of 5 rows.

However, if the 12:00 and 12:30 rows could be grouped that would be even better as it would form a group of 6 rows.

 

I probably need to add a helper column to identify which rows to merge. Any help would be greatly appreciated.

 

Although I've been doing this in power query, it would be fine, or even preferable, to do it in DAX.

 

I've uploaded a sample pbix here:

https://ufile.io/xyd0n3u9

 

Many thanks

 

 

 

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.