Forum Discussion

Jake_Raz's avatar
01-31-2024
Solved

Add a Plus Sign to positive values in a Pivot Table

Hello! I was hoping there was some sort of script I can add to a Pivot Table widget that will make positive numbers for a particular value display a plus sign (+) in front of them. Consider a pivot t...
  • rapidbisupport's avatar
    rapidbisupport
    02-05-2024

    Hi Jake_Raz ,

    The solution provided was for a linux instance.

    This solution uses Changing Color Of Specific Cells In A Pivot Based On Textual Condition (sisense.com) as inspiration in order to achieve the same result as we have above using what we have access to in Windows Pivot.

    You should be able to use the widget script below:

    // Credit intapiuser
    // https://community.sisense.com/t5/knowledge/changing-color-of-specific-cells-in-a-pivot-based-on-textual/ta-p/9201
    
    var columns = [4]; //select the date columns that should be transformed
    
    widget.on('ready', function (se, ev) {
        $.each(columns, function (index, value) {
            num = $("tbody tr:first").children().length - value;
            var e = element;
            var w = widget;
            var d = dashboard;
    
            $("tbody tr", e).not('.wrapper, .p-head-content').each(function () {
                var cell = $(this).children().last();
                for (var a = 0; a < num; a++) {
                    cell = cell.prev();
                }
    
                var cell_value = cell.text()
                // make cells italic
                $(cell).css("font-style", "italic");
                if (cell_value.includes('-')) {
                    // if the cell has '-' then don't add a +, but make the text red.
                    $(cell).css("color", 'red');
                }
                else {
                    // if the cell doesn't have a '-', then add a + to the front.
                    cell.text(`+${cell_value}`)
                }
            });
        });
    });
    

    I've commented the code above, so you should be able to adjust the styling of the cell on the other end (e.g. I have made negative numbers red, and all numbers italic based on your initial post)

    Let me know how you go?

    Thanks,

    Daniel

    RAPID BI

    [email protected]

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

  • rapidbisupport's avatar
    rapidbisupport
    02-08-2024

    Hi Jake_Raz ,

    Great to hear it! Have no idea why column 6 was the one here, but good catch!

    Here is a fix for the alignment issue:

    // https://community.sisense.com/t5/knowledge/changing-color-of-specific-cells-in-a-pivot-based-on-textual/ta-p/9201
    
    var columns = [4]; //select the date columns that should be transformed
    
    widget.on('ready', function (se, ev) {
        $.each(columns, function (index, value) {
            num = $("tbody tr:first").children().length - value;
            var e = element;
            var w = widget;
            var d = dashboard;
    
            $("tbody tr", e).not('.wrapper, .p-head-content').each(function () {
                var cell = $(this).children().last();
                for (var a = 0; a < num; a++) {
                    cell = cell.prev();
                }
    
                var cell_value = cell.text()
                // make cells italic
                $(cell).css("font-style", "italic");
                if (cell_value.includes('-')) {
                    // if the cell has '-' then don't add a +, but make the text red.
                    $(cell).css("color", 'red');
                }
                else {
                    // if the cell doesn't have a '-', then add a + to the front.
                    cell.html(`<div class="p-value">+${cell_value}</div>`)
                }
            });
        });
    });
    

    I changed the way we're modifying the cell when adding the '+', this should apply the appropriate class with css product side that's centering the value in the cell appropriately.

    Feel free to reach out if you'd like to see any of our other work (styling dashboards, custom addons, etc.). If you're open to it, i'd love to hear more about how you're currently leveraging Sisense within your organization and maybe compare notes?

    Thanks,

    Daniel

    RAPID BI

    [email protected]

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