Working with Data of Open Cube

Executing the example requires that the repository contains an automatic cube with the Cube_1 identifier. There cube contains three dimensions. Dimensions are based on the following dictionaries:

Sub GetCubeData;
Var
    MB: IMetabase;
    CubInst: ICubeInstance;
    DestInst: ICubeInstanceDestination;
    DestMod: ICubeModelDestination;
    DimsInst: ICubeInstanceDimensions;
    DimsMod: ICubeModelDimensions;
    Dim1, Dim2, Dim3: IDimensionModel;
    DimInst: IDimInstance;
    AttrInst: IDimAttributeInstance;
    DimSS: IDimSelectionSet;
    Mat: IMatrix;
    Coord: IMatrixCoord;
    Sto: ICubeInstanceStorage;
    AttrKey, Dim1Key, Dim2Key, Dim3Key, n1, n2, n3: Integer;
Begin
    MB := MetabaseClass.Active;
    //Open cube
    CubInst := MB.ItemById("Cube_1").Open(NullAs ICubeInstance;
    DestInst := CubInst.Destinations.DefaultDestination;
    //Get information about cube dimensions
    DestMod := DestInst.DestinationModel;
    DimsMod := DestMod.Dimensions;
    //Search for dictionaries, which are cube dimensions
    Dim1 := DimsMod.FindById("D_TO");
    Dim2 := DimsMod.FindById("D_SEP");
    Dim3 := DimsMod.FindById("CALENDAR");
    //Get keys of dictionaries
    Dim1Key := (Dim1 As IMetabaseObject).Key;
    Dim2Key := (Dim2 As IMetabaseObject).Key;
    Dim3Key := (Dim3 As IMetabaseObject).Key;
    //Get data by dimensions
    DimsInst := DestInst.Dimensions;
    //Search for elements by dimensions
    DimInst := DimsInst.FindByKey(Dim1Key);
    AttrKey := Dim1.Attributes.Name.Key; //Search by the Name attribute
    AttrInst := DimInst.Attributes.FindByKey(AttrKey);
    n1 := AttrInst.LookupValue("Russian Federation");
    DimInst := DimsInst.FindByKey(Dim2Key);
    AttrKey := Dim2.Attributes.Name.Key; //Search by the Name attribute
    AttrInst := DimInst.Attributes.FindByKey(AttrKey);
    n2 := AttrInst.LookupValue("Number of banks, units");
    DimInst := DimsInst.FindByKey(Dim3Key);
    AttrKey := Dim3.Attributes.Id.Key; //Search by the Identifier attribute
    AttrInst := DimInst.Attributes.FindByKey(AttrKey);
    n3 := AttrInst.LookupValue("YEARS:2010");
    If (n1 <> -1And (n2 <> -1And (n3 <> -1Then
        //Create a selection
        DimSS := DestInst.CreateDimSelectionSet;
        //Elements selection
        DimSS.FindByKey(Dim1Key).SelectElement(n1, False);
        DimSS.FindByKey(Dim2Key).SelectElement(n2, False);
        DimSS.FindByKey(Dim3Key).SelectElement(n3, False);
        //Extract data matrix by the specified cube selection
        Mat := DestInst.Execute(DimSS);
        Mat.ValueFlag := Mat.ValueFlag + 1;
        //Create the coordinate, by which it is required to change value
        //Dimensions in the matrix correspond to cube dimensions
        Coord := Mat.CreateCoord;
        Coord.Item(DimSS.IndexOfKey(Dim1Key)) := n1;
        Coord.Item(DimSS.IndexOfKey(Dim2Key)) := n2;
        Coord.Item(DimSS.IndexOfKey(Dim3Key)) := n3;
        //Increase value by specified coordinate
        Mat.Item(Coord) := (Mat.Item(Coord) As Integer) + 10;
        //Create the object that is used to save matrix to cube
        Sto := DestInst.CreateStorage;
        //Save data back to cube
        Sto.SaveMatrix(Mat, Mat.ValueFlag);
    End If;
End Sub GetCubeData;

The cube is opened on executing the example. The specified elements are searched by cube dimensions. If all elements are found, a cube selection is created. A data matrix is obtained on the basis of selection. Value of the element that corresponds to found elements of cube dimensions is changed in the matrix. Then the updated matrix is saved back to cube.

See also:

Examples