cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
rocky09 Impactful Individual
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.

 

TicketIDACTIVITY_DATE       LASTMODIFIEDDATE    
8625628-12-2017 03:1128-12-2017 15:11
8989015-08-2017 20:2315-08-2017 21:12
11161104-10-2017 21:3010-10-2017 13:00
11151104-10-2017 02:3010-10-2017 13:00
11154204-10-2017 02:4110-10-2017 13:00
11148504-10-2017 02:4110-10-2017 13:00
21141106-10-2017 14:2510-10-2017 13:00
15089510-11-2017 12:3218-01-2018 13:29
15299608-08-2017 03:4009-08-2017 11:00

 

any help?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support
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 (
        ADDCOLUMNS (
            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 )

 2.PNG

 

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

View solution in original post

19 REPLIES 19
rocky09 Impactful Individual
Impactful Individual

Re: Calculating Working hours

Community Support
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 (
        ADDCOLUMNS (
            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 )

 2.PNG

 

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

View solution in original post

rocky09 Impactful Individual
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

rocky09 Impactful Individual
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
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
mpalha04 Helper III
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

omarevp Helper II
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.

 

rivastoday
Frequent Visitor

Re: Calculating Working hours

I got the same error, could you solve it ?

mimyer
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."

Helpful resources

Announcements
New Ranks Launched March 24th!

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!

‘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

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 Microsoft Power Platform event series.

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

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