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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
Solution Specialist
Solution Specialist

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
Disclaimer : My solutions are my own and are not AI generated

View solution in original post

13 REPLIES 13
SachinNandanwar
Solution Specialist
Solution Specialist

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
Disclaimer : My solutions are my own and are not AI generated

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
Disclaimer : My solutions are my own and are not AI generated

@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
Disclaimer : My solutions are my own and are not AI generated

@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
Disclaimer : My solutions are my own and are not AI generated

How can i share a PBI?

put it on any file sharing app.



Regards,
Sachin
Check out my Blog
Disclaimer : My solutions are my own and are not AI generated

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
Disclaimer : My solutions are my own and are not AI generated
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.LeanAndPractise(Everyday)


)



ValtteriN
Super User
Super User

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.