Interactive Time Period Comparison with BloX & Custom Actions
/* Getting the BloX Widget which shows the KPI value and get the formula's elements
i.e., the measured value elements */
const widgetId = payload.data.widgetToModify[0];
const widget = ((prism.activeDashboard.widgets.$$widgets).filter(widget => widget.oid == widgetId))[0];
const monthField = payload.data.monthField;
const monthCompareField = payload.data.monthCompareField;
var formulaContext = widget.metadata.panels[1].items[0].jaql.context;
//console.log(formulaContext);
// Getting the Dropdown values
var m = (payload.data.month) + "-01T00:00:00";
var mc = (payload.data.monthCompare) + "-01T00:00:00";
//console.log(m);
//console.log(mc);
//Setting the value of triggered_at as the SECOND Dropdown Value
var f1 = Object.keys(formulaContext).find(key => formulaContext[key].column === monthField);
formulaContext[f1].filter.members[0] = m;
//Setting the value of triggered_at_compare as the FIRST Dropdown Value
var f2 = Object.keys(formulaContext).find(key => formulaContext[key].column === monthCompareField)
formulaContext[f2].filter.members[0] = mc;
//Constructing and Setting the Subtitle text on the KPI Widget
var subTitle = 'Comparing ' + ((payload.data.month)) + " vs " + ((payload.data.monthCompare));
widget.style.currentCard.body[0].items.filter(i => i.id == "blox_text")[0]
.text = subTitle;
//Saving the changes and refreshing the widget
widget.changesMade('BloX-Actions', ['metadata', 'style']);
widget.refresh();
- 3 - Output Widget Configuration: (BloX Template file attached: momCompareDsply.json)
- Calculates the percentage change in the KPI between M1 and M2 using the formula below: the formula is based on calculating the % change in Engagement Rate, where
The above formula is translated into Sisense formula expression using the measured value filters.
Sisense Formula Expression using Measured Value Filters to calculate the % Change in Rate
This formula is added to the "Values" panel of the Display BloX Widget and it is named as "Engagement Rate - % Change"
Values Panel containing the Value on the Display Widget
- Also includes a subtext to show the compared periods, which reads:This is auto-generated by the BloX Action code using the months selected on the dropdowns.
- Calculates the percentage change in the KPI between M1 and M2 using the formula below: the formula is based on calculating the % change in Engagement Rate, where
Step-by-Step Guide
1. Create the Input Widget
- Add a BloX widget to your dashboard.
- Use the attached BloX template - momCompareInput.json.
- Add the two date fields to the "Items" panel. Choose "Months" as the date granularity and Format as yyyy-MM. Set the title of the added date fields as "monthCompare" and "month"
Month Format of the Date Fields on the Input Widget
Update the three input parameters of the Action Block as described in the "How it works" section. (If you don't know the widgetId of the Display BloX Widget yet, come back and update that later)
2. Set Up the Custom BloX Action
- From One of the BloX widgets (as Custom Actions are common to all BloX Widgets in your Sisense instance), go to the "Action" tab on the Design panel. Use the three-dot to access the menu and choose "Create Action"
Creating a new BloX Action
- The name of the action should be "momRateCompare"(If you're using a different name for the action, then the Input Widget's Action block has to be updated accordingly).
- Use JavaScript (see above) to build the logic required.
- Click "Next" and then click "Create" on the subsequent screen.
3. Configure the Display Widget
- Add another BloX widget to display the result.
- For this, use the attached BloX template - momCompareDsply.json.
- In the "Values" panel of the BloX widget, create the formula that calculates the % change of the required KPI using measured value filters. Ensure you use both the date fields in your model, exactly how it was explained in the "How it works" section. The Value should have the title - "% Change" and the format should be set to "Percent". (If you're using a different name for the value, then the Display Widget's Editor has to be updated accordingly - Ln.35)
- Modify the displayed Title to what matches your KPI. In the Editor, modify Ln.26 to provide the new title.
- Note: Conditional Formatting has been used to display the % Change value in Red when the value is positive and Green when the value is negative. Do change the logic, if needed.
For reference, I have attached the dashboard that I built to demonstrate this usecase - MoMSelector-MeasuredValueFilter-BloX.dash.txt (download and rename it to MeasuredValueFilter-BloX.dash before importing). The dashboard will only work if you have the datasource - "MoM Compare BloX" (you can download the .sdata from the drive link and import it into your Sisense).
Conclusion
This use case demonstrates the power of BloX widgets in creating interactive and dynamic dashboards in Sisense. You can extend this use case to support other Date granularities like Years, Quarters, Weeks, and Days by simply tweaking the action code and BloX setup by a little bit. By enabling users to select and compare different periods, you can provide more flexible and insightful data analysis for any KPI.
Feel free to ask any questions or share your experiences with similar use cases in the comments below!
DRay
·2 years agoHi ramansingh89.
Thank you for reaching out. This question is better suited for the Help and How-To forum. If you still need help with this, please submit there.
Thank you.
Sisense User
OP2 years agoramansingh89 - This is more of a "function" related question than a BloX one. Have you tried using the DiffPastPeriod function to calculate the difference from the last period and the "period" context is grabbed from the slicer or the filter 's date granularity. If you filter for last month, then the function assigns "Month" to the date context and does a "DiffPastMonth".
Hope this helps. If you still need help on this one, please reach out to your Customer Success Manager and they can get you in touch with the FES team of Sisense who can help you to implement this.
Gowtham Senthilkumar
ramansingh89
·2 years agotagging Anonymous
ramansingh89
·2 years agoHi Anonymous
We are trying to build a KPI ndicator widget that will show the current value of KPI (based on user date filter) + also shows % change with 'previous period' which should be the dynamically adjusted based on user date values. for example
Is this something that can be done in Sisense?