?
Solved

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

Posted on 2012-09-18
13
Medium Priority
?
281 Views
Last Modified: 2012-10-23
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.
0
Comment
Question by:wn411
  • 7
  • 6
13 Comments
 

Author Comment

by:wn411
ID: 38411756
I think I might have an idea based on using the weekday number and the absolute value difference from the date.
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38412493
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
 

Author Comment

by:wn411
ID: 38414404
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:IrogSinta
ID: 38414514
I'm not following. Can you be more specific and answer each of my prevIous questions?
0
 

Author Comment

by:wn411
ID: 38429369
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
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38429674
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
 

Author Comment

by:wn411
ID: 38447983
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
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38447989
You missed my question on how many departments are involved.  Are there only 3?
0
 
LVL 29

Accepted Solution

by:
IrogSinta earned 2000 total points
ID: 38448005
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
 

Author Comment

by:wn411
ID: 38463526
Yes there will be more than 3. OK I will try that and compare it to the results I have from my function.
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38464235
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
 

Author Comment

by:wn411
ID: 38471602
Yes I already have the crosstab :)
0
 

Author Closing Comment

by:wn411
ID: 38528381
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question