⏱️ 3 minute read
Here is a ready-to-use widget script that creates a custom date filter based on the current date and the last day of a future month.
Replace the variables highlighted in bold with the appropriate values for your use case.
Custom Filter for Today Plus Next 2 Months
The following example creates a filter based on the current date plus the next two months. You may adjust the number of months by replacing the variable highlighted in bold with the appropriate value for your use case.
// Custom Sisense filter: from today through the end of two months out
var today = new Date();
today.setHours(0, 0, 0, 0);
// Calculate end of two months from now
var endDate = new Date(today);
endDate.setMonth(endDate.getMonth() + 2); //specify number of months
// Move to the last day of that month
endDate = new Date(endDate.getFullYear(), endDate.getMonth() + 1, 0);
endDate.setHours(23, 59, 59, 999);
// Apply dynamic filter on [Date.Date]
dashboard.filters.update({
jaql: {
dim: "[Date.Date]",
datatype: "datetime",
filter: {
from: today.toISOString(),
to: endDate.toISOString(),
include: true
}
}
});
The filter Start Date increments based on the current date ("today") while the End Date remains static based on the script value. To edit custom filter, access Edit Script, update the script code, save and refresh to see the changes.
Rendered result: Script creates the filter shown in right panel based on custom code.
Year-to-date filter
widget.on('beforequery', function (se, ev) {
var dateDim = "[Date.Date]";
var now = new Date();
function getIsoDate(dateObj) {
var offset = dateObj.getTimezoneOffset() * 60000;
var localTime = new Date(dateObj.getTime() - offset);
return localTime.toISOString().slice(0, 10);
}var startStr = getIsoDate(new Date(now.getFullYear(), 0, 1)); // Jan 1
var endStr = getIsoDate(now); // Today
ev.query.metadata = ev.query.metadata.filter(function(item) {
var isTarget = (item.jaql.dim === dateDim);
var isBackground = (item.panel === 'scope');
return !(isTarget && isBackground);
});ev.query.metadata.push({
"jaql": {
"dim": dateDim,
"datatype": "datetime",
"level": "days", // Must be 'days' to filter 2026-01-01 to 2026-01-29
"filter": {
"from": startStr,
"to": endStr
}
},
"panel": "scope" // 'scope' ensures it applies in the background
});
});
Custom-to-date filter
This date filter captures all records from a specified static date up to the current date.
widget.on('beforequery', function (se, ev) {
var dateDim = "[Date.Date]";
var customStartDateString = "2026-04-15"; // specify the start date here
var now = new Date();
function getIsoDate(dateObj) {
var offset = dateObj.getTimezoneOffset() * 60000;
var localTime = new Date(dateObj.getTime() - offset);
return localTime.toISOString().slice(0, 10);
}var startStr = customStartDateString;
var endStr = getIsoDate(now);
ev.query.metadata = ev.query.metadata.filter(function(item) {
var isTarget = (item.jaql.dim === dateDim);
var isBackground = (item.panel === 'scope');
return !(isTarget && isBackground);
});ev.query.metadata.push({
"jaql": {
"dim": dateDim,
"datatype": "datetime",
"level": "days", // Crucial for Sisense to process the YYYY-MM-DD format properly
"filter": {
"from": startStr,
"to": endStr
}
},
"panel": "scope" // 'scope' ensures it applies safely in the background
});
});
Related Articles
Comments
0 comments
Article is closed for comments.