How to apply a custom fixed sort order in a pivot table [Linux]
Introduction
This article demonstrates how to apply a custom fixed sort order to row values in a Sisense Pivot table using a widget script. The solution is tested on Sisense L2026.2.1-c and works for both cloud and on-premises deployments. By following this guide, you can pin specific items to the top of your pivot in a defined sequence, regardless of the default alphabetical or data-driven sort.
Step-by-step guide
1. Prepare your environment
Create a new dashboard or go to an existing dashboard, and create a Pivot widget (or use an existing one).
2. Apply the code
Edit the Pivot widget, go to the three dots menu and Edit Script, and paste the following code:
var classificationOrder = [
"Emdimax",
"Dopglibin",
"Ciptanefar",
"Addimar",
"ABC",
];
var refreshed = false;
// Reset the flag on every genuine data fetch (filter change, sort change, etc.)
// beforequery does NOT fire on the script-triggered sender.refresh()
// when rawQueryResult is already present — only on real new queries.
// You can remove/comment this beforequery event if you want the logic to be
// applied only in the first dashboard-load.
widget.on("beforequery", function (sender, ev) {
refreshed = false;
});
widget.on("domready", function (sender, ev) {
if (sender.rawQueryResult && !refreshed) {
refreshed = true;
var rawData = sender.rawQueryResult;
if (rawData && rawData.data && rawData.data.data) {
var groups = rawData.data.data;
var pinned = [];
classificationOrder.forEach(function (brand) {
for (var g = 0; g < groups.length; g++) {
var group = groups[g];
if (!group.data || !Array.isArray(group.data)) continue;
for (var i = 0; i < group.data.length; i++) {
var val = group.data[i].value ? group.data[i].value.trim() : "";
if (val === brand) {
pinned.push(group.data.splice(i, 1)[0]);
break;
}
}
}
});
if (groups[0] && groups[0].data) {
Array.prototype.unshift.apply(groups[0].data, pinned);
}
sender.refresh();
}
}
});
3. Code explanation
In this example we are defining a classificationOrder array with the exact sequence of row values we want pinned at the top of the pivot. The script listens for the domready event, accesses the raw query result data, extracts matching items from the data groups, and re-inserts them at the top in the defined order.
The refreshed flag ensures sender.refresh() runs only once per data cycle, preventing an infinite loop. This refresh is necessary because Pivot 2.0 splits its render into HTML and raw data — a single pass won't visually apply the reorder.
Additionally, the beforequery event listener resets the refreshed flag whenever a genuine new query is executed (e.g., when the user changes a filter or sort). This is important because beforequery does not fire on the script-triggered sender.refresh() when rawQueryResult is already present — it only fires on real new queries. This means the custom sort is correctly re-applied after every user interaction that fetches fresh data, while still avoiding the infinite refresh loop.
Conclusion
By leveraging the Pivot widget's scripting capabilities and the domready event, you can override the default sort behavior and enforce a fixed custom order for specific row values. This is particularly useful when business logic requires certain items (e.g., key products or categories) to always appear first, regardless of how the underlying data is sorted. The use of beforequery to reset the flag ensures the sort persists through filter and sort changes without manual intervention. The provided script is a flexible starting point — feel free to expand the classificationOrder array with your own values as needed for your use case.
References/Related content
Disclaimer: This post outlines a potential custom workaround for a specific use case or provides instructions regarding a specific task. The solution may not work in all scenarios or Sisense versions, so we strongly recommend testing it in your environment before deployment. If you need further assistance with this, please let us know.