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
rocky09
Solution Sage
Solution Sage

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
v-shex-msft
Community Support
Community Support

 

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
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

34 REPLIES 34
Anonymous
Not applicable

I tried this solution for the data in SQL server database. I am also getting the same error:

 

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

 

Can some one please help me to replve this 

KKMUrthy

Working Hours.jpg

Please check your Data Type for the Date Column. It should be Date format.

Anonymous
Not applicable

I ensure the data type as Datetime in query editor. I then changed the datetime format to the one shown in the screenshot

Anonymous
Not applicable

I chose  the data type as Datetime for the date fields  in query editor. I then changed the datetime format to the one shown in the screenshot in the data tab. I still get the same error

any thoughts please?

 

replace the comma with a semicolon

 

Cattura.png

Anonymous
Not applicable

Hi

Thank you for spending time on this issue.

I am getting the syntax error: The syntax for ';' is incorrect.

for using ; in place of ,

I am using the power BI Desktop( Aug 2018)

Any further help is greatly appreciated

Did you checked your data type of Date Column in query editor?

@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

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
If this post helps, please consider accept as solution to help other members find it more quickly.

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.

 

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

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

I got the same error, could you solve it ?

rocky09
Solution Sage
Solution Sage

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.