Forum Discussion
rapidbisupport
01-12-2024Data Pipeline
Hi Jim Silutions ,
You can use the following functions to manipulate date filters on a widget or widget panel item calculation (filter in calculation) level in a persistent way on the dashboard level on 'filters changed' event using the following dashboard script:
const dashboardFilterDim = '[DimDate.Date (Calendar)]'
const targetWidgetOid = '65a0accf800ddf004150f2ae'
const widgetFilterDim = '[DimDate.Date (Calendar)]'
const widgetItemTitleName = '(sum([Order Revenue]), [Years in Date1])'
const widgetItemPanelName = 'value'
const widgetItemFilterDim = '[DimDate.Date (Calendar)]'
function getFilterMember(args, filterDim) {
return args.items.find((i) => { return i.jaql.dim === filterDim }).jaql.filter.members
}
function getSameDayLastYear(date) {
const dashboardFilterDate = new Date(date)
dashboardFilterDate.setDate(dashboardFilterDate.getDate() - 365)
const dashboardFilterDateCopy = new Date(date)
while (dashboardFilterDateCopy.getDay() + 1 !== dashboardFilterDate.getDay()) {
console.log(dashboardFilterDateCopy.getDay() , dashboardFilterDate.getDay())
dashboardFilterDate.setDate(dashboardFilterDate.getDate() + 1)
}
return dashboardFilterDate.toISOString().slice(0, 19)
}
function updateWidgetFilter(widget, filterDim, members) {
widget.metadata.panel('filters').items.find((i) => { return i.jaql.dim === filterDim }).jaql.filter.members = members
}
function updateWidgetPanelItemFilter(widget, panelName, itemName, filterDim, members) {
const context = widget.metadata.panel(panelName || 'values').items.find((i) => { return i.jaql.title === itemName }).jaql.context
for (item in context) {
if (context[item].dim === filterDim) {
context[item].filter.members = members
}
}
}
dashboard.on('filterschanged', (e, args) => {
const targetWidget = dashboard.widgets.$$widgets.find((w) => { return w.oid === targetWidgetOid })
if (args.items.some((i) => { return i.jaql.dim !== dashboardFilterDim })) { return }
const dashboardFilterMembers = getFilterMember(args, dashboardFilterDim)
const sameDayLastYear = getSameDayLastYear(dashboardFilterMembers[0])
// Updating a Widget Filter
updateWidgetFilter(targetWidget, widgetFilterDim, [sameDayLastYear])
// Updating a Widget Panel Item Filter (calculated measure)
updateWidgetPanelItemFilter(targetWidget, widgetItemPanelName, widgetItemTitleName, widgetFilterDim, [sameDayLastYear])
// Persisting changes
targetWidget.changesMade('metadata', 'filters')
targetWidget.refresh()
})
The implementation here changes the 'filter' in the 'target widgets' filter panel to the same weekday of the previous year relative to the dashboard filter date, and then changes the calculated filter in the
'(sum([Order Revenue]), [Years in Date1])' item on the 'value' panel on the targetWidget.
You should be able to use these functions to:
- get the filter member from the dashboard,
- get the same day last year,
- update a widget filter given a widget, filterDim and members you want to update and
- update a widget panel item filter, given a widget, panel name, item name, filter dim and the members you want to update.
You'll see the implementation in the body of 'dashboard.on('filterschanged', (e, args) => {'
This implementation should work on any widget, not just BloX controls.
I'm also happy to walk through over a quick call if you'd like - I would genuinely be interested in hearing more about your use case, how you and your customers leverage Sisense more generally and potentially trade/share some notes.
Let me know how you go?