Search code examples
c#sql-serverssasmdx

SSAS: programmatically & permanently create calculated member


I'm trying to permanently create a calculated dimension member using a MDX ADOMD command from C# code. The following MDX script creates the member but it's only available for the current session.

CREATE MEMBER [Cube].[Dimension].[Hierarchy].[My Member Name] as 
    Aggregate({
        [Dimension].[Hierarchy].[Level].&[1], [Dimension].[Hierarchy].[Level].&[2], ...
    })   

How to permanently create it?


Solution

  • Never tried it, but I found some AMO code that appears to do this, it's loading the current MDX calculation script from the cube, adding to it, then posting it back.

    Dim objServer As Server
    Dim objDatabase As Database
    Dim strDataBaseID As String
    Dim objCube As Cube
    Dim objMdxScript As MdxScript
    Dim objCommand As Command
    Dim strCommand As String
    objServer = New Server
    objServer.Connect(“localhost”)
    objDatabase = objServer.Databases(“Adventure Works DW 2008″)
    strDataBaseID = objDatabase.ID
    If objDatabase.Cubes.Count > 0 Then
    objCube = objDatabase.Cubes(“Adventure Works”)
    If objCube.MdxScripts.Count > 0 Then
    objMdxScript = objCube.MdxScripts(“MdxScript”)
    ”objMdxScript = objCube.MdxScripts(0) 
    Else
    objCube.MdxScripts.Add(“MdxScript”, “MdxScript”)
    objMdxScript = objCube.MdxScripts(“MdxScript”)
    End If
    objCommand = New Command
    strCommand = “CREATE MEMBER CURRENTCUBE.[Measures].[Multipy Measures By 20]“
    strCommand = strCommand & ” AS [Measures].[Internet Sales Amount] * 20, “
    strCommand = strCommand & ” VISIBLE = 1 ; “
    objCommand.Text = strCommand
    objMdxScript.Commands.Add(objCommand)
    objMdxScript.Update()
    objCube.Update()
    End If
    objServer.Disconnect()
    

    Found at http://www.win8s.com/sql-server/sql-server-analysis-services/2011/programically-changing-calculated-members.html

    I use AMO elsewhere, but haven't used it like this, so do report back on results :)