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

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.

Reply
sureshsonti
Helper II
Helper II

Calculate Hours and Minutes and Sum Hours and Minutes

My data has Employee Code, Punch In, Punch Out. I am trying to find the number of hours worked (Hours: Minutes) and calculate total hours worked in Hours:Minutes

 

 

 

 

Thanks

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

Check out this article:

 

https://community.powerbi.com/t5/Community-Blog/Aggregating-Duration-Time/ba-p/22486

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

10 REPLIES 10
sureshsonti
Helper II
Helper II

I still get the sum of all hours worked by the employees.

ChrisMendoza
Resident Rockstar
Resident Rockstar

@sureshsonti,

 

I think you'll be better off sum-ing in tenths of an hour rather than trying to keep with a time format 00:00. How would the computer visualize a duration greater than 24 hours in time format? Since datetime is a visual format that only represents a decimal number i suggest you change to decimal format as:

SimpleDuration (hrs in tenths) = 
VAR SimpleDurationMins = 
    DATEDIFF('Table'[Punch In],'Table'[Punch Out],MINUTE)
    
RETURN DIVIDE(SimpleDurationMins,60) 





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



MitchM
Resolver II
Resolver II

Another option is to create a new column:

 

Time Worked = Sheet1[Punch Out] - Sheet1[Punch In] (i was using excel)

 

This will calculate the time worked. Make sure you change the data type to:

Modeling --> Date Type --> Date/Time --> 13:30(H:mm) 

 

You can then create a measure to calculate the sum:

Total Time worked = CALCULATE(SUM(Sheet1[Time Worked]))

Results below

Picture1.png

Total Hours is only showing 7:43 hours. I got the first part which is hours worked by each employee. But not able to get the total hours worked by all employees. Thanks for your help

I am not sure if this will be the type of solution you want, but here is a way to do it:

 

1. Create 2 Columns:

 

    (set data type to time) 

Time Worked = (Sheet1[Punch Out] - Sheet1[Punch In])

 

     (set data type to number) 

Minutes Worked = 
VAR TimeWorked = Sheet1[Punch Out] - Sheet1[Punch In]
VAR Hours = HOUR(TimeWorked)*60
VAR Minutes = MINUTE(TimeWorked)
RETURN
(Hours + Minutes)

 

 

2. Create 2 measures

 This will allow you to sum up hours for each employee. The total will not work, so turn it off for the chart to avoid confusion.

Total Time worked = CALCULATE(SUM(Sheet1[Time Worked]))

This will create a string that displays the sum of all hours worked. Use a card to display the value.

Sum of Time Worked = 
VAR HoursWorked = ROUNDDOWN(SUM(Sheet1[Minutes Worked])/60,0)
VAR MinutesWorked = SUM(Sheet1[Minutes Worked])-(HoursWorked*60)
RETURN
HoursWorked & ":" & MinutesWorked

Here is the final result

example2.jpg

Hi,

 

I would like to know, in the first column, why are we multiplying with the 60?.

 

Then in the measure, why are we multiplying by 60 in the VAR for hours and then in the minutes VAR we decide by 60

 

THANKS

Anonymous
Not applicable

Test (Hour:Minute Diff) = FLOOR(24 * ('Table'[LastDate] - 'Table'[FirstDate]),1)
&":"&
FLOOR(24 * 60 * ('Table'[LastDate] - 'Table'[FirstDate]),1) - FLOOR(24 * ('Table'[LastDate] - 'Table'[FirstDate]),1) * 60

Greg_Deckler
Super User
Super User

Check out this article:

 

https://community.powerbi.com/t5/Community-Blog/Aggregating-Duration-Time/ba-p/22486

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

If you want to do the final sum as a PowerBI visual, but do the analysis in Power Query, you can add a column in Power Query that subtracts the Punch In Time from the Punch Out Time (Below). If you want to sum within Power Query you can then use the Transform--> Group By function Group by Employee ID and Have the new column Sum the Time Worked Column (also below)

Example.jpgexample2.png

I am using a direct query report. Power Query not letting me create a custom column. I used this DAX formula and I get all 00:00 values. Data Type is Time

 

TODAY_HOURS = DATEDIFF('tick'[drvt.punch_in_time_1], 'tick'[drvt.punch_out_time_1],HOUR)

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.