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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
MaJar
Frequent Visitor

Calculate Working hours between 2 dates in a specified interval

Hi All,

Knowing that I have 4 columns in my dataset, I want to create a custom function in power query that will have these 4 attributes:

- Start date as DateTime (Example: 05/05/2023 05:45:54 AM)

- End date as DateTime (Example: 07/05/2023 02:56:34 PM)

-Work Start as Time (Example: 08:30:00 AM)

-Work End as Time (Example: 05:30:00 PM)

 

MaJar_0-1701772252064.png

This function will invoke a custom column that will calculate the total working hours between the start date and end date in the interval of the work start and work end.

 

Thank you in advance for the help.

2 REPLIES 2
Anonymous
Not applicable

Hi @MaJar,

Here is the custom function to calculate total second from the start date, end date based on specific work time ranges:

let
    WorkDuration = (startDate as datetime, enddate as datetime, workstart as time, workend as time) =>
        let
            _start = Date.From(startDate) & List.Max({Time.From(startDate), workstart}),
            _end = Date.From(enddate) & List.Min({Time.From(enddate), workend}),
            IsCrossDays = if Date.From(startDate) = Date.From(enddate) then true else false,
            result =
                if IsCrossDays = false then
                    Duration.TotalSeconds(_end - _start)
                else
                    Duration.TotalSeconds(Date.From(startDate) & workend - _start) + 
                    Number.Round(Duration.TotalDays(_end - _start)-1,0) * Duration.TotalSeconds(workend - workstart) + 
                    Duration.TotalSeconds(_end - Date.From(enddate) & workstart)
        in
            result
in
    WorkDuration

You can add a new custom column to invoke this custom function with current row field values.

1.png

Regards,

Xiaoxin Sheng

lbendlin
Super User
Super User

This has been covered multiple times recently.

 

You need to bring your own definition of 

- workdays

- working hours

- holidays and weekends

- what should happen when events start or end outside of working hours.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors