Link to home
Start Free TrialLog in
Avatar of wn411
wn411

asked on

Access or grouping by week and showing week range and week ending

Hi, I would like to create a query from the following sample data:

Date, Department
1/1/2012           A
1/1/2012           A
1/1/2012           A
1/2/2012           B
1/2/2012           B
1/4/2012           C
1/7/2012           C
...

WeekEnd, WeekRange, Dept_A_Count, Dept_B_Count, Dept_C_Count
1/7          1/1 - 1/7                 3                         2                     2

Ideally, I would also be able to adjust the query if the WeekRange and WeekEnd need to be something different.
Avatar of wn411
wn411

ASKER

I think I might have an idea based on using the weekday number and the absolute value difference from the date.
Avatar of IrogSinta
Can you explain a bit more?  What if you had 1 month of data, would each record in your query result refer to ranges of 1 week each?  How would you define when your week starts and ends?  Or will your table only have 1 full week and nothing out of that 7 day range?
Avatar of wn411

ASKER

Yes, there will be a new field that will have the week range based on the date.

I am halfway through an Access VBA function for this. At first, i tried a solution to convert the week number to the range. But that fails once to cross over to the next or previous year. The dates can be anything. The week will be Monday to Sunday. So, based on the current date, my function is going to add or subtract days to find the Monday (begin date) or Sunday (end date). I am using the weekday number as a reference.
I'm not following. Can you be more specific and answer each of my prevIous questions?
Avatar of wn411

ASKER

I did this in two stages. First, I wrote a VBA function that takes a date and gives me the beginning of the week and the end of the week in two fields. Now that I have this, i can group. The next step was to create a crosstab query to count the records for each department. The result the sample data I posted above.
You mentioned that the week will be from Monday to Sunday but in your sample you have the dates 1/1 - 1/7 which is Sunday to Saturday.  Is the sample data incorrect?

You definitely don't need a vba function to get you the WeekEnd and WeekRange.  These can calculated fields in a query.

Also, will there be more than these 3 departments?  You may not need a crosstab query depending on the number here.
Avatar of wn411

ASKER

Yes the sample data should be Monday to Sunday. Yes, I created a calculated field. But, in that field is my custom VBA function. It seemed too cumbersome to write the logic as an expression in the field.
You missed my question on how many departments are involved.  Are there only 3?
ASKER CERTIFIED SOLUTION
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wn411

ASKER

Yes there will be more than 3. OK I will try that and compare it to the results I have from my function.
Since you said there will be more than 3 departments, you need 2 queries to accomplish this.  The second being a crosstab query.  Create 2 queries using the following SQL:

qryDeptCounts  -  (change TableName to your actual table):
SELECT Format([dt]+7-Weekday([DT],2),"m/d") AS WeekEnd, Format([dt]-Weekday([DT],2)+1,"m/d") & " - " & Format([dt]+7-Weekday([DT],2),"m/d") AS WeekRange, TableName.DEPT
FROM TableName;

Open in new window

qryDeptCounts_Crosstab  -  (this will have the final result):
TRANSFORM Count(qryDeptCounts.DEPT) AS [The Value]
SELECT qryDeptCounts.WeekEnd, qryDeptCounts.WeekRange
FROM qryDeptCounts
GROUP BY qryDeptCounts.WeekEnd, qryDeptCounts.WeekRange
PIVOT "Dept " & [DEPT];

Open in new window

Avatar of wn411

ASKER

Yes I already have the crosstab :)
Avatar of wn411

ASKER

I finally got the chance to try this and it works as well. I decided to keep my custom function. But, this is a great option. Thank you!