Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Use data from previous year Post Reply Post New Topic
Author Message
Kwirky
Newbie
Newbie
Avatar

Joined: 21 Apr 2010
Online Status: Offline
Posts: 32
Quote Kwirky Replybullet Topic: Use data from previous year
     Posted: 01 Jun 2011 at 5:55pm
Hi,
Am trying to figure out a formula/s to show me the date of the previous year, based on day of the week and week number.
Say yesterday is Wednesday 1st June. I want to find out the DATE for Wednesday LAST YEAR. I know that this date is 2/6/2010 by checking a calendar.
So what I want is for an automatic calculation based on the week number and then the day number.
So far I have the following (but have not figured out how to tie them together to get the date):
@previousyear
date(year(currentdatetime)-1,month(currentdatetime),day(currentdatetime)-1)

@weeknumber
DatePart ("ww", {@PreviousYear})

I tried this, which I got from Ken Harmady website, but it gives me the date of 8th of June 2010 and no matter what I do to it, I cannot get it to say 2nd June 2010:

WhileReadingRecords;
DateVar target := Date (2010,1,1);
    if DayOfWeek (target) in 2 to 5
    then  target:= target - 7 * ({@weeknumber}-1)  
    else target:= target + 7 * ({@weeknumber}) ;

target - DayOfWeek (target) + {@dayofweek}



I am using CR2008 & SQL database
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 02 Jun 2011 at 4:09am
local datetimevar target;
if DATEPART(dw,'6/5/11') <> 1
target := DATEADD("YY,"-1,'6/5/11') + DATEPART("dw",'6/5/11') - DATEPART("dw",dateadd(yy,-1,'6/5/11'))

else

target := DATEADD("YY",-1,'6/5/11') + DATEPART("dw",'6/5/11') - DATEPART("dw",dateadd(yy,-1,'6/5/11'))+7

this worked for me in sql server...
 
it finds out the date from a year ago and then adjusts it for the day of week to get the same day of week.
 
HTH


Edited by lockwelle - 02 Jun 2011 at 4:11am
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum