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.
- select [CATALOG_NAME],CUBE_NAME],MEASURE_NAME, DATA_TYPE,EXPRESSION,MEASURE_IS_VISIBLE,MEASUREGROUP_NAME,MEASURE_DISPLAY_FOLDER,DEFAULT_FORMAT_STRINGfrom $system.mdschema_measures
- using Microsoft.AnalysisServices.AdomdClient;
- #region Return the data types of measures
- /// <summary>
- /// FindMeasureDataTypes-Find the measure type whetehr it is a currency or a percentage or a number
- /// </summary>
- /// <param name="serverName"></param>
- /// <param name="databaseName"></param>
- /// <param name="myMeasureCollection"></param>
- public DataTable FindMeasureDataTypes(string serverName, string databaseName, string myMeasureCollection)
- {
- try
- {
- string res = string.Empty;
- List < string > myMeasures = new List < string > ();
- //Buiding the connection string start
- StringBuilder sbConnectionString = new StringBuilder();
- sbConnectionString.Append("Provider=MSOLAP;data source=");
- sbConnectionString.Append(serverName + ";initial catalog=" + databaseName + ";Integrated Security=SSPI;Persist Security Info=False;");
- //Buiding the connection string start
- AdomdConnection conn = new AdomdConnection(sbConnectionString.ToString());
- myMeasures = myMeasureCollection.Split(new string[]
- {
- "||"
- }, StringSplitOptions.None).ToList();
- for (int i = 0; i < myMeasures.Count; i++)
- {
- //Format the measure name
- if (i == 0) res += "MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].", "").Replace("[", "").Replace("]", "") + "'";
- else res += " OR MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].", "").Replace("[", "").Replace("]", "") + "'";
- }
- 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;
- using(AdomdCommand cmd = new AdomdCommand(query, conn))
- {
- DataTable tblMeasureType = new DataTable();
- AdomdDataAdapter da = new AdomdDataAdapter(cmd);
- da.Fill(tblMeasureType);
- return tblMeasureType;
- }
- }
- catch (Exception)
- {
- return null;
- }
- }#endregion
- 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.
- for (int i = 0; i < myMeasures.Count; i++)
- {
- //Format the measure name
- if (i == 0) res += "MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].", "").Replace("[", "").Replace("]", "") + "'";
- else res += " OR MEASURE_NAME ='" + myMeasures[i].Replace("[Measures].", "").Replace("[", "").Replace("]", "") + "'";
- }
- myMeasures = myMeasureCollection.Split(new string[] { "||" }, StringSplitOptions.None).ToList();
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.

Rupali ShindePosted Feb 23, 2016, 5:15 AM
MDX quries , thanks for sharing
Rupali ShindePosted Feb 23, 2016, 5:14 AM
nice one sir
Saineshwar BageriPosted Feb 21, 2016, 11:06 PM
Nice one
Former memberPosted Feb 21, 2016, 9:33 PM
Good one
Shubham KumarPosted Feb 20, 2016, 2:01 AM
nice sir
Sibeesh VenuPosted Feb 20, 2016, 1:41 AM
Mohammed Ibrahim Thanks a lot
Sibeesh VenuPosted Feb 20, 2016, 1:41 AM
Debasis Saha Thanks a lot
Sibeesh VenuPosted Feb 20, 2016, 1:41 AM
Gowtham K Thanks a lot
Mohammed IbrahimPosted Feb 19, 2016, 2:47 PM
nice
Debasis SahaPosted Feb 19, 2016, 1:01 PM
Thanks for nice article..
Gowtham KPosted Feb 19, 2016, 12:40 PM
Good One
Sibeesh VenuPosted Feb 19, 2016, 12:37 AM
Ankur Mistry Thanks much
Sibeesh VenuPosted Feb 19, 2016, 12:37 AM
Kumaresh Rajalingam Thanks much
Ankur MistryPosted Feb 18, 2016, 9:15 PM
Nice
Kumaresh RajalingamPosted Feb 18, 2016, 8:21 PM
Nice share