Find Datatype Of Each Measure In SSAS MDX Queries

In this post we will see how we can get the datatype of each measure we use in our SSAS MDX Queries.

In this post we will see how we can get the data type of each measure we use in our SSAS MDX queries. This post may be helpful if you are working with SSAS cubes, especially you need to work with the data you get from the cubes, such as formatting the data, assigning the data as grid data source, or formulating the data to any other form. I got a requirement to show the cube data as a grid, so I needed to know the types of each measure users select so that I could assign the grid column types accordingly. There are many ways we can find the types of measure, here I am going to discuss that with you. I hope you will like this.

Background

I went through this requirement and I was able to do this in time with the help of Mr. Greg Galloway (Stack overflow user). You can find my question here in stack overflow.

If your cube has data with the types of string and numbers alone, finding the types will be too easy. Now before going through the processes listed in this article, if you don’t have only string and numbers as the types in the cube, this post will definitely help you.

If you are looking for some sample stored procedures that will help you with your development with your analysis services, you can always see it here.

Using the code

Here we are going to create a function which accepts server name, database name, and the measure name collection in which we need to find out what type it is. The core part of this function will be a DMV query which we can run against our SSAS cube. The query will be as follows.

  1. select [CATALOG_NAME],CUBE_NAME],MEASURE_NAME, DATA_TYPE,EXPRESSION,MEASURE_IS_VISIBLE,MEASUREGROUP_NAME,MEASURE_DISPLAY_FOLDER,DEFAULT_FORMAT_STRINGfrom $system.mdschema_measures  
Now before going further, please make sure that you have included AnalysisServices Adomd Client.
  1. using Microsoft.AnalysisServices.AdomdClient;  
Now we will create the function which will give you the data about the datatypes of your measures.
  1. #region Return the data types of measures  
  2. /// <summary>  
  3. /// FindMeasureDataTypes-Find the measure type whetehr it is a currency or a percentage or a number  
  4. /// </summary>  
  5. /// <param name="serverName"></param>  
  6. /// <param name="databaseName"></param>  
  7. /// <param name="myMeasureCollection"></param>  
  8. public DataTable FindMeasureDataTypes(string serverName, string databaseName, string myMeasureCollection)  
  9. {  
  10.     try  
  11.     {  
  12.         string res = string.Empty;  
  13.         List < string > myMeasures = new List < string > ();  
  14.         //Buiding the connection string start  
  15.         StringBuilder sbConnectionString = new StringBuilder();  
  16.         sbConnectionString.Append("Provider=MSOLAP;data source=");  
  17.         sbConnectionString.Append(serverName + ";initial catalog=" + databaseName + ";Integrated Security=SSPI;Persist Security Info=False;");  
  18.         //Buiding the connection string start  
  19.         AdomdConnection conn = new AdomdConnection(sbConnectionString.ToString());  
  20.         myMeasures = myMeasureCollection.Split(new string[]  
  21.         {  
  22.             "||"  
  23.         }, StringSplitOptions.None).ToList();  
  24.         for (int i = 0; i < myMeasures.Count; i++)  
  25.         {  
  26.             //Format the measure name  
  27.             if (i == 0) res += "MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].""").Replace("[""").Replace("]""") + "'";  
  28.             else res += " OR MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].""").Replace("[""").Replace("]""") + "'";  
  29.         }  
  30.         string query = "select [CATALOG_NAME],[CUBE_NAME],MEASURE_NAME, DATA_TYPE,EXPRESSION,MEASURE_IS_VISIBLE,MEASUREGROUP_NAME,MEASURE_DISPLAY_FOLDER,DEFAULT_FORMAT_STRING from $system.mdschema_measures where " + res;  
  31.         using(AdomdCommand cmd = new AdomdCommand(query, conn))  
  32.         {  
  33.             DataTable tblMeasureType = new DataTable();  
  34.             AdomdDataAdapter da = new AdomdDataAdapter(cmd);  
  35.             da.Fill(tblMeasureType);  
  36.             return tblMeasureType;  
  37.         }  
  38.     }  
  39.     catch (Exception)  
  40.     {  
  41.         return null;  
  42.     }  
  43. }#endregion  
So in the table tblMeasureType, we will get the data. In the column DEFAULT_FORMAT_STRING, you can see the actual type of your measure. It will show as ‘Currency’ if it is a currency type and ‘Percentage’ if it is a percentage type and ‘#,##’ for the numbers. The DEFAULT_FORMAT_STRING actually describes each measures which are all available in the cube. And DEFAULT_FORMAT_STRING rowset contains the following columns. 
  • CATALOG_NAME
  • SCHEMA_NAME
  • CUBE_NAME
  • MEASURE_NAME
  • MEASURE_UNIQUE_NAME
  • MEASURE_CAPTION
  • MEASURE_GUID
  • MEASURE_AGGREGATOR
  • DATA_TYPE
  • NUMERIC_PRECISION
  • NUMERIC_SCALE
  • MEASURE_UNITS
  • DESCRIPTION
  • EXPRESSION
  • MEASURE_IS_VISIBLE
  • LEVELS_LIST
  • MEASURE_NAME_SQL_COLUMN_NAME
  • MEASURE_UNQUALIFIED_CAPTION
  • MEASUREGROUP_NAME
  • MEASURE_DISPLAY_FOLDER
  • DEFAULT_FORMAT_STRING

You can always check here to see for more information about MDSCHEMA_MEASURES Rowset.

As you can see the function accepts one parameter called myMeasureCollection, this is the collection of our measures and we are formatting the same as follows.

  1. for (int i = 0; i < myMeasures.Count; i++)  
  2. {  
  3.     //Format the measure name  
  4.     if (i == 0) res += "MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].""").Replace("[""").Replace("]""") + "'";  
  5.     else res += " OR MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].""").Replace("[""").Replace("]""") + "'";  
  6. }  
The myMeasureCollection is a string variable which has the values in the format of [Measures].[My Measure 1]||[Measures].[My Measure 2]||[Measures].[My Measure 3]. That is why we are splitting the values with || as follows.
  1. myMeasures = myMeasureCollection.Split(new string[] { "||" }, StringSplitOptions.None).ToList();  
I hope everything is clear. Have a happy coding.

Conclusion

Did I miss anything that you may think is needed? Have you ever wanted to do this requirement? Did you find this post as useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, ASP.NET Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

See this article in my blog here
 
Read more articles on ASP.NET Programming: