DatePeriod: MDCalculationFormulaDatePeriod;
The DatePeriod property determines a calendar period that is used to shift a date using the Shift function.
Executing the example requires that the repository contains multidimensional calculation on DB server with the MDCalc_1 identifier.
Sub Main;
Var
MB: IMetabase;
MDInst: IMDCalculationInstance;
Formulas: IMDCalculationFormulas;
Formula: IMDCalculationFormula;
FormulaExpression: IMDCalculationFormulaExpression;
Source: IMDCalculationSourceInstance;
Dest: IMDCalculationDestinationInstance;
Slices: IMDCalculationSlicesInstance;
Operand: IMDCalculationFormulaOperand;
DestCoo, SourceCoo: IMatrixCoord;
ElementKey, SourceElKey: IMDCalculationFormulaElementKey;
CoordOperand: IMDCalculationFormulaElement;
ConstOperand: IMDCalculationFormulaConstantValue;
BinaryOperand: IMDCalculationFormulaBinaryOperation;
FuncCallOperand: IMDCalculationFormulaFunctionCall;
DelimiterOperand: IMDCalculationFormulaDelimiter;
DatePeriodOperand: IMDCalculationFormulaDatePeriod;
i: Integer;
Begin
MB := MetabaseClass.Active;
MDInst := MB.ItemById("MDCalc_1").Open(Null) As IMDCalculationInstance;
Formulas := MDInst.CreateFormulas;
Formula := Formulas.Add;
FormulaExpression := Formula.Expression;
Source := MDInst.Sources.Item(0);
Slices := Source.Slices;
//Assign as an operand - coordinate in the source
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.Element);
SourceCoo := Source.NewCoord;
For i := 0 To Slices.Count - 1 Do
SourceCoo.Item(i) := 0;
End For;
SourceElKey := Source.CoordToKey(SourceCoo);
SourceElKey.FactIndex := 0;
CoordOperand := Operand As IMDCalculationFormulaElement;
CoordOperand.Key := SourceElKey;
FormulaExpression.InsertItem(Operand);
//Assign symbol * as an operand
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.BinaryOperation);
BinaryOperand := Operand As IMDCalculationFormulaBinaryOperation;
BinaryOperand.BinaryOperation := MDCalculationFormulaBinaryOperation.Mul;
FormulaExpression.InsertItem(Operand);
//Assign constant 3.14 as an operand
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.ConstantValue);
ConstOperand := Operand As IMDCalculationFormulaConstantValue;
ConstOperand.ConstantValue := 3.14;
FormulaExpression.InsertItem(Operand);
//Coordinate, by which formula is written into the consumer
Dest := MDInst.Destination;
DestCoo := Dest.NewCoord;
Slices := Dest.Slices;
For i := 0 To Slices.Count - 1 Do
DestCoo.Item(i) := 0;
End For;
ElementKey := Dest.CoordToKey(DestCoo);
ElementKey.FactIndex := 0;
//Writing formula by first coordinate
MDInst.WriteFormulas(ElementKey, Formulas);
//Next is adding Shift function to the formula and writing by second coordinate
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.FunctionCall);
FuncCallOperand := Operand As IMDCalculationFormulaFunctionCall;
FuncCallOperand.FunctionCall := MDCalculationFormulaFunctionCall.Shift;
//Inserting Shift function in first position
FormulaExpression.InsertItem(Operand, 0);
//Assign separator , as an operand
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.Delimiter);
DelimiterOperand := Operand As IMDCalculationFormulaDelimiter;
DelimiterOperand.Delimiter := MDCalculationFormulaDelimiter.Comma;
//Inserting in third position ,
FormulaExpression.InsertItem(Operand, 2);
//Inserting Day
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.DatePeriod);
DatePeriodOperand := Operand As IMDCalculationFormulaDatePeriod;
DatePeriodOperand.DatePeriod := MDCalculationFormulaDatePeriod.Day;
FormulaExpression.InsertItem(Operand, 3);
//Inserting ,
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.Delimiter);
DelimiterOperand := Operand As IMDCalculationFormulaDelimiter;
DelimiterOperand.Delimiter := MDCalculationFormulaDelimiter.Comma;
FormulaExpression.InsertItem(Operand, 4);
//inserting in last position 1
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.ConstantValue);
ConstOperand := Operand As IMDCalculationFormulaConstantValue;
ConstOperand.ConstantValue := 1;
FormulaExpression.InsertItem(Operand, 5);
//Inserting in last position )
Operand := FormulaExpression.CreateItem(MDCalculationFormulaOperandKind.Delimiter);
DelimiterOperand := Operand As IMDCalculationFormulaDelimiter;
DelimiterOperand.Delimiter := MDCalculationFormulaDelimiter.RightPar;
FormulaExpression.InsertItem(Operand, 6);
Dest := MDInst.Destination;
DestCoo := Dest.NewCoord;
Slices := Dest.Slices;
For i := 0 To Slices.Count - 1 Do
DestCoo.Item(i) := 1;
End For;
ElementKey := Dest.CoordToKey(DestCoo);
ElementKey.FactIndex := 0;
//Writing formula by second coordinate
MDInst.WriteFormulas(ElementKey, Formulas);
End Sub Main;
After executing the example the formula is set for the specified data consumer element. The value by coordinate in the data consumer is calculated by the formula: value by coordinate in the source is multiplied by 3.14. The value by the second coordinate in the data consumer is calculated by the same formula, but coordinate value in the data source is taken by one day shift according to calendar dimension.
See also: