March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe 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
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:
Event | date | time |
A | 1-1-2024 | 8:00:00 |
A | 1-1-2024 | 8:00:42 |
B | 1-1-2024 | 8:01:32 |
A | 1-1-2024 | 8:01:58 |
B | 1-1-2024 | 8:02:00 |
A | 1-1-2024 | 8:07:00 |
B | 1-1-2024 | 8:08:52 |
A | 1-1-2024 | 8:21:00 |
B | 1-1-2024 | 8:35:00 |
B | 1-1-2024 | 8:35:58 |
Needed results:
Event A: 3
Event B; 3
Thnx for the support.
Solved! Go to Solution.
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
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])
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
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])
What is the error that you get ?
In the dax i get an error on:
I want to know the error message. After you create the measure it should give you an error message.
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 ?
How can i share a PBI?
put it on any file sharing app.
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...
Hi,
Here is one way to do this with dax:
You can visualize this in dax query view:
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/
Proud to be a Super User!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
22 | |
19 | |
16 | |
9 | |
5 |
User | Count |
---|---|
37 | |
29 | |
16 | |
14 | |
12 |