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.
wn411Asked:
Who is Participating?
 
IrogSintaConnect With a Mentor Commented:
This isn't too cumbersome for a query, is it?

WeekBeg: Format([dt]-Weekday([DT],2)+1,"m/d")
WeekEnd: Format([dt]+7-Weekday([DT],2),"m/d")
WeekRange: [WeekBeg] & " - " & [WeekEnd]
0
 
wn411Author Commented:
I think I might have an idea based on using the weekday number and the absolute value difference from the date.
0
 
IrogSintaCommented:
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?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
wn411Author Commented:
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.
0
 
IrogSintaCommented:
I'm not following. Can you be more specific and answer each of my prevIous questions?
0
 
wn411Author Commented:
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.
0
 
IrogSintaCommented:
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.
0
 
wn411Author Commented:
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.
0
 
IrogSintaCommented:
You missed my question on how many departments are involved.  Are there only 3?
0
 
wn411Author Commented:
Yes there will be more than 3. OK I will try that and compare it to the results I have from my function.
0
 
IrogSintaCommented:
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

0
 
wn411Author Commented:
Yes I already have the crosstab :)
0
 
wn411Author Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.