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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
patienza
Regular Visitor

PowerBI-Grouping Events happening within 12 hours

Hello! I am fairly new working with PowerBI, I am struggling with creating the following dax.

I have a date column "Metric_DateTime" on my "All Loaded Incidents" table. I want to create a new column "Group ID" that groups all events happening within 12hours of each other under the same Group ID. Example below.  Thank you in advance for your help. 

Metric_DateTimeGroup ID
2025-01-20 8:071
2025-01-20 15:021
2025-01-20 21:122
2025-01-21 18:013
2025-01-21 22:043
2025-01-22 11:004
2025-01-22 15:124
2025-01-24 15:004
2025-01-24 17:044
2025-01-25 8:005
2025-01-25 12:005
2025-01-25 13:245
2 ACCEPTED SOLUTIONS
patienza
Regular Visitor

thank you very much for your solution and time. Again, Im new with BI so not exactly sure if I am looking at the right thing, but I tried to look at the PowerQuery but only see the data source error. Can you provide the solution in DAX form so I can add it as a new column?

patienza_0-1743667088664.png

 

View solution in original post

Hi @patienza,
To view the Power Query script in Power BI, open the Advanced Editor. Below is the DAX query, but ensure you make the necessary adjustments based on your column names and source.

 

Below is the Dax Query : 

 

let
    Source = Excel.Workbook(File.Contents("PATH"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Metric_DateTime", type datetime}}),

 

    // Sort data by Metric_DateTime
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Metric_DateTime", Order.Ascending}}),

 

    // Add an Index Column
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),

 

    // Add Previous Row's Metric_DateTime
    #"Added Prev Time" = Table.AddColumn(#"Added Index", "Prev Metric_DateTime", 
        each if [Index] = 1 then null 
             else #"Added Index"[Metric_DateTime]{[Index] - 2}, type nullable datetime),

 

    // Determine if a new group should start (if time difference > 12 hours)
    #"Added Group Flag" = Table.AddColumn(#"Added Prev Time", "New Group", 
        each if [Prev Metric_DateTime] = null 
             or Duration.TotalHours([Metric_DateTime] - [Prev Metric_DateTime]) > 12 then 1 else 0, 
        Int64.Type),

 

    // Generate Group ID using a running total of the "New Group" column
    #"Added Group ID" = Table.AddColumn(#"Added Group Flag", "Group ID", 
        each List.Sum(List.FirstN(#"Added Group Flag"[New Group], [Index])), 
        Int64.Type),
    // Remove unnecessary columns
    #"Removed Columns" = Table.RemoveColumns(#"Added Group ID",{"Index", "Prev Metric_DateTime", "New Group"})

in
    #"Removed Columns"

View solution in original post

5 REPLIES 5
patienza
Regular Visitor

thank you very much! this worked perfectly

patienza
Regular Visitor

thank you very much for your solution and time. Again, Im new with BI so not exactly sure if I am looking at the right thing, but I tried to look at the PowerQuery but only see the data source error. Can you provide the solution in DAX form so I can add it as a new column?

patienza_0-1743667088664.png

 

Hi @patienza,
To view the Power Query script in Power BI, open the Advanced Editor. Below is the DAX query, but ensure you make the necessary adjustments based on your column names and source.

 

Below is the Dax Query : 

 

let
    Source = Excel.Workbook(File.Contents("PATH"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Metric_DateTime", type datetime}}),

 

    // Sort data by Metric_DateTime
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Metric_DateTime", Order.Ascending}}),

 

    // Add an Index Column
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),

 

    // Add Previous Row's Metric_DateTime
    #"Added Prev Time" = Table.AddColumn(#"Added Index", "Prev Metric_DateTime", 
        each if [Index] = 1 then null 
             else #"Added Index"[Metric_DateTime]{[Index] - 2}, type nullable datetime),

 

    // Determine if a new group should start (if time difference > 12 hours)
    #"Added Group Flag" = Table.AddColumn(#"Added Prev Time", "New Group", 
        each if [Prev Metric_DateTime] = null 
             or Duration.TotalHours([Metric_DateTime] - [Prev Metric_DateTime]) > 12 then 1 else 0, 
        Int64.Type),

 

    // Generate Group ID using a running total of the "New Group" column
    #"Added Group ID" = Table.AddColumn(#"Added Group Flag", "Group ID", 
        each List.Sum(List.FirstN(#"Added Group Flag"[New Group], [Index])), 
        Int64.Type),
    // Remove unnecessary columns
    #"Removed Columns" = Table.RemoveColumns(#"Added Group ID",{"Index", "Prev Metric_DateTime", "New Group"})

in
    #"Removed Columns"

patienza
Regular Visitor

patienza_0-1743648653875.png

 

Hi @patienza,

Thank you for reaching out to Microsoft Fabric Community.

 

I have reproduced your scenario and attached the pbix file for your reference.

 

If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.