Correct ISO week numbering in Crystal Reports XI - crystal-reports

How can I get the ISO-8601 week number of a given date in Crystal Reports XI?

Crystal Reports supports the DatePart-function which can give you the ISO week number of a given date.
NumberVar week := DatePart("ww", date, crMonday, crFirstFourDays);
However, in Crystal Reports XI there is a bug that gives erronous results round new year. The best solution is probably to create an own function getISOWeekNumber:
Function (optional DateVar d := CurrentDate)
NumberVar week := DatePart("ww", d, crMonday, crFirstFourDays);
// Correct for that CR doesn't handle the fact that the last days of a year can belong to week 1 of the next year:
if week = 53 and DatePart("ww", cDate(year(d) + 1, 1, 1), crMonday, crFirstFourDays) = 1 then
week := 1
// A bug in CR makes DatePart return values like 9363 for days in January that belongs to the last week of the previous year.
else if week > 53 then
week := DatePart("ww", cDate(year(d) - 1, 12, 31), crMonday, crFirstFourDays);
week;
To get the "week-year" of a specific date, you could then use the following function:
// Returns the year to which the ISO week of the specified date belongs.
// E.g. 2012-12-31 will return 2013, as that date belongs to week 1 of 2013.
Function (optional DateVar d := CurrentDate)
NumberVar week := getISOWeekNumber (d);
if week = 1 and month(d) = 12 then
year(d) + 1
else if week > 10 and month(d) = 1 then
year(d) - 1
else
year(d);

Related

Subtracting 1 ISO 8601 year from a date in BigQuery

I'm trying to manipulate a date value to go back in time exactly 1 ISO-8601 year.
The following does not work, but best describes what I want to accomplish:
date_add(date '2018-01-03', interval -1 isoyear)
I tried string conversion as an intermediate step, but that doesn't work either:
select parse_date('%G%V%u',safe_cast(safe_cast(format_date('%G%V%u',date '2018-01-03') as int64)-1000 as string))
The error provided for the last one is "Failed to parse input string "2017013"". I don't understand why, this should always resolve to a unique date value.
Is there another way in which I can subtract an ISO year from a date?
This gives the corresponding day of the previous ISO year by subtracting the appropriate number of weeks from the date. I based the calculation on the description of weeks per year from the Wikipedia page:
CREATE TEMP FUNCTION IsLongYear(d DATE) AS (
-- Year starting on Thursday
EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 5 OR
-- Leap year starting on Wednesday
(EXTRACT(DAY FROM DATE_ADD(DATE(EXTRACT(YEAR FROM d), 2, 28), INTERVAL 1 DAY)) = 29
AND EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 4)
);
CREATE TEMP FUNCTION PreviousIsoYear(d DATE) AS (
DATE_SUB(d, INTERVAL IF(IsLongYear(d), 53, 52) WEEK)
);
SELECT PreviousIsoYear('2018-01-03');
This returns 2017-01-04, which is the third day of the 2017 ISO year. 2018-01-03 is the third day of the 2018 ISO year.

Format date range for next year

I'm trying to get a date range that takes the calendar year after the transaction date. Say if the date was this year then it should be 1/01/2018 - 12/31/2018.
I'm using this syntax but am getting the error that everything after the first line is not part of the formula. Any ideas?
Local DateVar d := CDate cstr(year({TransDate}));
Local DateVar Range dr := DateSerial (Year(d)+ 1, Month(d) - 1, 1) To
DateSerial (Year(d)+ 1, Month(d) - 1, 1);
You're missing parentheses for CDate in your first line. Proper syntax would be:
Local DateVar d := CDate(cstr(year({?Start Date})));
However that still won't work, because the result of a formula cannot be a date range. Instead, separate the range into a StartDate and EndDate field, then use those for your calculations.
StartDate: DateSerial(Year({?Date}) + 1, 1, 1)
EndDate: DateSerial(Year({?Date}) + 1, 12, 31)

Want to get previous date from current date

i have problem finding solution on getting previous date from current's date. but i only want the months to change. for example, now is August, i want the output to display 3 months ago = May.
Heres my code that i wrote
givenDate= "14-August-15"
DD = Day (givenDate)
'MsgBox DD
MM = Month (givenDate)
'MsgBox MM
YY = Year (givenDate)
'MsgBox YY
SysDate = DD&"/"&MM&"/"&YY
MsgBox Month(DateAdd("m", -3, "14-August-2015"))
'MsgBox(FormatDateTime(SysDate,1))
If you just need to display the name of the month that was 3 months ago, combine DateAdd(), Month(), and MonthName(). For example:
Dim dt1, dt2
dt1 = Date() ' Use today's date, for example
dt2 = DateAdd("m", -3, dt1) ' Subtract 3 months
WScript.Echo MonthName(Month(dt2)) ' Display the name of the month
Month() returns the month number (1 - 12). MonthName() takes that number and returns the name of the month ("January" - "December").

How to get the Weekly or 7 days of the Date Range in Crystal Report

How can I get the 7 days or weekly depends on date range in formula fields of Crystal Report?
Example:
Date Range from March 01, 2014 to March 31, 2014
Output:
Week 1 (March 01 to March 07)
Week 2 (March 08 to March 14)
Week 3 (March 15 to March 21)
Week 4 (March 22 to March 28)
Week 5 (March 29 to March 31)
Custom function RangeWeekSplitter works within one year. Crystal Syntax.
Function (dateTimeVar dFrom, dateTimeVar dTo)
// First day of the year
local dateTimeVar dBegCurrYear:= Date (Year(dFrom), 1, 1);
// Day of year (1 to 365 or 366 in a leap year)
local numberVar nFrom:= DatePart ("y", dFrom);
local numberVar nTo:= DatePart ("y", dTo);
local numberVar i;
local numberVar iTo;
local numberVar nDaysInWeek:= 7; // number of days in the week
local numberVar nWeek:= 0; // counter weeks
local stringVar sResult:= ""; // output string
for i:= nFrom to nTo step nDaysInWeek do
(
nWeek:= nWeek+1;
iTo:= i+(nDaysInWeek-1);
if(i+nDaysInWeek > nTo)
then iTo:= nTo;
// generate output string
sResult:= sResult + chr(13)+
"Week " + CStr(nWeek) + " (" +
CStr(DateAdd ("y", i-1, dBegCurrYear), "MMMM d") +
" to " +
CStr(DateAdd ("y", iTo-1, dBegCurrYear), "MMMM d") +
")";
);
sResult;
Usage example:
// Date range
local dateTimeVar dFrom:= Date (2016, 1, 14);
local dateTimeVar dTo:= Date (2016, 3, 4);
RangeWeekSplitter (dFrom, dTo);
You can use DatePart with "ww", too. See IBM Knowledge Center:
DatePart (intervalType, inputDateTime)
...
ww: Week of year (1 to 53 with firstDayOfWeek and firstWeekOfYear determining the exact days of the first calendar week of the year)
Combined with DatePart ("w", inputDateTime) or DayOfWeek(inputDateTime) getting the day of week, you can calculate your first and last day of the current calendar week.
So for one specific date (inputDateTime), this would be your formula "RangeWeek":
Function (DateTimeVar inputDateTime)
NumberVar cw := DatePart("ww", inputDateTime);
DateTimeVar first := DateAdd("d", 1 - DayOfWeek(inputDateTime, crMonday), inputDateTime);
DateTimeVar last := DateAdd("d", 7 - DayOfWeek(inputDateTime, crMonday), inputDateTime);
"Week " + ToText(cw) + " (" + ToText(first) + " to " + ToText(last) + ")"
You need to give ToText the format strings you want, of course.
Example:
Input: "August 23, 2017"
Output: "Week 34 (August 21 to August 27)"
That makes things easier in the formula where you get the date range.
DateTimeVar from := ...;
DateTimeVar to := ...;
NumberVar cw;
NumberVar count := 0;
StringVar output := "";
for cw := DatePart("ww", from) to DatePart("ww", to) do
(
output := output + chr(13) + RangeWeek(DateAdd("d", 7*count, from));
count := count + 1;
);
output

LastFullWeek starting with monday in crystal reports

When using LastFullWeek i get the last Sunday - Saturday week, but for my reports i want the last Monday - Sunday week.
Is there any simple way to get this behaviour, or do I have to write my own function for it (which isn't that hard, but unconvenient for such a common date span.)
custom function:
//LastFullWeekEx
Function (DateVar date, Optional NumberVar firstDayOfWeek := crSunday)
(date - DayOfWeek(date, firstDayOfWeek)) - 6 TO (date - DayOfWeek(date, firstDayOfWeek))
usage:
// use with non-volatile DataDate and Sunday
{TABLE.DATE} IN LastFullWeekEx(DataDate)
// use with non-volatile DataDate and Monday
{TABLE.DATE} IN LastFullWeekEx(DataDate, crMonday)
testing:
// should return True
( Minimum(LastFullWeek) = Minimum(LastFullWeekEx(DataDate, crSunday)) ) AND
( Maximum(LastFullWeek) = Maximum(LastFullWeekEx(DataDate, crSunday)) )
Instead of using
{DATE} in LastFullWeek
use the form
{DATE}-1 in LastFullWeek
The code, if no better answer comes along, for others who find this question through the search engine:
{DATE} >= currentdate - dayofweek(currentdate, crMonday) - 6 AND
{DATE} < currentdate - dayofweek(currentdate, crMonday) + 1
You have no other choice than to use a custom formule as Crystal Reports uses fixed week settings (US). So a week is Sunday to Saturday and DayOfWeek starts with Sunday (value/index 1).
Add +1 to LastFullWeek min and max:
DateVar Start := Minimum(LastFullWeek)+1; //Monday
DateVar End := Maximum(LastFullWeek)+1; //Sunday
Edit: Since LastFullWeek always takes Sunday as the first day of the week, we need to check if the current day is Sunday. If so, we need to subtract a week:
// Check if the current day is Sunday
IF (DAYOFWEEK(CurrentDate) = 1)
THEN (
Start := Start - 7;
End := End - 7
);
Example: Date range to string
StringVar LastWeekRange := ToText(Start) + " - " + ToText(End);
LastWeekRange;
Return example: "MM/DD/YYYY - MM/DD/YYYY"
Example: Selecting all dates within date range
{datefield} >= Start and {datefield} <= End;