Forum Discussion

Astroraf's avatar
Astroraf
Data Pipeline
09-06-2024
Solved

How to change week date filter to display differently?

When a person chooses a week date filter in Sisense, it is displayed as 33 2023, 23 1998, 02 2008, etc. This is a confusing way to display a filter for a specific week in Sisense. Is there an edit sc...
  • rapidbisupport's avatar
    09-09-2024

    Hey Astroraf ,

     I think you'd have less head/heartache by just creating a dimWeeks and attaching a string in the format you want from a datamodel perspective - but this wouldn't be as tightly integrated into the filters panel (multiple filters, etc.)

    Here is a dashboard script that waits for the weeks in the dropdown to exist, and then changes their text relative to their original text.

    It's a bit of a mess - but you could change the result text by adjusting the getSundayFromWeekNum function at top. Not sure what effect it'll have on performance as it always checks for there to be an element created consistent with the weeks filter, and then when it exists it changes the text values - would be worthwhile testing and iterating before deploying.

    If I had more time, i'd experiement with destroying the observer when not needed anymore - but not sure how that'd work and how the list is generated after the fact.

    // https://thewebdev.info/2021/08/29/how-to-calculate-date-from-week-number-in-javascript/
    function getSundayFromWeekNum(weekNum, year) {
        const sunday = new Date(year, 0, (1 + (weekNum - 1) * 7));
        while (sunday.getDay() !== 0) {
            sunday.setDate(sunday.getDate() - 1);
        }
        return sunday;
    }
    
    dashboard.on('initialized', (d, args) => {
        // Function to update the text format
        function updateWeekFormat() {
            $('.K1ShsxUa7DDQ260o6OLv').find('.custom-checkbox__text').each((i, element) => {
                if (element.mod) { return }
                element.mod = true
                const text = element.textContent;
                const [week, year] = text.split(' ');
                element.textContent = getSundayFromWeekNum(week, year).toLocaleDateString('en-us')
            });
        }
    
        // Create a MutationObserver to watch for changes in the dropdown
        const observer = new MutationObserver((mutationsList, observer) => {
            if ($('.K1ShsxUa7DDQ260o6OLv').find('.custom-checkbox__text').length === 0) { return }
            for (const mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    updateWeekFormat();
                }
            }
        });
    
        // Start observing the dropdown for changes
        observer.observe(document, { childList: true, subtree: true });
    
        // Initial call to update the format if elements are already present
        updateWeekFormat();
    })
    

    Let me know how you go?

    Thanks,

    Daniel

    RAPID BI

    [email protected]

    RAPID BI - Sisense Professional Services | Implementations | Custom Add-ons