Knowledge Base Article
Hi Jake,
Glad this article and code was helpful!
A similar approach can be used for any date format whether for the "from" or "to" parameter , where the date is defined in Javascript as a date object and converted to a string for either the "from" or "to" parameter".
Any valid date object can be used, including date objects dynamically defined to the beginning of the year or quarter, and which will automatically update.
Here is code to automatically set the "from" parameter to the first date of the current year, and which will dynamically update:
dashboard.on('initialized', function () {
// Modify to match the relevant date filter title, date filter must already exist and be "From" and "To" date filter
let filterModifiedName = "Date";
// Number of days ago to find date of
// Modify to days ago to calculate "From" date
var daysAgo = 60
filterModifiedName = filterModifiedName.toLowerCase()
// Variable containing the Dashboard filter modified by script
let modifiedFilter = dashboard.filters.$$items.find(function (filterItem) {
return !filterItem.isCascading && filterItem.jaql && filterItem.jaql.title && filterItem.jaql.title.toLowerCase() === filterModifiedName;
});
// If relevant filter has from and to values selected, date filter must already exist and have from and to values
if (modifiedFilter && modifiedFilter.jaql && modifiedFilter.jaql.filter && modifiedFilter.jaql.filter.from && modifiedFilter.jaql.filter.to &&
daysAgo && daysAgo >= 1) {
// "From" date variable, always first day of current year
var fromDate = new Date(new Date().getFullYear(), 0, 1);
// Format date for Sisense date format
var formattedFromDate = fromDate.toISOString().slice(0, 10)
// Set date filter "From" value
modifiedFilter.jaql.filter.from = formattedFromDate
// "To" date variable
var todayDate = new Date
// Format date for Sisense date format
var formattedToDate = todayDate.toISOString().slice(0, 10)
// Set date filter "To" value
modifiedFilter.jaql.filter.to = formattedToDate
}
});
Similarly, this is the code to set the "from" parameter to always use the first day of the current quarter:
dashboard.on('initialized', function () {
// Modify to match the relevant date filter title, date filter must already exist and be "From" and "To" date filter
let filterModifiedName = "Date";
// Number of days ago to find date of
// Modify to days ago to calculate "From" date
var daysAgo = 60
filterModifiedName = filterModifiedName.toLowerCase()
// Variable containing the Dashboard filter modified by script
let modifiedFilter = dashboard.filters.$$items.find(function (filterItem) {
return !filterItem.isCascading && filterItem.jaql && filterItem.jaql.title && filterItem.jaql.title.toLowerCase() === filterModifiedName;
});
// If relevant filter has from and to values selected, date filter must already exist and have from and to values
if (modifiedFilter && modifiedFilter.jaql && modifiedFilter.jaql.filter && modifiedFilter.jaql.filter.from && modifiedFilter.jaql.filter.to &&
daysAgo && daysAgo >= 1) {
const today = new Date();
// Current quarter of year
const quarter = Math.floor((today.getMonth() / 3));
// First day of the current quarter
var fromDate = new Date(today.getFullYear(), quarter * 3, 1);
// Format date for Sisense date format
var formattedFromDate = fromDate.toISOString().slice(0, 10)
// Set date filter "From" value
modifiedFilter.jaql.filter.from = formattedFromDate
// "To" date variable
var todayDate = new Date
// Format date for Sisense date format
var formattedToDate = todayDate.toISOString().slice(0, 10)
// Set date filter "To" value
modifiedFilter.jaql.filter.to = formattedToDate
}
});
A similar format can be used for any date that can be defined programmatically.
If either the "from" or "to" parameter does not need to be defined programmatically the code modifying this parameter can be removed, leaving only the code modifying the other parameter.