logo

Filling in KPI row values using a script

Sometimes you may need to write data to a row KPI using a script.

For example, there is a Revenue KPI, which is expanded to a row by Branch Office object.

Script text:

public void setDimensionValue(Context context) 
{ 
    // Get the KPI by name 
    var indicator = IndicatorManager.Instance.FindByName("Revenue"); 
    // Create the dates the will indicate the KPI period      
    Pair<DateTime, DateTime> period = null;             
    // Service for working with periodicities 
    var periodicityService = Locator.GetService<EleWise.ELMA.KPI.Common.Interfaces.IPeriodicityService>(); 
    if (periodicityService != null) 
    { 
     // Identify the period for writing values 
     period = periodicityService.GetPeriod(indicator.Periodicity, DateTime.Now); 
    }         
           
    // A list that will store the row items 
    var dimensions = new List<IKpiValueDimensionItem>(); 
           
    //Check that the KPI is expanded to a row 
    if (indicator.ExpandedInSeries && indicator.DimensionUid.HasValue) 
    { 
  dimensions = indicator.GetDimensionItems()
   .Where(k => k.StartDate <= period.Second && (k.EndDate == null || k.EndDate >= period.First)) 
   .ToList();
     } 
           
    // select the required row item, e.g. by Id 
    var dimension = dimensions.FirstOrDefault(d => d.Id == 3); 
  
    if(dimension != null){ 
    // write the required value 
    IndicatorsService.Instance.SetPeriodValue(indicator, period, 123.45, "writing a value from the process", null, dimension); 
    } 
}