We help IT Professionals succeed at work.

Group by Week Ending Monday through Sunday in a Cross-Tab Object

gsszuber asked
Medium Priority
Last Modified: 2012-05-06
I'm trying to create a reports that summarizes sales and groups by year, then by month. Within the month grouping I would like to have a cross-tab with columns for each week ending Sunday or the last day of the month, whichever comes first in that particular week. For instance, in January 2009 there would be columns for 1/4/2009, 1/11/2009, 1/18/2009, 1/25/2009, and 1/31/2009 (I would want to display the last day in the period for the column label). I tried to group on a formula contributed by bdreed35 to a previous question I asked (see code snippit), but it doesn't seem to function correctly when used in a cross-tab. It seems to display the week ending Saturdays instead.
local datevar input := {@DateKey};
if dayofweek(input) in 2 to 7
then input + (8 - dayofweek(input))
else input

Open in new window

Watch Question

The formula looks OK.  It seems like it should give you the next Sunday, and I can't think of any reason why you'd get different results in a cross-tab.  Have you tried putting the formula somewhere on the report, just to make sure that it's giving you the correct dates?  (Kind of a desperation move, really, since I really think the formula should work)

 Putting that aside for the moment, you also need to change the formula to not advance past the last day of the month.  I think the following will work (ignoring the problem that you're having in the cross-tab):

local datevar input := {@DateKey};
if dayofweek(input) in 2 to 7
then input := input + (8 - dayofweek(input));

if Month (input) <> Month ({@DateKey}) then
  input := Date (DateAdd ("d", -1, Date (Year (input), Month (input), 1)));


 That seems to work here, but I didn't try it in a cross-tab.


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Indeed, when I group by @DateKey in the report itself, not in a cross-tab, it outputs each Sunday. Then again, I'm not sure whether it's displaying the first or last day of the week... I don't see an option of which to display when grouping in the report.

That being said, I just thought of another approach that should work. I was thinking I could create a SQL Command that would add a calculated column to the table that would simply be the week-endinding Sunday date that that particular day belongs in. The logic should work something like this:

1. Is the date a Sunday? If yes, output the same date.
2. If no, is the next Sunday in the same month? If yes, output the next Sunday.
3. If no, output the last day of the month.

Then I could group on this calulated column and it should produce the desired result. Would you agree?

What does @DateKey produce?  Could it be any date, or is it another formula designed to only produce certain dates (eg. only a certain day of the week)?

 Assuming that it's not restricted in some way, if you're grouping by @DateKey and you're getting groups for each Sunday, I assume that you have the report set to group by week.  That seems to use the default week of Sunday - Saturday and the date shown for the group is from the start of each week.  I don't see any way to change either of those (although there may be something that I'm not aware of).

 However, that also seems pretty irrelevant.  The point was to have the @WeekEndDate formula produce the dates you needed and group by that.  It doesn't really matter what you get when you group by @DateKey.  All that matters is that @DateKey produces the correct dates, and isn't doing some manipulating of its own, like altering the dates to get a certain day of the week.  If it's altering the dates, then that might affect how, or if, you can use it in the @WeekEndDate formula.

 Getting back to the @WeekEndDate formula:

 I took the version that I posted and tried it here and it worked fine as the columns in a cross-tab (and, FWIW, as a group on the report).  For example, when I included all of January, the dates I got were 01/04, 01/11, 01/18, 01/25 and 01/31 (01/31 was a Saturday).  For the Group Options for that group in the cross-tab, I selected:

 "The column will be printed:"
  "for each day"

 "The value printed for the column will be:"
  "the first date in the period"

 Technically, the second one shouldn't matter.  The choices are "first date" or "last date" and since I used "for each day", each group only has one date, so the results should be the same either way.

 So, depending on what's in @DateKey, @WeekEndDate should work.

 You could do this in SQL, using code very similar to what I posted.  But you shouldn't have to.  The formula that I posted should work.



@DateKey is simply a formula that converts the a date column from YYYYMMDD to an actual date.

For whatever reason, when I replaced the formula I started with for @WeekEndDate with yours, everything behaves the way I would like! I'm not going to argue with that. I'm going to give you full credit for the solution.

In the meantime, I also solved this using a SQL Command, so I'm going to post my solution as well for all the other Expert Exchange readers. It's a little convoluted, but it worked for me. This is the SQL just for the calculated field called 'WeekEnding' that you will be able to group by and get the same result as with James' solution. It does have the benefit of maybe some additional server-side processing, but I couldn't tell a difference between the two on my report.
WHEN DATEPART(WEEKDAY, DateField) = '1' THEN DateField
	CASE WHEN (DATEADD(mm,1,DateField - day(DateField)+1)-1) <
	(DATEADD(dd, 6, DateField)) THEN
	(DATEADD(mm,1,DateField - day(DateField)+1)-1) ELSE
	(DATEADD(dd, 6, DateField)) END
	CASE WHEN (DATEADD(mm,1,DateField - day(DateField)+1)-1) <
	(DATEADD(dd, 5, DateField)) THEN
	(DATEADD(mm,1,DateField - day(DateField)+1)-1) ELSE
	(DATEADD(dd, 5, DateField)) END
	CASE WHEN (DATEADD(mm,1,DateField - day(DateField)+1)-1) <
	(DATEADD(dd, 4, DateField)) THEN
	(DATEADD(mm,1,DateField - day(DateField)+1)-1) ELSE
	(DATEADD(dd, 4, DateField)) END
	CASE WHEN (DATEADD(mm,1,DateField - day(DateField)+1)-1) <
	(DATEADD(dd, 3, DateField)) THEN
	(DATEADD(mm,1,DateField - day(DateField)+1)-1) ELSE
	(DATEADD(dd, 3, DateField)) END
	CASE WHEN (DATEADD(mm,1,DateField - day(DateField)+1)-1) <
	(DATEADD(dd, 2, DateField)) THEN
	(DATEADD(mm,1,DateField - day(DateField)+1)-1) ELSE
	(DATEADD(dd, 2, DateField)) END
	CASE WHEN (DATEADD(mm,1,DateField - day(DateField)+1)-1) <
	(DATEADD(dd, 1, DateField)) THEN
	(DATEADD(mm,1,DateField - day(DateField)+1)-1) ELSE
	(DATEADD(dd, 1, DateField)) END
END AS 'WeekEnding'
FROM TableName

Open in new window


I don't know.  Like I said, apart from not handling the end of the month, your formula looked like it should work.  About the only thing I can think of is that you had the cross-tab set to group by the week, instead of the day, and it was showing you the last day of the week (Saturday).

 Thanks for posting that code.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.