cancel
Showing results for 
Search instead for 
Did you mean: 

How to change week date filter to display differently?

Astroraf
10 - ETL
10 - ETL

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 script or other option to display the weeks as:

"09/01-09/07 2024" 

"07/08 - 07/14 2005"

etc.

 

@DRay  @gowtham_sisense @rapidbisupport 

1 ACCEPTED SOLUTION

rapidbisupport
11 - Data Pipeline
11 - Data Pipeline

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

 

View solution in original post

4 REPLIES 4

Astroraf
10 - ETL
10 - ETL

 

 

Hi @DRay ,

I am specifically trying to get the date filter in the first section to be changed to the format above, the provided answers don't have necessarily a specific solution for this format. The image below is the specific filter I am trying to format. 

Screen Shot 2024-09-06 at 19.04.33.png

DRay
Community Team Member
Community Team Member

Gotcha. Let's see if one of the more qualified members of the community can provide a solution.

David Raynor (DRay)

rapidbisupport
11 - Data Pipeline
11 - Data Pipeline

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