In this article
The Date Filter component allows you to filter the report or a report page by different time periods (custom date ranges, days, weeks, months, quarters, years).
To create a Date Filter:
- Add a Date Filter component to a report master or page .
- Create a Parameter of type: String Response (go to Defining a Parameter for more information).
- Associate the Parameter with the Date Filter by dragging it on top of the Date Filter .
- Create a Filter Script . Paste the script below into it.
- In the Report toolbox > Code Library Script folder, add a Codelibrary Script and paste the script below into it.
- Enable the Filter for the report/page (go to Creating a Simple Filter on Interview Status - Example for more information).
- For testing purposes, you may wish to display some text to ensure the date component is returning the values you expect. To do this place a Text object near the Date Filter component and insert the following script (via right-click):
Figure 1 - Adding a Date Filter to the Report Master
Figure 2 - Associating the parameter with the Date Filter
The Date Filter component will now have the parameter name on it.
Note: If you have changed the parameter name from the default (p1), you need to edit the script accordingly to refer to the name you have given the parameter.
filter.Expression = DateUtils.GetExpression(state, report, "p1", "interview_start", log);
filter.Expression=report.DateRangeUtils.GetRollingDateRangeExpression('interview_start','p1');
log.LogDebug("filter"+filter.Expression);
Here you need to provide the parameter name (“p1” in the example) and the name for the date (“interview_start” in the example). State, report and log should be left as is.
class DateUtils
{
static function GetExpression(state:ReportState, report:Report, parameterName:String, variableName:String, log: Logger)
{
if (state.Parameters.IsNull(parameterName))
return '';
var dr:DateInfo = report.DateRangeUtils.GetDateInfo(parameterName);
var eTemplate : String ="DATEBETWEEN(" + variableName + ", TODATE(\"{0}\"), TODATE(\"{1}\"))";
var expression: String = getRange(variableName, dr, eTemplate);
var dates = [];
var type: String = dr.Type;;
switch (type)
{
case 'WEEKS':
log.LogDebug("week");
dates = DateUtils.setWeeksRoll(dr.Options);
break;
case 'DAYS':
log.LogDebug("Days ");
dates = DateUtils.setDaysRoll(dr.Options, log);
break;
case 'MONTHS':
log.LogDebug("MONTHS");
dates = DateUtils.setMonthsRoll(dr.Options);
break;
case 'QUARTERS':
log.LogDebug("q");
dates = DateUtils.setQuartersRoll(dr.Options);
break;
case 'YEARS':
log.LogDebug("years");
dates = DateUtils.setYearsRoll(dr.Options);
break;
}
log.LogDebug("dates: " + dates);
if (dates && dates[0] && dates[1])
expression = String.Format(eTemplate, dates[0], dates[1]);
log.LogDebug("Expression: " + expression);
return expression;
}
static function getRange(variableName:String, drinfo:DateInfo, template:String)
{
var startDate = "";
var endDate ="";
var start = "";
var end ="";
if (drinfo.Range.start.HasValue)
{
startDate = drinfo.Range.start.Value.ToString("yyyy-M-d");
if (!drinfo.Range.end.HasValue)
return String.Format('{0} >= TODATE("{1}")', variableName, startDate);
}
if (drinfo.Range.end.HasValue)
{
endDate = drinfo.Range.end.Value.ToString("yyyy-M-d");
if (!drinfo.Range.start.HasValue)
return String.Format('{0} <= TODATE("{1}")', variableName, endDate);
}
var expr = String.Format(template, startDate, endDate);
return expr;
}
static function startOfWeek()
{
var s = DateTime.Today;
var diff: int = s.DayOfWeek - DayOfWeek.Sunday;
if (diff < 0)
{
diff += 7;
}
return s.AddDays(-1 * diff).Date;
}
static function setWeeksRoll(options)
{
var start = "";
var end = "";
switch (options['periodType'])
{
case 1:
end = DateTime.Now.ToString("yyyy-M-d");
start = startOfWeek().ToString("yyyy-M-d");
return [start, end];
break;
case 2:
var prev: int = options['previousWeeks'];
var endDate = startOfWeek();
end = endDate.ToString("yyyy-M-d");
start = endDate.AddDays(-7*prev).ToString("yyyy-M-d");
return [start, end];
}
}
static function setDaysRoll(options, log:Logger)
{
var start = "";
var end = "";
switch (options['periodType'])
{
case 1:
start = DateTime.Today.ToString("yyyy-M-d");
end = DateTime.Now.ToString("yyyy-M-d");
return [start, end];
break;
case 2:
var today = DateTime.Today;
end = today.AddDays(-1).ToString("yyyy-M-d");
start = end;
return [start, end];
break;
case 3:
var prev: int = options['previousDays']['number'];
var endDate = DateTime.Today;
var includeToday: Boolean = options['previousDays']['isTodayIncluded'];
start = endDate.AddDays(-1*prev).ToString("yyyy-M-d");
if (!includeToday)
endDate = endDate.AddDays(-1);
end = endDate.ToString("yyyy-M-d");
return [start, end];
break;
}
}
static function setMonthsRoll(options)
{
var start = "";
var end = "";
switch (options['periodType'])
{
case 1:
var endDate = DateTime.Now;
end = endDate.ToString("yyyy-M-d");
start = new DateTime(endDate.Year, endDate.Month, 1).ToString("yyyy-M-d");
return [start, end];
break;
case 2:
var prev: int = options['previousMonths'] - 1;
var now = DateTime.Now;
var endDate = new DateTime(now.Year, now.Month, 1).AddDays(-1);
end = endDate.ToString("yyyy-M-d");
start = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(-1*prev).ToString("yyyy-M-d");
return [start, end];
}
}
static function setQuartersRoll(options)
{
var start = "";
var end = "";
switch (options['periodType'])
{
case 1:
var endDate = DateTime.Now;
end = endDate.ToString("yyyy-M-d");
start = new DateTime(endDate.Year, endDate.Month/3*3, 1).ToString("yyyy-M-d");
return [start, end];
break;
case 2:
var prev: int = options['previousQuarters'] - 1;
var now = DateTime.Now;
var endDate = new DateTime(now.Year, now.Month/3*3, 1).AddDays(-1);
end = endDate.ToString("yyyy-M-d");
start = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(-3*prev).ToString("yyyy-M-d");
return [start, end];
}
}
static function setYearsRoll(options)
{
var start = "";
var end = "";
switch (options['periodType'])
{
case 1:
var endDate = DateTime.Now;
end = endDate.ToString("yyyy-M-d");
start = new DateTime(endDate.Year, 1, 1).ToString("yyyy-M-d");
return [start, end];
break;
case 2:
var prev: int = options['previousYears']-1;
var now = DateTime.Now;
var endDate = new DateTime(now.Year, 1, 1).AddDays(-1);
end = endDate.ToString("yyyy-M-d");
start = new DateTime(now.Year-1, 1, 1).AddYears(-1*prev).ToString("yyyy-M-d");
return [start, end];
}
}
}
:var p = state.Parameters.GetString('p1');
text.Output.Append(p);Note: Be sure to use the same parameter name you entered above.
Applying the Date Filter
By default, the All dates value is set for the Date Filter, which means that the Date Filter is not applied. You can change the default values by adding a custom render script (that will pre-define Date Filter values) to any report component/page . Also, you can output the Date Range value(s) as text in a Text Component by adding a custom render script.
To apply the Date Filter:
- Click All dates. The Date Filter menu opens .
- Select the appropriate filtering method and click Apply to apply the filter to the report/report page. Available filter criteria are:
- Custom range - selecting this displays the custom date range tab. Define a date range by selecting the start and end dates of the range either by means typing the dates in the input fields in the <DD.MM.YYYY> format or by navigating through the calendar using the arrow buttons and clicking the date. You can check the No start date or No end date option to make the date range open in either direction.
- Days - selecting this displays the days tab where you can set the day(s) to filter the report/report page by:
- Today.
- Yesterday.
- Previous days - specify the relative date interval by typing an integer number up to 999 in the input field. Check the Include today's date option if desired.
- Weeks - selecting this displays the weeks tab where you can set the week(s) to filter the report/report page by:
- Current week to date.
- Previous week(s) - specify the relative date interval by typing an integer number up to 100 in the input field.
- Selected week - select the week via the calendar control.
- Months - selecting this displays the months tab where you can set the month(s) to filter the report/report page by:
- Current month to date.
- Previous month(s) - specify the relative date interval by typing an integer number up to 100 in the input field.
- Month - select the month and the year from the drop-down menus.
- Quarters - selecting this displays the quarters tab where you can set the quarter(s) to filter the report/report page by:
- Current quarter to date.
- Previous quarter(s) - specify the relative date interval by typing an integer number up to 50 in the input field.
- Quarter - select the quarter and the year from the drop-down menus.
- Years - selecting this displays the years tab where you can set the year(s) to filter the report/report page by
- Current year to date.
- Previous year(s) - specify the relative date interval by typing an integer number up to 15 in the input field.
- Year - select the year from the drop-down menu.
- Click Apply to apply the filter to the page components.
Figure 3 - Example of a Date Filter