Expression: IExpression;
The Expression property determines expression to calculate value of calculated field.
The correct work of calculated field is possible if the repository is specified for data consumer or data provider, that is, the IDtObject.Metabase property is set.
Executing the example requires that the repository contains a time series data base with the TSDB_IMPORT identifier. This time series database must contain attributes of time series with the CITY and INDICATOR identifiers. The attributes must be links to dictionaries.
The file system is considered to have the C:\Import.xlsx file containing a data sheet named Sheet1.
Add links to the Cubes, Dal, Db, Dimensions, Dt, Metabase, Rds system assemblies.
Contents of the Sheet 1 sheet in the C:\Import.xlsx file
Sub UserProc;
Var
mb: IMetabase;
desc: IMetabaseObjectDescriptor;
importObject: IImportRequestDefinition;
createInfo: IMetabaseObjectCreateInfo;
params: IImportRequestProviderParams;
importBinding: ICubeMetaLoaderBinding;
rdsDictionary: IRdsDictionary;
excel: IDtExcelProviderEx;
instance: IImportRequestInstance;
rub: IRubricator;
Atts: IMetaAttributes;
Fields: IDtFieldDefinitions;
FieldDef: IDtFieldDefinition;
Begin
// Get repository
mb := MetabaseClass.Active;
// Get time series database
desc := mb.ItemById("TSDB_IMPORT");
rub := desc.Bind As IRubricator;
// Get time series attributes
Atts := rub.Facts.Attributes;
// Create import object
createInfo := mb.CreateCreateInfo;
createInfo.ClassId := MetabaseObjectClass.KE_CLASS_IMPORTREQUEST;
createInfo.Name := "Import with calculated field";
createInfo.KeepEdit := True;
createInfo.Permanent := False;
createInfo.Parent := desc;
// Create object for import of time series
importObject := mb.CreateObject(createInfo) As IImportRequestDefinition;
// Specify source type
importObject.SourceType := ImportRequestSourceType.Provider;
// Specify time series database, to which data will be imported
importObject.DestinationRubricator := desc.Bind As IRubricator;
// Set import options
params := importObject.ProviderParams;
// Specify that errors on import are not allowed
(params As ICubeMetaLoader).Log.MaxErrors := -1;
// Specify import options for the CITY attribute
desc := Atts.FindById("CITY").ValuesObject;
rdsDictionary := desc.Bind As IRdsDictionary;
importBinding := params.Bindings.Add;
importBinding.BindingType := CubeMetaLoaderBindingType.Attribute;
importBinding.Attribute := "CITY";
importBinding.Dimension := rdsDictionary As IDimensionModel;
importBinding.KeyAttribute := rdsDictionary.Attributes.Key.Id;
importBinding.Index := rdsDictionary.UniqueKeys.Item(0).Id;
// Attribute value will be set as a constant value
importBinding.FieldType := CubeMetaLoaderFieldType.ConstValue;
// Value will be taken drom the dictionary, to which this attribute refers
importBinding.FieldValue := (rdsDictionary.Open(Null) As IDimInstance).Elements.Id(0);
// Specify import options for the INDICATOR attribute
desc := Atts.FindById("INDICATOR").ValuesObject;
rdsDictionary := desc.Bind As IRdsDictionary;
importBinding := params.Bindings.Add;
importBinding.BindingType := CubeMetaLoaderBindingType.Attribute;
importBinding.Attribute := "INDICATOR";
importBinding.Dimension := rdsDictionary As IDimensionModel;
importBinding.KeyAttribute := rdsDictionary.Attributes.Key.Id;
importBinding.Index := rdsDictionary.UniqueKeys.Item(0).Id;
// Value will be taken from data source field
importBinding.FieldType := CubeMetaLoaderFieldType.Index;
// Specify index of the field, from which data will be taken
importBinding.Field := "0";
// Specify import options for the UNIT attribute
desc := Atts.FindById("UNIT").ValuesObject;
rdsDictionary := desc.Bind As IRdsDictionary;
importBinding := params.Bindings.Add;
importBinding.BindingType := CubeMetaLoaderBindingType.Unit;
importBinding.Dimension := rdsDictionary As IDimensionModel;
importBinding.KeyAttribute := rdsDictionary.Attributes.Key.Id;
importBinding.Index := rdsDictionary.UniqueKeys.Item(0).Id;
// Attribute value will be taken as a constant value
importBinding.FieldType := CubeMetaLoaderFieldType.ConstValue;
// Value will be taken from the dictionary, to which this attribute refers
importBinding.FieldValue := (rdsDictionary.Open(Null) As IDimInstance).Elements.Id(0);
// Set calendar export options
importBinding := params.Bindings.Add;
importBinding.BindingType := CubeMetaLoaderBindingType.Calendar;
importBinding.ByColumns := False;
// Set calendar data format
importBinding.CalendarDateFormatEx(DimCalendarLevel.Year) := "$Year$";
importBinding.CalendarDateFormatEx(DimCalendarLevel.HalfYear) := "$Year$A$Halfyear$";
importBinding.CalendarDateFormatEx(DimCalendarLevel.Quarter) := "$Year$Q$Quarter$";
importBinding.CalendarOptions.Levels := DimCalendarLevelSet.Year Or DimCalendarLevelSet.HalfYear Or DimCalendarLevelSet.Quarter;
// Value will be taken from data source field
importBinding.FieldType := CubeMetaLoaderFieldType.Index;
// Specify index of the field, from which data will be taken
importBinding.Field := "1";
// Specify export options for time series values
importBinding := params.Bindings.Add;
importBinding.BindingType := CubeMetaLoaderBindingType.Value;
// Value will be taken from data source field
importBinding.FieldType := CubeMetaLoaderFieldType.Index;
// Specify index of the field, from which data will be taken
importBinding.Field := "4";
// Create object for import from Excel
excel := New DtExcelProviderEx.Create;
// Specify provider file parameters
excel.File := "C:\Import.xlsx";
excel.Sheet := "Sheet1";
excel.Format := "XLSX";
excel.HasHeader := True;
excel.Metabase := MB;
// Load fields from provider file
excel.FieldsFromFile;
// Get loaded fields
Fields := excel.Fields;
// Specify data types for fields with the 2 and 3 indexes
FieldDef := Fields.Item(2);
FieldDef.DataType := DbDataType.Float;
FieldDef := Fields.Item(3);
FieldDef.DataType := DbDataType.Float;
// Add a new calculated field,
// from which time series values will be loaded
FieldDef := Fields.Add;
// Set expression for field value calculation
FieldDef.Expression.AsString := Fields.Item(2).Name + '*' + Fields.Item(3).Name;
// Specify the created object for import from Excel as a data source for import
importObject.ProviderParams.Provider := excel As IDatasetDataProvider;
// Import data
instance := (importObject As IMetabaseObject).Open(Null) As IImportRequestInstance;
instance.LoadData;
End Sub UserProc;
After executing the example data from the C:\Import.xlsx file is loaded in the time series data base. Values of time series will be calculated as product of the Value1 and Value2 fields of the file.
See also: