Show Date Ending on the group and Chart Crystal Reports - date

I have a Crystal Reports that is pulling data by date range and grouping it by WEEK. Also the data is displayed in Chart. Everything works fine. However, I run in into one problem.
When I group by WEEK, it displays the start date in both grouping and chart. I would like to display the END Date. I wrote a formula to take care of this, however, for some reason the dates are off:
totext(cdate(dateadd("d",6,date({CallDate}))),"MM/dd/yyyy")
For example, I do a date range between 11/10/2013 - 11/23/2013. If I run this without a formula it groups like this:
11/10/2013
11/17/2013
I want to display
11/16/2013
11/23/2013
If I run this with the formula above, I get the following output:
11/20/2013
11/27/2013
I'm not even sure how they formula calculates 20 and 26. Any suggestions on how to fix this problem?

This is a little tricky. CR assumes Sunday to be the first day of the week, unless specified otherwise. So, for example to get the first day of the week is Monday (number 2 in the default CR week), this formula will give you the first day of the week:
If DayOfWeek({#Today}) = 2 Then
{#Today}
Else If DayOfWeek({#Today}) = 3 Then
dateadd ("d",-1,{#Today})
Else If DayOfWeek({#Today}) = 4 Then
dateadd ("d",-2,{#Today})
Else If DayOfWeek({#Today}) = 5 Then
dateadd ("d",-3,{#Today})
Else If DayOfWeek({#Today}) = 6 Then
dateadd ("d",-4,{#Today})
Else If DayOfWeek({#Today}) = 7 Then
dateadd ("d",-5,{#Today})
Else If DayOfWeek({#Today}) = 1 Then
dateadd ("d",-6,{#Today})
To get you the last day of the week, which should be Sunday in a regular work week, this formula would give it to you:
If DayOfWeek({#Today}) = 1 Then
dateadd("d", +6, {#Today})
Else If DayOfWeek({#Today}) = 2 Then
dateadd("d", +5, {#Today})
Else If DayOfWeek({#Today}) = 3 Then
dateadd("d", +4, {#Today})
Else If DayOfWeek({#Today}) = 4 Then
dateadd ("d",+3,{#Today})
Else If DayOfWeek({#Today}) = 5 Then
dateadd ("d",+2,{#Today})
Else If DayOfWeek({#Today}) = 6 Then
dateadd ("d",+1,{#Today})
Else If DayOfWeek({#Today}) = 7 Then
{#Today}
So your days are:
Sunday (1)
Monday (2)
Tuesday (3)
Wednesday (4)
Thursday (5)
Friday (6)
Saturday(7)
So in your case, the formula to get the end date should be:
totext(cdate(dateadd("d",+6,date({CallDate}))),"MM/dd/yyyy")

Seems easier to convert the date to a week number value, then add the number of days you need to get to end of the week date.
Modified from Source:
dateadd("ww",datepart("ww",{Command.REPORTDATE})-1,date(year({Command.REPORTDATE}),01,01)-dayofweek(date(year({Command.REPORTDATE}),01,01)))+7
Adjust the +7 at the end of the formula if you want the week to end on a different day.
Proof sample report

Related

Crystal date based record selection; no lower date through Saturday

I want to set up a Crystal report with a default date range for record selection. I want the date range to be no lower date through Saturday of the current week based on the formula field {#Calc Promise Date}. I am a lighter end user of Crystal and am having trouble with this.
I can use:
{#Calc Promise Date} in AllDatesToToday
But I am looking for a way to have the results be all dates to Saturday of the current week.
Thanks in advance for your help.
Thanks to help from Siva on a similar question, I now have the answer for this one.
Make a new Formula Field of "End Date" that contains:
if DayOfWeek (DateSerial(Year(currentdate),Month(currentdate),Day(currentdate))) = 1
then DateSerial(Year(currentdate),Month(currentdate),Day(currentdate)+6)
else if DayOfWeek(DateSerial(Year(currentdate),Month(currentdate),Day(currentdate))) = 2
then DateSerial(Year(currentdate),Month(currentdate),Day(currentdate)+5)
else if DayOfWeek(DateSerial(Year(currentdate),Month(currentdate),Day(currentdate))) = 3
then DateSerial(Year(currentdate),Month(currentdate),Day(currentdate)+4)
else if DayOfWeek(DateSerial(Year(currentdate),Month(currentdate),Day(currentdate))) = 4
then DateSerial(Year(currentdate),Month(currentdate),Day(currentdate)+3)
else if DayOfWeek(DateSerial(Year(currentdate),Month(currentdate),Day(currentdate))) = 5
then DateSerial(Year(currentdate),Month(currentdate),Day(currentdate)+2)
else if DayOfWeek(DateSerial(Year(currentdate),Month(currentdate),Day(currentdate))) = 6
then DateSerial(Year(currentdate),Month(currentdate),Day(currentdate)+1)
else if DayOfWeek(DateSerial(Year(currentdate),Month(currentdate),Day(currentdate))) = 7
then DateSerial(Year(currentdate),Month(currentdate),Day(currentdate)+0)
Then in Record Selection reference that formula. In my case, the correlating date range for the records was a Through Current Date Range formula field, so my Record Selection was then updated to include the following line:
{#Through Current Date Range}<{#End Date}

Qlikview - arrayList

i need to calculate difference between two date excluding sunday. I have table with dates and i need to calculate number of dates of repeated days from last date.
if i have dates like that
27-05-2017
29-05-2017
30-05-2017
I use this code in script
date(max(Date)) as dateMax,
date(min(Date)) as dateMin
And i get min date = 27-05-2017 and max date = 30-05-2017 then i use in expressions
=floor(((dateMax - dateMin)+1)/7)*6 + mod((dateMax - dateMin)+1,7)
+ if(Weekday(dateMin) + mod((dateMax - dateMin)+1,7) < 7, 0, -1)
And get result 3 days. Thats OK, but the problem is if I have next dates:
10-05-2017
11-05-2017
27-05-2017
29-05-2017
30-05-2017
When use previously code I get min date = 10-05-2017 and max date = 30-05-2017 and result 18, but this is not OK.
I need to count only dates from
27-05-2017
29-05-2017
30-05-2017
I need to get max date and go throw loop repeated dates and if have brake to see is that date sunday if yes then step that date and continue to count repeated dates and if i again have break and if not sunday than close loop and remember number of days.
In my case instead of 18 days i need to get 3 days.
Any idea?
I'd recommend you creating a master calendar in the script where you can apply weights or any other rule to your days. Then in your table or app you can just loop through the dates or perform operations and sum their weights (0: if sunday, 1: if not). Let's see an example:
// In this case I'll do a master calendar of the present year
LET vMinDate = Num(MakeDate(year(today()),1,1));
LET vMaxDate = Num(MakeDate(year(today()),12,31));
Calendar_tmp:
LOAD
$(vMinDate) + Iterno() - 1 as Num,
Date($(vMinDate) + Iterno() - 1) as Date_tmp
AUTOGENERATE 1 WHILE $(vMinDate) + Iterno() - 1 <= $(vMaxDate);
Master_Calendar:
LOAD
Date_tmp AS Date,
Week(Date_tmp) as Week,
Year(Date_tmp) as Year,
Capitalize(Month(Date_tmp)) as Month,
Day(Date_tmp) as Day,
WeekDay(Date_tmp) as WeekDay,
if(WeekDay = '7',0,1) as DayWeight //HERE IS WHERE YOU COULD DEFINE A VARIABLE TO DIRECTLY COUNT THE DAY IF IT IS NOT SUNDAY
'T' & ceil(num(Month(Date_tmp))/3) as Quarter,
'T' & ceil(num(Month(Date_tmp))/3) & '-' & right(year(Date_tmp),2) as QuarterYear,
date(monthStart(Date_tmp),'MMMM-YYYY') as MonthYear,
date(monthstart(Date_tmp),'MMM-YY') as MonthYear2
RESIDENT Calendar_tmp
ORDER BY Date_tmp ASC;
DROP Table Calendar_tmp;

MS Access 2010 (Design View): return Monday of the current week with Monday as 1st day of the week

I need to make my Access query always return the Monday of the current week. I have seen a few solutions on Google/StackOverflow but they are written in SQL and I am a beginner in creating Access queries (I am using the Design view to make them).
Goal: The week should be considered as M T W T F S S. Then, the query should always return the Monday of the current week. Therefore, if it is Sunday, it should still return the Monday before, NOT the next week's Monday. Can anyone explain how to do this using the Design View in Access 2010?
Keep in mind that in this context we are working with dates, so if we do Date() - 1, we will get 1 day prior to today.
Date() ~ Today's date
DatePart(
"w" - Weekday
Date() - Today's date
2 - vBMonday (Access assumes Sunday is the first day of the week, which is why this is necessary.)
1 - vbFirstJan1 - This gets into using the first week of the year. We could have omitted this, as 1 is the default.
)
-1 - Subtract 1 from the DatePart value.
Values
Date() = 4/27/2015 (at time of this writing)
DatePart("w",Date(),2,1) = 1
DatePart("w",Date(),2,1)-1 = 0
So we have Date()-0... Okay, what's so great about that? Well, let's look at a more useful scenario where today's date is a day other than Monday.
Let's act like today is 4/28/2015 (Tuesday)
Date() = 4/28/2015
DatePart("w",Date(),2,1) = 2
DatePart("w",Date(),2,1)-1 = 1
So, from the outside, in; give me the current weekday value. (1 = Monday, 2 = Tuesday, etc.), and subtract 1 from that -> that's how many days we need to subtract from the current date to get back to the weekday value of 1 (Monday).
Here's a function that will do this:
Public Function DatePrevWeekday( _
ByVal datDate As Date, _
Optional ByVal bytWeekday As VbDayOfWeek = vbMonday) _
As Date
' Returns the date of the previous weekday, as spelled in vbXxxxday, prior to datDate.
' 2000-09-06. Cactus Data ApS.
' No special error handling.
On Error Resume Next
DatePrevWeekday = DateAdd("d", 1 - Weekday(datDate, bytWeekday), datDate)
End Function
As vbMonday is 2 and your date is today, you can use the core expression in a query:
PreviousMonday: DateAdd("d",1-Weekday(Date(),2),Date())

Check if the difference between dates is exactly 'n' months in expression SSRS

In my quarterly report Im trying to validate the two parameters StartDate and EndDate.
I first check if the difference between the dates is 2 months:
Switch(DateDiff(
DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2,
"Error message")
Then I try to add whether the StartDate is the first day of month AND EndDate is last day of month:
And (Day(Parameters!StartDate.Value) <> 1
And Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value)))
So the whole expression looks like this:
Switch(DateDiff(DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2
And
Parameters!IsQuarterly.Value = true
And
Day(Parameters!StartDate.Value) <> 1
And
Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value))<>1),
"Error: Quarterly report must include 3 months")
But It works wrong when the difference between dates is still 2 months, but StartDate and EndDate are not first and last day of the whole period.
I'd appreciate any help :)
I would say just change the implementation Add another two Parameter With Quarter and Year
Quarter like Q1,Q2,Q3 & Q4 with Value 1,2,3 & 4 respectively and year 2012,2013,2014 & so on
Now based on the parameter selected Qtr & Year set Default value of start & End Date
=DateSerial(Parameters!Year.Value), (3*Parameters!Qtr.Value)-2, 1) --First day of Quarter
=DateAdd("d",-1,DateAdd("q",1,Parameters!Year.Value, (3*Parameters!Qtr.Value)-2, 1))) --Last day of quarter
Doing this no need to do any validation bcz its always get the correct Date Difference.
Other Reference
First day of current quarter
=DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)
Last day of current quarter
=DateAdd("d",-1,DateAdd("q",1,DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)))

Crystal Date Parameter for last year

I have tried using this formula with a date parameter. It works for the other 11 months but not the 12th month. Could some one please let me know what additional code I need to add to the formula.
If {?End Date} = 12/14/13
I want the formula to = 12/31/12
This is what I have been using to make it work for the other 11 months.
Date(year({?End Date})-1, month({?End Date})+1,1) - 1
I receive an error that the month needs to be 1 - 12.
Any suggestions will be greatly appreciated.
Thanks
This will give you the last day of the month of the previous year:
DateAdd("m", 1, DateTime( Year({immaster.timestmp})-1, Month({immaster.timestmp}), 1, 0,0,0 )) - 1
Try this.
CDate(Year(DateAdd ('yyyy',-1,CurrentDate)),Month(DateAdd ('M',0,CurrentDate)),31)