Microsoft Access
--
Questions
--
Followers
Top Experts
Can someone help me choose the right function to count the number of days per month within a given set of dates?
A small sample file attached.
I can get the the total count of days between the two days, using this:
Optimally, I need to show something like this (summing the total days in each month for all Orders whose dates fall within those months):
Month   Total Days
Sep        90.23
Oct         125.15
Nov        150.85
Book1.xlsx
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
and will your dates span years?
and what about leap years?
use the code on this link.
http://www.iaccessworld.com/wp-content/uploads/2016/06/function.png
or
http://www.projectperfect.com.au/microsoft-access-sample-6.htm
So if Start Date is 10/24/2016, End Date 11/4/2016
Days per Week are 4
Hours per Day are 10
Then the result should be
Oct:
 (8 days/7 days in a week) = work weeks
  Work Weeks * 4 Days per week * 10 hours per day= Total hours worked
 Total hours worked / 8 hour day = Total Work Days (based on an 8 hour day)
= 5.71 days
24/10/2016 to 4/11/2016 is 9 working days and 11 in total - neither have 4 days/week?
I believe I have answered your original question but do not have time to address the second - hopefully someone else will run with this.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
For example, if the dates are 10/14/16 through 12/05/16, then I need to know how many of the total days fall in October, how many fall in November and how many fall in December.
There there's the additional calculation of days/week and hours/day, but I think I can add that in easily if I knew how to subtotal my days by month.
I found a custom SQL function by WayneS on the Internet: Â http://www.sqlservercentral.com/Forums/Topic1022335-149-1.aspx
But I don't know how to modify this to work in Access.
two dates using an Access function
Upload sample data in an access database.Add few representative records, and show the required output.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
The end result will be a number of cells containing the number of days in each month.
PARAMETERS
DateStart DateTime,
DateEnd DateTime;
SELECT DISTINCT
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10) AS Factor,
Year(DateAdd("m",[Factor],[DateStart])) AS [Year],
Month(DateAdd("m",[Factor],[DateStart])) AS [Month],
IIf(DateDiff("m",DateAdd("m",[Factor],[DateStart]),[DateEnd])=0,
Day([DateEnd]),Day(DateSerial(Year(DateAdd("m",[Factor],[DateStart])),Month(DateAdd("m",[Factor],[DateStart]))+1,0))
-IIf([Factor]=0,Day([DateStart])-1,0)) AS Days
FROM
msysobjects AS Uno,
msysobjects AS Deca
WHERE
(((10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10))<=DateDiff("m",[DateStart],[DateEnd])));
Example from 2015-12-15 to 2016-04-17:






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
SELECT DISTINCT
Dates.Id,
Dates.DateStart,
Dates.DateEnd,
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10) AS Factor,
Year(DateAdd("m",[Factor],[DateStart])) AS [Year],
Month(DateAdd("m",[Factor],[DateStart])) AS [Month],
IIf(DateDiff("m",DateAdd("m",[Factor],[DateStart]),[DateEnd])=0,
DateDiff("d",[DateStart],[DateEnd]),Day(DateSerial(Year(DateAdd("m",[Factor],[DateStart])),Month(DateAdd("m",[Factor],[DateStart]))+1,0))
-IIf([Factor]=0,Day([DateStart])-1,0)) AS Days
FROM
msysobjects AS Uno,
msysobjects AS Deca,
Dates
WHERE
(((10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10))<=DateDiff("m",[DateStart],[DateEnd])));
Note please, that I've corrected an error in the day count of the first month in those cases where both dates fall in the same month, and in the day count of the last month which was off by 1.Example:

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Here is the corrected query. I've added a field for the total count of days as a control:
SELECT DISTINCT
Dates.Id,
Dates.DateStart,
Dates.DateEnd,
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10) AS Factor,
Year(DateAdd("m",[Factor],[DateStart])) AS [Year],
Month(DateAdd("m",[Factor],[DateStart])) AS [Month],
IIf(DateDiff("m",[DateStart],[DateEnd])=0,
DateDiff("d",[DateStart],[DateEnd]),
IIf(DateDiff("m",DateAdd("m",[Factor],[DateStart]),[DateEnd])=0,
Day([DateEnd])-1,
Day(DateSerial(Year(DateAdd("m",[Factor],[DateStart])),Month(DateAdd("m",[Factor],[DateStart]))+1,0))
-IIf([Factor]=0,Day([DateStart])-1,0))) AS
Days,
DateDiff("d",[DateStart],[DateEnd]) AS DaysTotal
FROM
msysobjects AS Uno,
msysobjects AS Deca,
Dates
WHERE
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)<=DateDiff("m",[DateStart],[DateEnd]);
/gustav
Thank you for sticking with me and helping with this.
It seems that Total Days and the day count of the last month may be off by 1.
I can easily modify the total days formula, ((DateDiff("d",[StartDate]
But I honestly don't know how to change your other formulas to add a day to the count of the last month.
The count is 0 if DateStart equals DateEnd. If you wish to raise the count to include the end date, just add 1, but then you will never be able to have a count of zero.
/gustav






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
You should be paid for this help! Â Â ;)
/gustav

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Thanks man :-( Â It wasn't like I wasn't feeling bad enough already.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
/gustav
Microsoft Access
--
Questions
--
Followers
Top Experts
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.
