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 all,
I would like to calculate the elapsed time in office by whole day for each worker ID.
How can i calculate this without the time spended at outside
thnx
Solved! Go to Solution.
@daglaroglu Please try this as a "New Column" which will give the each breaktime difference between OUT and IN time
BreakDuration = VAR _InTime = CALCULATE(MAX(Test95BreakTime[Time]),FILTER(ALL(Test95BreakTime),Test95BreakTime[Time] < EARLIER(Test95BreakTime[Time]) && Test95BreakTime[WorkerID] = EARLIER(Test95BreakTime[WorkerID]) && Test95BreakTime[Direction] <> EARLIER(Test95BreakTime[Direction]) && Test95BreakTime[Direction] = "OUT")) RETURN DATEDIFF(TIME(HOUR(_InTime),MINUTE(_InTime),SECOND(_InTime)),Test95BreakTime[Time],MINUTE)
Then, you can create a measure to get the total worked/logged time excluding the break time using the below as "Measure"
Test95TotalLoggedTime = VAR _BreakTime = SUM(Test95BreakTime[BreakDuration]) VAR _FirstIn = CALCULATE(MIN(Test95BreakTime[Time]),Test95BreakTime[Direction]="IN") VAR _LastOut = CALCULATE(MAX(Test95BreakTime[Time]),Test95BreakTime[Direction]="OUT") RETURN DATEDIFF(_FirstIn,_LastOut,MINUTE)-_BreakTime
Proud to be a PBI Community Champion
@daglaroglu Please try this as a "New Column" which will give the each breaktime difference between OUT and IN time
BreakDuration = VAR _InTime = CALCULATE(MAX(Test95BreakTime[Time]),FILTER(ALL(Test95BreakTime),Test95BreakTime[Time] < EARLIER(Test95BreakTime[Time]) && Test95BreakTime[WorkerID] = EARLIER(Test95BreakTime[WorkerID]) && Test95BreakTime[Direction] <> EARLIER(Test95BreakTime[Direction]) && Test95BreakTime[Direction] = "OUT")) RETURN DATEDIFF(TIME(HOUR(_InTime),MINUTE(_InTime),SECOND(_InTime)),Test95BreakTime[Time],MINUTE)
Then, you can create a measure to get the total worked/logged time excluding the break time using the below as "Measure"
Test95TotalLoggedTime = VAR _BreakTime = SUM(Test95BreakTime[BreakDuration]) VAR _FirstIn = CALCULATE(MIN(Test95BreakTime[Time]),Test95BreakTime[Direction]="IN") VAR _LastOut = CALCULATE(MAX(Test95BreakTime[Time]),Test95BreakTime[Direction]="OUT") RETURN DATEDIFF(_FirstIn,_LastOut,MINUTE)-_BreakTime
Proud to be a PBI Community Champion
Hi,
While I was trying to implement above logic getting below error.
"The syntax for ')' is incorrect. (DAX(VAR _InTime = CALCULATE(MAX('Raw Data'[Time]),FILTER(ALL('Raw Data'[Time]),'Raw Data'[Time] < EARLIER('Raw Data'[Time]) && 'Raw Data'[Description #2] = EARLIER('Raw Data'[Description #2]) && 'Raw Data'[Description #1] <> EARLIER('Raw Data'[Description #1]) && CONTAINS('Raw Data','Raw Data'[Description #1]," EXIT")) "
Could you please help me out here.
Thanks
Siva
Works great! Pattem, I realy appreciate your support
Hi @PattemManohar,
I have a bug. How can i add a filter for multiple days? In some cases moethod crqashes and it starts to calculate from other days recors. I think it has a relation with exits and entrances after-midnight.
Thanks
@daglaroglu Please post the sample data in copiable format to replicate the issue that you are facing.
Proud to be a PBI Community Champion
Please find the sample data below. Thnx
Worker ID | Direction | Date | Time | BreakDuration |
1 | IN | 1.08.2018 | 08:29:24 | 290 |
1 | OUT | 1.08.2018 | 12:34:02 | |
1 | IN | 1.08.2018 | 13:04:02 | 25 |
1 | OUT | 1.08.2018 | 17:52:38 | |
1 | IN | 2.08.2018 | 08:21:21 | 282 |
1 | OUT | 2.08.2018 | 12:09:40 | |
1 | IN | 2.08.2018 | 12:17:23 | 6 |
1 | OUT | 2.08.2018 | 12:33:44 | |
1 | IN | 2.08.2018 | 12:36:15 | 1 |
1 | OUT | 2.08.2018 | 19:37:12 | |
1 | IN | 3.08.2018 | 09:48:28 | 60 |
1 | OUT | 3.08.2018 | 13:27:17 | |
1 | IN | 3.08.2018 | 13:51:52 | 21 |
1 | OUT | 3.08.2018 | 14:51:06 | |
1 | IN | 3.08.2018 | 15:15:09 | 24 |
1 | OUT | 3.08.2018 | 17:00:56 | |
1 | IN | 3.08.2018 | 17:03:41 | 3 |
1 | OUT | 3.08.2018 | 20:25:34 | |
1 | IN | 9.08.2018 | 09:07:33 | 19 |
1 | OUT | 9.08.2018 | 13:11:37 | |
1 | IN | 9.08.2018 | 13:29:49 | 2 |
1 | OUT | 9.08.2018 | 16:46:00 | |
1 | IN | 9.08.2018 | 17:00:00 | 2 |
1 | OUT | 9.08.2018 | 17:56:45 | |
1 | IN | 9.08.2018 | 18:12:54 | 16 |
1 | OUT | 10.08.2018 | 03:39:04 | |
1 | IN | 10.08.2018 | 08:43:26 | 304 |
1 | OUT | 10.08.2018 | 13:22:01 | |
1 | IN | 10.08.2018 | 14:33:50 | 63 |
1 | OUT | 10.08.2018 | 18:12:56 | |
1 | IN | 13.08.2018 | 08:59:25 | 11 |
1 | OUT | 13.08.2018 | 11:30:52 | |
1 | IN | 13.08.2018 | 11:53:49 | 23 |
1 | OUT | 13.08.2018 | 13:30:24 | |
1 | IN | 13.08.2018 | 14:04:43 | 34 |
1 | OUT | 13.08.2018 | 17:56:58 | |
1 | IN | 14.08.2018 | 08:33:53 | 294 |
1 | OUT | 14.08.2018 | 08:48:38 | |
1 | IN | 14.08.2018 | 08:57:35 | 9 |
1 | OUT | 14.08.2018 | 10:27:31 | |
1 | IN | 14.08.2018 | 10:35:07 | 2 |
1 | OUT | 14.08.2018 | 12:11:22 | |
1 | IN | 14.08.2018 | 12:42:01 | 3 |
1 | OUT | 14.08.2018 | 17:17:12 | |
1 | IN | 14.08.2018 | 18:52:22 | 40 |
1 | OUT | 15.08.2018 | 00:49:41 | |
1 | IN | 15.08.2018 | 11:42:49 | 12 |
1 | OUT | 15.08.2018 | 12:39:35 | |
1 | IN | 15.08.2018 | 13:34:55 | 4 |
1 | OUT | 15.08.2018 | 17:56:34 | |
1 | IN | 16.08.2018 | 09:37:55 | 49 |
1 | OUT | 16.08.2018 | 10:33:37 | |
1 | IN | 16.08.2018 | 10:50:11 | 17 |
1 | OUT | 16.08.2018 | 12:35:54 | |
1 | IN | 16.08.2018 | 13:33:13 | 3 |
1 | OUT | 16.08.2018 | 16:58:34 | |
1 | IN | 16.08.2018 | 17:12:07 | 12 |
1 | OUT | 16.08.2018 | 17:41:25 | |
1 | IN | 17.08.2018 | 08:26:00 | 287 |
1 | OUT | 17.08.2018 | 13:10:10 | |
1 | IN | 17.08.2018 | 13:29:22 | 2 |
1 | OUT | 17.08.2018 | 17:26:58 | |
2 | IN | 1.08.2018 | 09:46:43 | 20 |
2 | OUT | 1.08.2018 | 09:54:09 | |
2 | IN | 1.08.2018 | 10:07:59 | 13 |
2 | OUT | 1.08.2018 | 11:29:28 | |
2 | IN | 1.08.2018 | 11:41:40 | 1 |
2 | OUT | 1.08.2018 | 12:01:26 | |
2 | IN | 1.08.2018 | 13:04:44 | 59 |
2 | OUT | 1.08.2018 | 15:13:26 | |
2 | IN | 1.08.2018 | 15:26:09 | 6 |
2 | OUT | 1.08.2018 | 16:42:35 | |
2 | IN | 1.08.2018 | 17:03:51 | 2 |
2 | OUT | 1.08.2018 | 18:08:12 | |
2 | IN | 1.08.2018 | 18:36:38 | 4 |
2 | OUT | 1.08.2018 | 19:49:22 | |
2 | IN | 2.08.2018 | 09:15:25 | 542 |
2 | OUT | 2.08.2018 | 09:26:01 | |
2 | IN | 3.08.2018 | 10:30:16 | 36 |
2 | OUT | 3.08.2018 | 10:54:56 | |
2 | IN | 3.08.2018 | 11:07:00 | 13 |
2 | OUT | 3.08.2018 | 11:59:24 | |
2 | IN | 3.08.2018 | 13:35:45 | 90 |
2 | OUT | 3.08.2018 | 15:02:07 | |
2 | IN | 3.08.2018 | 15:23:19 | 3 |
2 | OUT | 3.08.2018 | 17:34:40 | |
2 | IN | 6.08.2018 | 10:28:26 | 34 |
2 | OUT | 6.08.2018 | 10:45:56 | |
2 | IN | 6.08.2018 | 11:18:10 | 11 |
2 | OUT | 6.08.2018 | 11:56:59 | |
2 | IN | 6.08.2018 | 13:16:56 | 71 |
2 | OUT | 6.08.2018 | 14:58:05 | |
2 | IN | 6.08.2018 | 16:21:58 | 4 |
2 | OUT | 6.08.2018 | 17:58:42 | |
2 | IN | 6.08.2018 | 18:29:38 | 21 |
2 | OUT | 6.08.2018 | 18:32:36 | |
2 | IN | 7.08.2018 | 10:10:44 | 16 |
2 | OUT | 7.08.2018 | 10:47:15 | |
2 | IN | 7.08.2018 | 11:07:20 | 13 |
2 | OUT | 7.08.2018 | 11:55:25 | |
2 | IN | 7.08.2018 | 13:18:24 | 73 |
2 | OUT | 7.08.2018 | 14:59:45 | |
2 | IN | 7.08.2018 | 15:17:31 | 4 |
2 | OUT | 7.08.2018 | 16:17:52 | |
2 | IN | 7.08.2018 | 16:50:34 | 8 |
2 | OUT | 7.08.2018 | 17:29:05 | |
2 | IN | 7.08.2018 | 18:00:15 | 2 |
2 | OUT | 7.08.2018 | 18:32:13 | |
2 | IN | 8.08.2018 | 09:20:28 | 1 |
2 | OUT | 8.08.2018 | 09:26:29 | |
2 | IN | 8.08.2018 | 09:40:58 | 14 |
2 | OUT | 8.08.2018 | 10:33:57 | |
2 | IN | 8.08.2018 | 11:04:06 | 10 |
2 | OUT | 8.08.2018 | 11:40:19 | |
2 | IN | 8.08.2018 | 11:54:32 | 4 |
2 | OUT | 8.08.2018 | 11:58:39 | |
2 | IN | 8.08.2018 | 13:23:38 | 78 |
2 | OUT | 8.08.2018 | 14:34:47 | |
2 | IN | 8.08.2018 | 14:57:30 | 7 |
2 | OUT | 8.08.2018 | 16:00:47 | |
2 | IN | 8.08.2018 | 16:14:26 | 14 |
2 | OUT | 8.08.2018 | 17:41:22 | |
2 | IN | 9.08.2018 | 09:40:54 | 14 |
2 | OUT | 9.08.2018 | 09:52:42 | |
2 | IN | 9.08.2018 | 10:28:16 | 34 |
2 | OUT | 9.08.2018 | 11:50:02 | |
2 | IN | 9.08.2018 | 13:26:02 | 81 |
2 | OUT | 9.08.2018 | 15:01:54 | |
2 | IN | 9.08.2018 | 15:24:08 | 4 |
2 | OUT | 9.08.2018 | 17:01:53 | |
2 | IN | 9.08.2018 | 17:21:35 | 14 |
2 | OUT | 9.08.2018 | 17:47:17 | |
2 | IN | 10.08.2018 | 09:24:51 | 5 |
2 | OUT | 10.08.2018 | 09:47:57 | |
2 | IN | 10.08.2018 | 10:59:48 | 5 |
2 | OUT | 10.08.2018 | 11:56:21 | |
2 | IN | 10.08.2018 | 11:57:01 | 1 |
2 | OUT | 10.08.2018 | 11:57:57 | |
2 | IN | 10.08.2018 | 13:45:16 | 100 |
2 | OUT | 10.08.2018 | 15:07:24 | |
2 | IN | 10.08.2018 | 15:38:59 | 5 |
2 | OUT | 10.08.2018 | 17:07:10 | |
2 | IN | 13.08.2018 | 10:19:12 | 25 |
2 | OUT | 13.08.2018 | 10:54:16 | |
2 | IN | 13.08.2018 | 11:44:37 | 4 |
2 | OUT | 13.08.2018 | 11:58:21 | |
2 | IN | 13.08.2018 | 13:13:04 | 68 |
2 | OUT | 13.08.2018 | 14:50:23 | |
2 | IN | 13.08.2018 | 15:26:01 | 6 |
2 | OUT | 13.08.2018 | 16:30:29 | |
2 | IN | 13.08.2018 | 16:58:41 | 7 |
2 | OUT | 13.08.2018 | 17:47:27 | |
2 | IN | 14.08.2018 | 10:20:50 | 26 |
2 | OUT | 14.08.2018 | 11:07:27 | |
2 | IN | 14.08.2018 | 11:32:24 | 3 |
2 | OUT | 14.08.2018 | 11:57:32 | |
2 | IN | 14.08.2018 | 13:19:09 | 74 |
2 | OUT | 14.08.2018 | 15:20:47 | |
2 | IN | 14.08.2018 | 15:44:25 | 11 |
2 | OUT | 14.08.2018 | 16:51:47 | |
2 | IN | 14.08.2018 | 17:06:59 | 5 |
2 | OUT | 14.08.2018 | 17:56:14 | |
2 | IN | 14.08.2018 | 18:09:34 | 1 |
2 | OUT | 14.08.2018 | 18:48:19 | |
2 | IN | 14.08.2018 | 19:02:33 | 14 |
2 | OUT | 14.08.2018 | 19:12:59 | |
2 | IN | 15.08.2018 | 10:16:43 | 22 |
2 | OUT | 15.08.2018 | 10:54:20 | |
2 | IN | 15.08.2018 | 11:18:25 | 11 |
2 | OUT | 15.08.2018 | 12:05:07 | |
2 | IN | 15.08.2018 | 13:26:13 | 81 |
2 | OUT | 15.08.2018 | 15:08:55 | |
2 | IN | 15.08.2018 | 15:42:21 | 9 |
2 | OUT | 15.08.2018 | 17:28:03 | |
2 | IN | 15.08.2018 | 17:52:32 | 5 |
2 | OUT | 15.08.2018 | 19:28:30 | |
2 | IN | 16.08.2018 | 09:14:14 | 541 |
2 | OUT | 16.08.2018 | 09:19:20 | |
2 | IN | 16.08.2018 | 09:43:55 | 17 |
2 | OUT | 16.08.2018 | 10:37:25 | |
2 | IN | 16.08.2018 | 10:47:39 | 0 |
2 | OUT | 16.08.2018 | 12:01:37 | |
2 | IN | 16.08.2018 | 13:05:40 | 60 |
2 | OUT | 16.08.2018 | 15:33:31 | |
2 | IN | 16.08.2018 | 15:58:05 | 25 |
2 | OUT | 16.08.2018 | 16:14:45 | |
2 | IN | 16.08.2018 | 16:29:42 | 12 |
2 | OUT | 16.08.2018 | 16:42:29 | |
2 | IN | 17.08.2018 | 00:10:52 | |
2 | OUT | 17.08.2018 | 00:13:28 |
@daglaroglu Thanks for providing the sample data.
Here is the new logic to handle mutliple dates,
Add a new column as below (Added an another condition to check the date as well)
BreakDuration = VAR _InTime = CALCULATE(MAX(Test95BreakTime1[Time]),FILTER(ALL(Test95BreakTime1),Test95BreakTime1[Time] < EARLIER(Test95BreakTime1[Time]) && Test95BreakTime1[WorkerID] = EARLIER(Test95BreakTime1[WorkerID]) && Test95BreakTime1[Direction] <> EARLIER(Test95BreakTime1[Direction]) && Test95BreakTime1[Date] = EARLIER(Test95BreakTime1[Date]) && Test95BreakTime1[Direction] = "OUT")) RETURN DATEDIFF(TIME(HOUR(_InTime),MINUTE(_InTime),SECOND(_InTime)),Test95BreakTime1[Time],MINUTE)
Create a measure as below
Test95TotalLoggedTime1 = VAR _BreakTime = SUM(Test95BreakTime1[BreakDuration]) VAR _FirstIn = FORMAT(MIN(Test95BreakTime1[Time]),"hh:mm:ss") VAR _LastOut = FORMAT(MAX(Test95BreakTime1[Time]),"hh:mm:ss") RETURN DATEDIFF(_FirstIn,_LastOut,MINUTE)-_BreakTime
Proud to be a PBI Community Champion
I tried the new formula but "BreakDuration" gave en error : DAX comparison values of type Date with values of type Text.
Consider the VALUE or FORMAT function to convert one of the values.
In my table, "date" column's data type is date. When i changed it's data type to text the formula didnt give an error but also didnt return any result.
@daglaroglu I just copied your sample data and the data types are detected automatically, here is the datatypes I've
WorkedID - Whole Number
Direction - Text
Date - Date
Time - Time
Proud to be a PBI Community Champion
Hi @PattemManohar, it is my fault. Now it works.
i have checked the results and calculation still crashes for spesific days.
Previous date's "afrer-midnight" records are effecting the calculation.
You can analysis below record for example. It starts with an OUT record but actually it is previous date's movement.
It i s a hard scanario to cover %100. Is there any idea to eliminate these OUT records ?
may be we can include next day's first "after- midnight OUT record" to calculation of calculated day? And disregard other records before 06:00 ?
Worker ID | Direction | Date | Time | BreakDuration |
1 | OUT | 10.08.2018 | 03:39:04 | |
1 | IN | 10.08.2018 | 08:43:26 | 304 |
1 | OUT | 10.08.2018 | 13:22:01 | |
1 | IN | 10.08.2018 | 14:33:50 | 71 |
1 | OUT | 10.08.2018 | 18:12:56 | |
Worker ID | Direction | Date | Time | BreakDuration |
1 | OUT | 15.08.2018 | 00:49:41 | |
1 | IN | 15.08.2018 | 11:42:49 | 653 |
1 | OUT | 15.08.2018 | 12:39:35 | |
1 | IN | 15.08.2018 | 13:34:55 | 55 |
1 | OUT | 15.08.2018 | 17:56:34 |
@daglaroglu Please try this...
Add an additional Rank field as below
Rnk = RANKX(FILTER(ALL(Test95BreakTime1),Test95BreakTime1[WorkerID]=EARLIER(Test95BreakTime1[WorkerID]) && Test95BreakTime1[Date]=EARLIER(Test95BreakTime1[Date])),Test95BreakTime1[Time],,ASC,Dense)
Now change the BreakDuration logic as below ( Just added additional Rnk condition)
BreakDuration = VAR _InTime = CALCULATE(MAX(Test95BreakTime1[Time]),FILTER(ALL(Test95BreakTime1),Test95BreakTime1[Time] < EARLIER(Test95BreakTime1[Time]) && Test95BreakTime1[WorkerID] = EARLIER(Test95BreakTime1[WorkerID]) && Test95BreakTime1[Direction] <> EARLIER(Test95BreakTime1[Direction]) && Test95BreakTime1[Date] = EARLIER(Test95BreakTime1[Date]) && Test95BreakTime1[Direction] = "OUT" && Test95BreakTime1[Rnk]<>1)) RETURN DATEDIFF(TIME(HOUR(_InTime),MINUTE(_InTime),SECOND(_InTime)),Test95BreakTime1[Time],MINUTE)
That's it !!! It should exclude the First Out of the day (ideally it should be IN but for some hard working staff who works over night, their first entry will OUT for the day)
Please Note - You need to tweak the measure calculation as well to exclude the first entry is OUT for the day
Proud to be a PBI Community Champion
@PattemManoharsorry for the delayed response. All works fine but I need a last tune.
If the worker exited after the midnight than the calculation crashes for the previous day. How can i change the "Test95TotalLoggedTime" logic to addept request below ?
"If the last movement is (IN) for the current day, assume the last (OUT) was 23:59 and calculates the difference between the last (IN) time and 23:59 as worked time."
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.
User | Count |
---|---|
98 | |
97 | |
81 | |
77 | |
66 |
User | Count |
---|---|
126 | |
105 | |
103 | |
81 | |
72 |