Forum Discussion
Hi Silutions ,
You can experiment with the following approach:
- Include the same date filter as widget filter within the Blox where you want to implement the 'same day of the week a year ago' filter. Select any one random date in this widget filter (this step is important). We will replace the selected date with the calculated date using script.
- Apply the provided widget script to the Blox. Ensure to update the variables
dashFilterName
andwidgetFilterName
with titles of dashboard date filter and widget date filter.
widget.on('beforequery', function(sender, args){
const dashFilterName = 'Date'
const widgetFilterName = 'Date'
const dashFilter = sender.dashboard.filters.$$items.find(el => el.jaql.title === dashFilterName);
const widgetFilter = args.query.metadata.find(el => el.jaql.title === widgetFilterName);
if (dashFilter && dashFilter.jaql.filter.members && dashFilter.jaql.filter.members.length > 0 &&
widgetFilter && widgetFilter.jaql.filter) {
let dashFilterDate = new Date(dashFilter.jaql.filter.members[0]);
//Get Week number of selected date
const dashFilterDateCopy = new Date(dashFilterDate);
dashFilterDateCopy.setDate(dashFilterDateCopy.getDate() + 4 - (dashFilterDateCopy.getDay() || 7));
const year = dashFilterDateCopy.getFullYear();
const dayOfYear = Math.floor((dashFilterDateCopy - new Date(year, 0, 1)) / 86400000);
const dashFilterWeekNumber = Math.ceil((dayOfYear + 1) / 7);
const dayOfWeek = dashFilterDate.getDay();
//get first week of last year
lastYearDate = new Date(dashFilterDate.getFullYear() - 1, 0, 4);
lastYearDayOfWeek = lastYearDate.getDay();
//set first day of first week
lastYearDate.setDate(lastYearDate.getDate() - lastYearDayOfWeek)
//set same week a year ago
lastYearDate.setDate((lastYearDate.getDate() + (dashFilterWeekNumber) * 7));
//set same day of week a year ago
lastYearDate.setDate(lastYearDate.getDate() - (dayOfWeek === 0 ? 0 : 7 - dayOfWeek));
lastYearDateStr = `${lastYearDate.getFullYear()}-${('0' + (lastYearDate.getMonth() + 1)).slice(-2)}-${('0' + lastYearDate.getDate()).slice(-2)}T00:00:00`;
widgetFilter.jaql.filter = {
explicit: true,
multiSelection: true,
members: [
lastYearDateStr
]
}
}
})
Result:
Let us know if this your expected result.
If you need to apply the filter only for a specific formula within Blox, you can update the script to replace date in formula instead of replacing widget filter.
Feel free to reach out if you have further questions, we're always happy to help 🙂
[email protected]
Paldi Solutions, Number #1 Sisense Plugins Developer
- Silutions12-22-2023ETL
Benji,
Thank you for this. I have tried on BloX and with Pivots. I followed your instructions. No joy. Can you send me your BloX templates from your example? With those I should be able to figure it out.
Regards, Jim
- Benji_PaldiTeam12-26-2023Data Pipeline
Attaching the Blox template for your reference.
Also here is the summary of steps:
- Add Date dashboard filter (level = Days). Make sure to select only one day at a time.
- Create Blox with attached template.
- Add Date widget filter to Blox (level = Days). and select any random date
- For the attached Blox template, add Date dimension under 'Items' panel. One with level Days and other with level Weeks
- Add widget script (from my previous reply) to Blox widget.
Please let me know if the solution works.
Feel free to reach out if you have further questions, we're always happy to help
[email protected]
Paldi Solutions, Number #1 Sisense Plugins Developer - Add Date dashboard filter (level = Days). Make sure to select only one day at a time.
- Silutions12-22-2023ETL
Benji,
Also, we do have some filters that are in calculations. It's not clear to me how to change the code to point to the calculation filter instead of the widget filter. Can you explain?
Thanks again, Jim