3 weeks ago
Hi Community,
I’m building a dashboard in SAP Analytics Cloud – Analytics Designer where the user enters a year (e.g. 2025) and the app should immediately show:
It means, that the user can input the current year in the input field, our script calculates the prior and prior-prior year and gives us the results.
What I already have:
An InputField for the year
ds.setDimensionFilter("BDATU_JAHR", { values: [year, year-1, year-2] });
Script that removes all measures from the Numeric Point and adds two IDs:
chart.addMeasure("ValueAxis", "BG_2025"); chart.addMeasure("ValueAxis2", "BG_2024");
–> works fine if the Restricted Measures BG_2025, BG_2024, … already exist.
Pain point:
I don’t want to create a new Restricted Measure every single year manually. I had the following idea:
Have three reusable Restricted Measures (current year, prior, prior-prior). Change their year filter (value in the dimension, used for the restriction) dynamically in script or swap them in/out without maintaining dozens of hard-coded IDs? My question is: Can I simply create 3 restricted measures: current year, prior year and prior-prior year and just change the dimension-value (2025, 2024, 2023 and so on), using scripting?
My code, that I have created so far looks like that. Here I just works with different restricted measures for each year, that I created manually. I do not want 2025, 2024, 2023, but rather: : current year, prior year and prior-prior year, which 'filters' (years) will be changed dynamically.
/* ───────── 1. Wert aus dem Eingabefeld holen ───────── */
var actualYear = InputField_1.getValue();
var current_date = new Date().getFullYear();
// Zeichenfolge in das Datum konvertieren
var yearNum = ConvertUtils.stringToNumber(actualYear);
if (yearNum >= 1931 && yearNum <= current_date) {
// Aktuelles, -Vor und -Vorvorjahr berechnen
VariableAktualYear = yearNum;
VariablePastYear = yearNum - 1;
VariablePastPastYear = yearNum - 2;
var msg_1 = VariableAktualYear.toString();
var msg_2 = VariablePastYear.toString();
var msg_3 = VariablePastPastYear.toString();
/* Daten quelle des Charts holen und den modifizieren */
var ds = Chart_1.getDataSource();
ds.setDimensionFilter("BDATU_JAHR", {values: [msg_1, msg_2, msg_3]});
/* ───────── 2. Measures tauschen ───────── */
//var measAct = "BG_" + msg_1;
//var measPrev = "BG_" + msg_2;
//Application.showMessage(ApplicationMessageType.Info, measAct);
//Application.showMessage(ApplicationMessageType.Info, measPrev);
// 23735103-3540-4518-3856-333700453218 - Jahr 2025
// 42543347-2523-4283-3882-314686436141 - jahr 2024
// 32937114-7011-4110-3663-482995517414 - Jahr 2023
// 29229304-1622-4153-3546-241342908683 - Jahr 2022
var idMap = {
2025 : "23735103-3540-4518-3856-333700453218",
2024 : "42543347-2523-4283-3882-314686436141",
2023 : "32937114-7011-4110-3663-482995517414",
2022 : "29229304-1622-4153-3546-241342908683"
};
var measAct = idMap[msg_1];
var measPrev = idMap[msg_2];
var chart = Chart_4;
var measures = Chart_4.getMeasures(Feed.ValueAxis);
for (var i = 0; i < measures.length; i++) {
chart.removeMeasure(measures[i], Feed.ValueAxis);
Application.showMessage(ApplicationMessageType.Info, 'Removed: ' + measures[i]);
}
Chart_4.addMeasure(measAct, Feed.ValueAxis);
Chart_4.addMeasure(measPrev, Feed.ValueAxis);
/* ───────── 2. Measures tauschen ───────── */
// Check
Application.showMessage(ApplicationMessageType.Info, 'Aktuelles Jahr: '+ msg_1);
Application.showMessage(ApplicationMessageType.Info, 'Vorjahr: '+ msg_2);
Application.showMessage(ApplicationMessageType.Info, 'Vorvorjahr: '+ msg_3);
Application.showMessage(ApplicationMessageType.Success,
'Jahr ' + actualYear + ' übernommen.');
} else {
var msg_4 = current_date.toString();
Application.showMessage(ApplicationMessageType.Error, 'Bitte eine vierstellige Jahreszahl (1931–' + msg_4 + ')');
I hope, you could help me with this issue.
Request clarification before answering.
Have three reusable Restricted Measures (current year, prior, prior-prior). Change their year filter (value in the dimension, used for the restriction) dynamically in script or swap them in/out without maintaining dozens of hard-coded IDs? My question is: Can I simply create 3 restricted measures: current year, prior year and prior-prior year and just change the dimension-value (2025, 2024, 2023 and so on), using scripting?
I think the idea is to make the restricted measures dynamic in terms of year selection. Restricted Measure do have the option to be restricted by Input controls. So you can create the restricted measure using Input control and then set the Input control selection using scripting. The Input controls could be hidden if required. The below API could be used for Input control setting.
setSelectedMembers of InputControlDataSource
Hope this helps !!
Nikhil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
40 | |
15 | |
10 | |
9 | |
5 | |
5 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.