• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Count working days between two dates in Power Query|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Count working days between two dates in Power Query|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryCount working days between two date…
sp_PrintTopic sp_TopicIcon
Count working days between two dates in Power Query
Avatar
Tina Kinnersly
Member
Members
Level 0
Forum Posts: 39
Member Since:
June 1, 2018
sp_UserOfflineSmall Offline
1
March 17, 2021 - 8:33 am
sp_Permalink sp_Print

Hi everyone

I want to find out how many working days (excludes weekends and holidays) between two dates in power query editing. I know there is a formula to use in excel, however I am hoping to have the calculation done behind the scene. Can anyone shed some light on it? the file is attached. 

I am still fairly new to power query, as I find it good to keep the file much smaller.

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1510
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
March 18, 2021 - 3:36 pm
sp_Permalink sp_Print

Hi Tina,

There isn't a native function in PQ to do this but Imke Feldman has written code to calculate this

Date.Networkdays function for Power Query and Power BI – (thebiccountant.com)

In the attached file I've used her code in a function and then called that function to work out the NetworkDays for your list of holiday requests.

The function requires a list of holiday dates so I amended the table you had so that each date is listed on its own row.

If you are interested, DAX does have the functionality to do this too with native DAX functions.

Regards

Phil

Avatar
Tina Kinnersly
Member
Members
Level 0
Forum Posts: 39
Member Since:
June 1, 2018
sp_UserOfflineSmall Offline
3
March 19, 2021 - 10:22 am
sp_Permalink sp_Print

Thank you so much for your help, Phil!

I will have a look the query and function and learn from there. Does it exclude the weekends as well?

What's your suggestion on learning DAX? any liable resources I can start with? 

Thanks again

Tina 

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1510
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
March 19, 2021 - 3:11 pm
sp_Permalink sp_Print

Hi Tina,

Yes it excludes weekends.

Our Power Pivot course covers DAX

https://www.myonlinetraininghu.....vot-course

Regards

Phil

Avatar
Katarzyna Mazur

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
September 1, 2022
sp_UserOfflineSmall Offline
5
September 1, 2022 - 6:53 pm
sp_Permalink sp_Print sp_EditHistory

Hi Everyone,

I recently found this post because I'm looking to achieve something similar but not quite the same.

My aim is to calculate the number of hours that passed between 'Request Start Time' and 'Request End Time'. I want to exclude weekends but I don't need to exclude non working hours during the week (i.e. night hours) or extra holidays.

What would be nice to have, would be an additional calculation for cases when the start date is Sunday or end date is Saturday. Then I would actually like to add this weekend day as a working day so add 24 hours.
Similarly, if start date is Saturday or end date is Sunday, I would like to add 2 working days so 48 hours.
However, I think I will be able to find a workaround for this myself so it is not necessary to solve here.

What is causing me the most problems is the fact that most of the solutions I was able to find either don't give the option to exclude weekends only or are based on calculations of working days of the week than multiply the result by 24 to get the hours count. The issue is that if the start date and end date are on the same day, the result is 0 working days and 0*24 is also 0 which is not true result for hours. I really need the exact hours between these 2 dates.

My Excel Networkdays formula is supported by MOD to get the exact numbers of hours:
=(NETWORKDAYS(RequestStartDate,RequestEndDate)-1-MOD(RequestStartDate,1)+MOD(RequestEndDate,1))*24

Is it possible to achieve something similar in Power Query?

Thank you so much!

Avatar
Riny van Eekelen
Örnsköldsvik, Sweden
Moderator
Members


Trusted Members

Moderators

Power BI
Level 0
Forum Posts: 439
Member Since:
January 31, 2022
sp_UserOfflineSmall Offline
6
September 1, 2022 - 7:45 pm
sp_Permalink sp_Print

Hi Katarzyna,

The attached workbook contains a small example, replicating the results of your formula in PQ. It's not a fancy function, but it's achieved by clicking in the User Interface and writing some fairly easy code for a few custom columns. I guess, you can make more dynamic, but all I wanted to demonstrate for now is that PQ can handle such calculations.

Riny

Avatar
Katarzyna Mazur

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
September 1, 2022
sp_UserOfflineSmall Offline
7
September 3, 2022 - 1:45 am
sp_Permalink sp_Print sp_EditHistory

Thank you Riny for looking into mu problem and suggesting a solution. I'm truly grateful for this!

However, I'm afraid this doesn't work 100% correctlyCry If I understand your code properly (I'm still a beginner, hence all of these questions), you create a list of days between the start date and end date so that you can remove Saturdays and Sundays and basing on that, calculate hours worked on weekdays.

As a result, calculations are missing for records with a start date or end date on Sunday or Saturday. I'm wondering if there is any solution for that?

I'm attaching a new doc with more data and also with the very final result I'm aiming for.

Thank you again!
Kasia

Avatar
Riny van Eekelen
Örnsköldsvik, Sweden
Moderator
Members


Trusted Members

Moderators

Power BI
Level 0
Forum Posts: 439
Member Since:
January 31, 2022
sp_UserOfflineSmall Offline
8
September 3, 2022 - 5:17 am
sp_Permalink sp_Print

But, regarding the start and end dates on week-ends you mentioned "However, I think I will be able to find a workaround for this myself so it is not necessary to solve here." Therefore I didn't pursue this any further.

Avatar
Katarzyna Mazur

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
September 1, 2022
sp_UserOfflineSmall Offline
9
September 3, 2022 - 5:33 am
sp_Permalink sp_Print

Ah, got you. I had something else in mind, I guess I wasn't specific enough. 
In general, I want to exclude the weekend days from the calculations because in theory there is no activity on Saturdays and Sundays. This is why my Excel formula is Networkdays.
However, there are some instances with start date or end date on Saturday or Sunday which clearly means that some activity happened on these days. This is why I not only want to have the calculations for these records but also I need to take a bit different approach. I need to make sure that Saturday or Sundays hours are also calculated - in Excel I simply add 24 or 48 hours for these records to my Networkdays formula.
What I meant previously was that the last part should be easy to be added if there is a solution to the main problem.

Avatar
Riny van Eekelen
Örnsköldsvik, Sweden
Moderator
Members


Trusted Members

Moderators

Power BI
Level 0
Forum Posts: 439
Member Since:
January 31, 2022
sp_UserOfflineSmall Offline
10
September 3, 2022 - 4:18 pm
sp_Permalink sp_Print

Okay! I took a closer look at the new data and notice 10 records with start and end on a Sunday and 8 records start on a Saturday and end on either a Saturday or a Sunday. Your Final duration column adds only 24 hours for these. Shouldn't that be 48?

If so, the attached file now includes the week-end adjustments. Note that the file size became too big to upload here, so I deleted the bottom 2000 records.

Let me know if this works for you.

Avatar
Katarzyna Mazur

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
September 1, 2022
sp_UserOfflineSmall Offline
11
September 6, 2022 - 6:45 pm
sp_Permalink sp_Print sp_EditHistory

Thank you Riny for looking into this again.

When both start date and end date are on Sunday or Saturday (e.i. the same day), the calculations should add 24 hours. The logic behind that is the fact that this activity just took one day and not whole weekend.

When activity started on Saturday and ended on Sunday, then it's 2 additional days, hence 48 hrs.

I was able to amend your code to get the exact result Excel gave me so all sorted.

Thank you so much!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Jessica Stewart, Mark Carlson, Calvin Richardson
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
yashal minahil
Oluwadamilola Ogun
Yannik H
dectator mang
Francis Drouillard
Orlando Inocente
Jovitha Clemence
Maloxat Axmatovna
Ricardo Freitas
Marko Meglic
Forum Stats:
Groups: 3
Forums: 24
Topics: 6201
Posts: 27185

 

Member Stats:
Guest Posters: 49
Members: 31861
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.