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

Get Fabric certified for FREE! Don't miss your chance! Learn more

Reply
Jeffrey_VC
Helper III
Helper III

Group by with timestamp in 3 minutes

Hi everyone,

 

I'm trying to make a Dax code that gives me a count of events but if there in the timestamp between them is less than 2 minutes that this counts as 1 event.

For example:

Eventdatetime
A1-1-20248:00:00
A1-1-20248:00:42
B1-1-20248:01:32
A1-1-20248:01:58
B1-1-20248:02:00
A1-1-20248:07:00
B1-1-20248:08:52
A1-1-20248:21:00
B1-1-20248:35:00
B1-1-20248:35:58

 

Needed results:

Event A: 3

Event B; 3

 

Thnx for the support.

1 ACCEPTED SOLUTION
SachinNandanwar
Impactful Individual
Impactful Individual

I assume you mean that if the diffrence between the two consecutive times for an event is less than 2 minutes then it should be considered as single record

SachinNandanwar_0-1728604620471.png

For example difference between 00:00 and 00:42 is less than 2 minutes and so is the difference between 00:42 and 01:58 less than 2 minutes , so both should be considered as a one count.difference between 01:58 and 07:00 is greater than 2 so it should be considered one new count.

 

Create a measure. t is the table name

 

TimeDifference =
VAR _M =
    DATEDIFF (
        CALCULATE (
            MAX ( t[time] ),
            OFFSET (
                -1,
                ( ALL ( t ) ),
                ORDERBY ( t[date].[Date], DESC ),
                ,
                PARTITIONBY ( t[Event] ),
                MATCHBY ( t[date].[Date], t[time] )
            )
        ),
        MAX( t[time] ),
        MINUTE
    )
RETURN
    IF ( _M < 2, 0, _M )

 

Then create a table

 

Table = SUMMARIZE(t,t[Event],t[date],t[time],"TimeDifference",[TimeDifference])

 

and create a new measure in the table to get the distinct count

 

FinalCount = DISTINCTCOUNT('Table'[TimeDifference])

 

 

SachinNandanwar_1-1728605047935.png

 



Regards,
Sachin
Check out my Blog

View solution in original post

13 REPLIES 13
SachinNandanwar
Impactful Individual
Impactful Individual

I assume you mean that if the diffrence between the two consecutive times for an event is less than 2 minutes then it should be considered as single record

SachinNandanwar_0-1728604620471.png

For example difference between 00:00 and 00:42 is less than 2 minutes and so is the difference between 00:42 and 01:58 less than 2 minutes , so both should be considered as a one count.difference between 01:58 and 07:00 is greater than 2 so it should be considered one new count.

 

Create a measure. t is the table name

 

TimeDifference =
VAR _M =
    DATEDIFF (
        CALCULATE (
            MAX ( t[time] ),
            OFFSET (
                -1,
                ( ALL ( t ) ),
                ORDERBY ( t[date].[Date], DESC ),
                ,
                PARTITIONBY ( t[Event] ),
                MATCHBY ( t[date].[Date], t[time] )
            )
        ),
        MAX( t[time] ),
        MINUTE
    )
RETURN
    IF ( _M < 2, 0, _M )

 

Then create a table

 

Table = SUMMARIZE(t,t[Event],t[date],t[time],"TimeDifference",[TimeDifference])

 

and create a new measure in the table to get the distinct count

 

FinalCount = DISTINCTCOUNT('Table'[TimeDifference])

 

 

SachinNandanwar_1-1728605047935.png

 



Regards,
Sachin
Check out my Blog

Hello @SachinNandanwar 

 

Thank you for your answer.

I have tried to use your dax, but I get an error:

 

Jeffrey_VC_0-1728630072912.png

 

What is the error that you get ?



Regards,
Sachin
Check out my Blog

@SachinNandanwar

In the dax i get an error on:

ORDERBY ( 'Logdata SQL'[Datum].[datum],DESC ), and
MATCHBY ( 'Logdata SQL'[Datum].[datum], 'Logdata SQL'[Tijd])
 

 

Jeffrey_VC_0-1728651215869.png

 

I want to know the error message. After you create the measure it should give you an error message.



Regards,
Sachin
Check out my Blog

@SachinNandanwar 

The column reference to Date in the SQL Logdata table cannot be used for the date variant because it does not contain a variant.

I suspect it has to do with the data type of the column. Can you share the PBI file with some mock data ?



Regards,
Sachin
Check out my Blog

How can i share a PBI?

put it on any file sharing app.



Regards,
Sachin
Check out my Blog

SachinNandanwar_0-1728656658440.png

Also check this post.It has some great insights.

https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Column-reference-to-Date-in-table-ca...



Regards,
Sachin
Check out my Blog
ThxAlot
Super User
Super User

Easy enough,

ThxAlot_0-1728597004762.png

ThxAlot_1-1728597042186.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



ValtteriN
Community Champion
Community Champion

Hi,

Here is one way to do this with dax:

Count of events =


VAR _vtable1 =
        ADDCOLUMNS(
        'Table (43)',
        "DateTime", 'Table (43)'[date] + 'Table (43)'[time],
        "Earlier",
        var _time = [time]
        var _date = [date]
        var _event = [Event]
        RETURN
        CALCULATE(
            MAX('Table (43)'[date]) + MAX('Table (43)'[time]),
            FILTER(
                'Table (43)',
                'Table (43)'[Event] = _event &&
                ('Table (43)'[date] + 'Table (43)'[time]) < (_date + _time)
            )
        )
    )

VAR _vtable2 =
    ADDCOLUMNS(
        _vtable1,
        "lessthan2",
        SWITCH(
            TRUE(),
            ISBLANK([Earlier]), 1,  -- If there is no earlier event, treat it as a new event
            DATEDIFF([Earlier], [DateTime], MINUTE) < 2, 0,  -- If less than 2 minutes, treat it as the same event
            1  -- Otherwise, treat it as a new event
        )
    )

RETURN
SUMX(_vtable2, [lessthan2])

ValtteriN_0-1728589676580.png

You can visualize this in dax query view:

ValtteriN_1-1728589727701.png

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

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.