Results 1 to 11 of 11
  1. #1
    BDibz is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2018
    Posts
    62

    Grouping weeks within years

    Hello,



    I have a dataset that spans a few years of revenue and relevant data. I want to create a chart that shows the revenue per week.

    I currently have a query field called "week" that gives the week number for the date. However, there are multiple data points for each week number since each year has the same week numbers, so I can't group by week number. I need to find a way to group the weeks within the years, and can't think of an effective way to do this.

    Here's a picture of my Weekly Revenue Query. The WHERE query for the date field is linked to a radio-button selector that just allows you to select which year you're viewing the weeks by, which is my current work-around. Ideally, I want to see all of the data, in order, by week.
    Click image for larger version. 

Name:	Capture.PNG 
Views:	16 
Size:	19.0 KB 
ID:	33429

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    make a sort field: YEAR & "-" & week

  3. #3
    BDibz is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2018
    Posts
    62
    Quote Originally Posted by ranman256 View Post
    make a sort field: YEAR & "-" & week
    What is a sort field? And for year and week, would these be [Year] and [Week] from my query?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Don't need DatePart to extract year. Year() function will do that, as you have done in criteria.

    Don't understand the issue. You already have Year and Week in the grouping criteria. This should accomplish what you want without creating another field. If you want all years retrieved then don't apply filter and don't include the Date field. If you do want to see all the raw data then build a report and use its Sorting & Grouping feature with aggregate calcs in group section textboxes instead of the GROUP BY query. Report allows display of detail records as well as summary data.

    Date and Year are reserved words (they are functions). Should not use reserved words as names for anything.

    Also should not use spaces nor punctuation/special characters (underscore is only exception) in any names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    BDibz is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2018
    Posts
    62
    Quote Originally Posted by June7 View Post
    Don't need DatePart to extract year. Year() function will do that, as you have done in criteria.

    Don't understand the issue. You already have Year and Week in the grouping criteria. This should accomplish what you want without creating another field. If you want all years retrieved then don't apply filter and don't include the Date field. If you do want to see all the raw data then build a report and use its Sorting & Grouping feature with aggregate calcs in group section textboxes instead of the GROUP BY query. Report allows display of detail records as well as summary data.

    Date and Year are reserved words (they are functions). Should not use reserved words as names for anything.

    Also should not use spaces nor punctuation/special characters (underscore is only exception) in any names.
    This all works fine in the query, yes, but when I create the chart, it just groups by Week since I can only put one assign one field to the x-axis. So, it will display weeks 1-52 along the x-axis, but it is summing up that week's values across all of the years. So I need to create some type of axis label that makes each week of each year a unique value.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Okay, if you want a chart to display multiple years, then yes, calculate another field that returns the Year and Week as a compound value and use that field in the chart design. Otherwise, apply filter criteria as you show in posted query and that should return only a single year of data.

    That sounds like a very long x-axis. How many years?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    BDibz is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2018
    Posts
    62
    Once I set it up to allow for it to span multiple years or cross over years (IE if I want to go back 6 months, and its only April, I need to cross the year line and still keep my weeks in order) I will set up a text-box input on my form that says "show last ___ weeks" and use that as an input for the criteria on the query.

    Would it work to just concatenate the year and week values into a year-week field and use that? Seems pretty simple. I'll try that.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Actually, 'show last x weeks' criteria is probably going to get rather complicated.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    Quote Originally Posted by June7 View Post
    Actually, 'show last x weeks' criteria is probably going to get rather complicated.
    Sort by Year-Week field in descending order and select top x
    That will work if the YearWeek field is e.g. 201815, 201803, 201751 etc
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  10. #10
    BDibz is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2018
    Posts
    62
    Quote Originally Posted by June7 View Post
    Actually, 'show last x weeks' criteria is probably going to get rather complicated.
    On my first attempt at this DB, I managed to do a rolling chart for the revenue that showed the last 'x' days:

    Click image for larger version. 

Name:	Capture2.PNG 
Views:	12 
Size:	15.6 KB 
ID:	33432

    Using the following query criteria:

    Code:
    Between Date() And Date()-[Forms]![Dashboard].[txtDayCount]
    Which I would consider pretty clever, but I stole it from somewhere on the internet. Anyways, I figure I can do the same thing but with my concatenated field, lets call it WeekYear. So
    Code:
    between Week(Date())&Year(Date()) And Week(Date())&Year(Date()) - [Forms]![Dashboard].[WeekYear]
    However, that would only work if SQL will interpret the WeekYear data as integers or dates or something with a value attached so that the Between function works... which I'm not sure if it will. What do you think?

  11. #11
    BDibz is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2018
    Posts
    62
    Can anyone tell me why Access seems to think 20182 is bigger than 201910, and how I could fix this?

    Edit: Fixed with Val(expression)
    Last edited by BDibz; 04-06-2018 at 03:29 PM. Reason: Solved

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Weeks Conversion
    By jake32008 in forum Access
    Replies: 3
    Last Post: 10-26-2012, 01:28 PM
  2. Replies: 3
    Last Post: 11-22-2011, 11:06 AM
  3. Replies: 1
    Last Post: 06-09-2011, 09:15 AM
  4. Group Result by weeks
    By Grooz13 in forum Queries
    Replies: 2
    Last Post: 09-14-2010, 07:09 AM
  5. How to calculate number of weeks
    By FeatherDust in forum Queries
    Replies: 0
    Last Post: 09-18-2009, 02:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums