cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Alanoudfahad
Frequent Visitor

Calculating time duration

Hi!

 

I have a data for employees where they check in and check out everytime they enter the building. I want to calculate the time duration per day (I have a date slicer)

 

2.png

 

For instance here I want to subtract 5:55pm from 7:59am to get 9hrs 56min total hours.

 

I appreciate any help!

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, @Alanoudfahad 

Please check the below picture and the measure.

 

Picture4.png

 

Result =
VAR currentid =
MAX ( Data[ID] )
VAR currentdate =
MAX ( Data[Date] )
VAR minintime =
CALCULATE (
MIN ( Data[Time] ),
FILTER (
ALL ( Data ),
Data[ID] = currentid
&& Data[Date] = currentdate
&& Data[In/Out] = "In"
)
)
VAR maxouttime =
CALCULATE (
MAX ( Data[Time] ),
FILTER (
ALL ( Data ),
Data[ID] = currentid
&& Data[Date] = currentdate
&& Data[In/Out] = "Out"
)
)
RETURN
maxouttime - minintime
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

Best regards, JiHwan Kim

Linkedin: https://www.linkedin.com/in/jihwankim1975/

Twitter: https://twitter.com/Jihwan_JHKIM

View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi, @Alanoudfahad 

Please check the below picture and the measure.

 

Picture4.png

 

Result =
VAR currentid =
MAX ( Data[ID] )
VAR currentdate =
MAX ( Data[Date] )
VAR minintime =
CALCULATE (
MIN ( Data[Time] ),
FILTER (
ALL ( Data ),
Data[ID] = currentid
&& Data[Date] = currentdate
&& Data[In/Out] = "In"
)
)
VAR maxouttime =
CALCULATE (
MAX ( Data[Time] ),
FILTER (
ALL ( Data ),
Data[ID] = currentid
&& Data[Date] = currentdate
&& Data[In/Out] = "Out"
)
)
RETURN
maxouttime - minintime
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

Best regards, JiHwan Kim

Linkedin: https://www.linkedin.com/in/jihwankim1975/

Twitter: https://twitter.com/Jihwan_JHKIM

View solution in original post

Works wonderfully, thanks!

amitchandak
Super User
Super User

@Alanoudfahad , Try a new column like

 

var _max = if([In/out] = "Out" , max(filter(Table, [Date] =earlier([Date]) && [Id] =earlier([ID]) && [In/out] ="In" && [Time] <earlier([Time])),[Time]), blank())
return
if([In/out] = "Out" && not(isblank(_max)) , datediff(_max, [Time], minute))/60

 

 

Ideally in place of time you should use date time column

 

date time  =[date]+[time]

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!

Hi @amitchandak It gives me this error "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value." even after crreating date/time column

Helpful resources

Announcements
Power BI December 2021 Update_carousel 768x460.jpg

Check it Out!

Click here to read more about the December 2021 Updates!

User Group Leader Meeting January 768x460.png

Calling all User Group Leaders!

Don't miss the User Group Leader meetings on January, 24th & 25th, 2022.

Jan 2022 Dev Camp 768x460 copy.png

Power BI Dev Camp- January 27th, 2022

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors