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.
Hi,
I'm new to DAX and I need your help.
I have the following table:
device_id | time | display_time | Calculate |
AA | 9/10/2020 01:02 | 9/10/2020 01:02 | 00:01:00 |
AA | 9/10/2020 13:05 | 9/10/2020 01:05 | 00:03:34 |
AA | 9/10/2020 01:06 | 9/10/2020 01:06 | 00:00:41 |
AA | 9/10/2020 13:09 | 9/10/2020 13:09 | 12:03:34 |
BB | 9/11/2020 00:28 | 9/11/2020 00:28 | 00:01:00 |
BB | 9/11/2020 12:30 | 9/11/2020 12:30 | 12:02:16 |
BB | 9/11/2020 00:32 | 9/11/2020 12:32 | 00:02:37 |
BB | 9/11/2020 12:35 | 9/11/2020 12:35 | 00:02:15 |
CC | 9/10/2020 09:59 | 9/10/2020 09:59 | 00:01:00 |
CC | 9/10/2020 22:01 | 9/10/2020 22:01 | 12:01:30 |
CC | 9/10/2020 10:03 | 9/10/2020 22:03 | 00:02:14 |
CC | 9/10/2020 22:05 | 9/10/2020 22:05 | 00:02:07 |
I want to calculate de difference in seconds between two consecutive display times for a device( like in the Calculate column) and if it's possible for the first display_time for a device to insert a value e.g. 60 seconds.
I've tried adding first an index column and after that using the DATEDIFF formula but I can't properly sort the table and get right the consecutive values for display_time when I used an Index column.
Kind regards,
Madalin
Solved! Go to Solution.
Hi, @madalin
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a measure as below.
Seconds =
var tab =
ADDCOLUMNS(
ALL('Table'),
"Result",
var _lastdatetime =
CALCULATE(
MAX('Table'[Display_Time]),
FILTER(
ALL('Table'),
'Table'[Device_ID]=EARLIER('Table'[Device_ID])&&
'Table'[Display_Time]<EARLIER('Table'[Display_Time])
)
)
return
IF(
ISBLANK(_lastdatetime),
60,
DATEDIFF(_lastdatetime,[Display_Time],SECOND)
)
)
return
SUMX(
ADDCOLUMNS(
'Table',
"Re",
SUMX(
FILTER(
tab,
[Device_ID]=EARLIER('Table'[Device_ID])&&
[Display_Time]=EARLIER('Table'[Display_Time])
),
[Result]
)
),
[Re]
)
Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi, @madalin
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a measure as below.
Seconds =
var tab =
ADDCOLUMNS(
ALL('Table'),
"Result",
var _lastdatetime =
CALCULATE(
MAX('Table'[Display_Time]),
FILTER(
ALL('Table'),
'Table'[Device_ID]=EARLIER('Table'[Device_ID])&&
'Table'[Display_Time]<EARLIER('Table'[Display_Time])
)
)
return
IF(
ISBLANK(_lastdatetime),
60,
DATEDIFF(_lastdatetime,[Display_Time],SECOND)
)
)
return
SUMX(
ADDCOLUMNS(
'Table',
"Re",
SUMX(
FILTER(
tab,
[Device_ID]=EARLIER('Table'[Device_ID])&&
[Display_Time]=EARLIER('Table'[Display_Time])
),
[Result]
)
),
[Re]
)
Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @madalin
Try this for your new column:
New Column =
VAR current_ = Table1[display_time]
VAR previous_ =
CALCULATE (
MAX ( Table1[display_time] ),
Table1[display_time] < EARLIER ( Table1[display_time] ),
ALLEXCEPT ( Table1, Table1[device_id] )
)
RETURN
IF ( ISBLANK ( previous_ ), 1 / 24, previous_ - current_ )
This will give you the result in days (decimal number). You can change the type to time if you want to show it in the format you used in your example but take into account that that only will show up to 23:59:59
Please mark the question solved when done and consider giving kudos if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.