cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SAP Analytics Cloud: How do I build dynamic “Current-Year vs. Prior-Year” KPIs

denyszhhuta
Discoverer
0 Kudos
279


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:

  • Bruttogewicht Lieferung (current year)
  • Bruttogewicht Lieferung (prior year)
  • Bruttogewicht Lieferung (prior-prior year)
  • an automatic %-deviation between current year and prior year (Numerical Point widget)

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

  • A Numeric Point (Chart_4) with two feeds
  • ValueAxis → Primary value
  • ValueAxis2 → Comparison value
  • denys_zhhuta_1-1750141972721.png

     

  • Working script that filters all charts to the three selected years:
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. 

 

Accepted Solutions (0)

Answers (1)

Answers (1)

N1kh1l
Active Contributor
0 Kudos

@denyszhhuta 

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

https://help.sap.com/doc/1639cb9ccaa54b2592224df577abe822/release/en-US/index.html#InputControlDataS...

Hope this helps !!

Nikhil

denyszhhuta
Discoverer
0 Kudos
So you mean, Instead of binding the year filter of your restricted measures to "2025," etc., I should bind it to an input control. If I change the value of this input control later in the script, the restricted measure will automatically jump to the new year?
N1kh1l
Active Contributor
0 Kudos
denys_zhhuta , Yes. If you bind to a specific year then its not possible to change the Year filter using scripting. It kinda becomes static. BUt with Input control you can achieve dynamic assignment by changing the value of input control and the measure will restricted as per the value of Input control.
denyszhhuta
Discoverer
0 Kudos
@N1kh1l , but is it possible then to create a deviation in the numerical point chart? I do not quite understand your idea. If I create the restricted measure, that I can only choose the dimension and the value from this dimension. There is not options to restrict the measure using values from Input Control.