cancel
Showing results for
Did you mean:
Impactful Individual

## Calculating Working hours

I have this following Data, I am trying to find a way to calculating  Working hours in betwen dates excluding Weekends.

Works hours are between: Morning 9:00 AM to Evening 6:00 PM and Saturday and Sunday are holidays.

 TicketID ACTIVITY_DATE LASTMODIFIEDDATE 86256 28-12-2017 03:11 28-12-2017 15:11 89890 15-08-2017 20:23 15-08-2017 21:12 111611 04-10-2017 21:30 10-10-2017 13:00 111511 04-10-2017 02:30 10-10-2017 13:00 111542 04-10-2017 02:41 10-10-2017 13:00 111485 04-10-2017 02:41 10-10-2017 13:00 211411 06-10-2017 14:25 10-10-2017 13:00 150895 10-11-2017 12:32 18-01-2018 13:29 152996 08-08-2017 03:40 09-08-2017 11:00

any help?

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support

## Re: Calculating Working hours

HI @rocky09,

You can try to use below calculated column formula to calculate valid working hour:

```Work Hour =
VAR filtered =
FILTER (
CROSSJOIN (
CALENDAR ( [ACTIVITY_DATE], [LASTMODIFIEDDATE] ),
SELECTCOLUMNS ( GENERATESERIES ( 9, 18 ), "Hour", [Value] )
),
"Day of week", WEEKDAY ( [Date], 2 )
),
[Day of week] < 6
&& [TicketID] = EARLIER ( Table1[TicketID] )
)
VAR hourcount =
COUNTROWS (
FILTER (
filtered,
(
[Date] >= DATEVALUE ( [ACTIVITY_DATE] )
&& [Hour]
> HOUR ( [ACTIVITY_DATE] ) + 1
)
&& (
[Date] <= DATEVALUE ( [LASTMODIFIEDDATE] )
&& [Hour]
> HOUR ( [LASTMODIFIEDDATE] ) - 1
)
)
)
VAR remained =
DATEDIFF (
TIMEVALUE ( [ACTIVITY_DATE] ),
TIME ( HOUR ( [ACTIVITY_DATE] ) + 1, 0, 0 ),
MINUTE
)
+ DATEDIFF (
TIME ( HOUR ( [LASTMODIFIEDDATE] ) - 1, 0, 0 ),
TIMEVALUE ( [LASTMODIFIEDDATE] ),
MINUTE
)
RETURN
IF ( hourcount <> BLANK (), (hourcount*60 + remained)/60, 0 )
```

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin Sheng
If this post helps, please consider Accept it as the solution to help the other members find it more quickly
Learning resources: Power BI
19 REPLIES 19
Impactful Individual

## Re: Calculating Working hours

Community Support

## Re: Calculating Working hours

HI @rocky09,

You can try to use below calculated column formula to calculate valid working hour:

```Work Hour =
VAR filtered =
FILTER (
CROSSJOIN (
CALENDAR ( [ACTIVITY_DATE], [LASTMODIFIEDDATE] ),
SELECTCOLUMNS ( GENERATESERIES ( 9, 18 ), "Hour", [Value] )
),
"Day of week", WEEKDAY ( [Date], 2 )
),
[Day of week] < 6
&& [TicketID] = EARLIER ( Table1[TicketID] )
)
VAR hourcount =
COUNTROWS (
FILTER (
filtered,
(
[Date] >= DATEVALUE ( [ACTIVITY_DATE] )
&& [Hour]
> HOUR ( [ACTIVITY_DATE] ) + 1
)
&& (
[Date] <= DATEVALUE ( [LASTMODIFIEDDATE] )
&& [Hour]
> HOUR ( [LASTMODIFIEDDATE] ) - 1
)
)
)
VAR remained =
DATEDIFF (
TIMEVALUE ( [ACTIVITY_DATE] ),
TIME ( HOUR ( [ACTIVITY_DATE] ) + 1, 0, 0 ),
MINUTE
)
+ DATEDIFF (
TIME ( HOUR ( [LASTMODIFIEDDATE] ) - 1, 0, 0 ),
TIMEVALUE ( [LASTMODIFIEDDATE] ),
MINUTE
)
RETURN
IF ( hourcount <> BLANK (), (hourcount*60 + remained)/60, 0 )
```

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin Sheng
If this post helps, please consider Accept it as the solution to help the other members find it more quickly
Learning resources: Power BI
Impactful Individual

## Re: Calculating Working hours

@v-shex-msft

Thank you so much sir.

I am getting the below error.

The Start date in Calendar function can not be later thanthe end date.

I guess, the first column may have greater date than modified date. Is there a way to dealth it

Impactful Individual

## Re: Calculating Working hours

actually, i found the reason,

My Date is changing once loading to data model. Actually, the time is early monring. Is it possible to prevent?

example: from 01-12-2017 01:50 to 01-12-2017 PM 01:50

Community Support

## Re: Calculating Working hours

Hi @rocky09,

>>I guess, the first column may have greater date than modified date. Is there a way to dealth it

It means your table contains records which start date greater than end date. Datediff function not support calculated with records who have greater startdate(compare with end date).

You can add some conditions to ignore calculation when 'start date' greater than 'end date'.

Notice: DATEDIFF(start date, end date, unit)

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin Sheng
If this post helps, please consider Accept it as the solution to help the other members find it more quickly
Learning resources: Power BI
Helper III

## Re: Calculating Working hours

Hi,

I tried the formula above and I get the error message "The start date or end date in Calendar function can not be Blank value". Any way to resolve this?

Thanks

Helper II

## Re: Calculating Working hours

Hi @v-shex-msft I got a question for you:

I have 2 issues when I use your solution.

1. Error because those fields in calendar can not be blank.

2. Error because sometimes the lastdate is previous to firstdate.

Can you PLEASE give me some solution? I know those are conditions I have to add in the same code you put. But I don't know where to make it work.

Thanks man! I hope you to be fine.

Frequent Visitor

## Re: Calculating Working hours

I got the same error, could you solve it ?

Frequent Visitor

## Re: Calculating Working hours

I am getting the following error. Any idea how to fix it?

"An argument of function 'TIME' has the wrong data type or the result is too large or too small."

Announcements

#### New Ranks Launched March 24th!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power BI Community!

#### ‘Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

#### Arun 'Triple A' Event Video, Q&A, and Slides

Missed the Arun 'Triple A' event or want to revisit it? We've got you covered! Check out the video, Q&A, and slides now.

#### Join THE global Power Platform event series.

Attend for two days of expert-led learning and innovation on topics like AI and Analytics, powered by Dynamic Communities.

#### Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors