PTR logo

Blog Post

MDX - Counting Members at a Hierarchy Level

MDX - Counting Members at a Hierarchy Level
MD

Author

Mandy Doward

Date

September 19, 2016

Length

3 mins

Counting Members in an MDX Query

MDX is a challenging query language. It is completely different to the SQL query language despite having a SELECT statement!

MDX queries are all about hierarchies, measures and members and when it comes to carrying out tasks that are simple in SQL it may not be so simple in MDX.

Most of the time when we work with MDX queries we are interested in bringing out aggregate values that are stored in our cube, but occasionally we may want to derive values from the stored cube data.

In the following examples I will be using the Microsoft sample cube, based on the AdventureWorksDW2012 database.

Counts Stored as Measures

Take the following example, In the Internet Customers Measure Group is a measure called Customer Count, which is a count of the customers in the cube data. (Customers are related to Internet Sales in the AdventureWorks cube).

The first MDX query simply retrieves the Customer Count measure from the cube:

Counts Derived With The MDX Count Function

The same result as the measure value above is obtained by using the MDX Count function with the leaf level of any of Customer dimension hierarchies that go down to the customer member level:

The Count function counts the number of members at the sprcified hierarchy level. Here is an extract from the [Customer].[Customer].[Customer] attribute hierarchy level:

The following example will only produce a count of 2 rather than 18484:

The reason for this? well the Home Owner attribute hierarchy only has two leaf members: Yes and No.

So for cases where there is no measure in the cube for a count of members we can use the Count function, as in the case of the Products. There is no measure in the cube for a count of the number of product members that exist. The following MDX query can be used to produce this figure:

The following two queries both return the same result (397 products), using two different hierarchies:

Combining The MDX Count Function With Other Functions

The Count function can be appended to any MDX set, including those created by an MDX Set Function. The following example counts the members in a set created by the Descendants function:

The following screen shot shows the results from this query:

The Descendants function returns the set of all members at the product level in the Product Categories hierarchy, for the given category (CurrentMember in the measure expression takes on  each category identified by the [Product].[Product Categories].[Category] tuple in the SELECT axis 1.

In other words we are counting the number of products in each product category.

Would you like to learn more about MDX querying and MDX functions? Email us at info@ptr.co.uk with your questions or why not take a look at the course outline for our MDX Programming training course?

Share This Page

MD

Mandy Doward

Managing Director

PTR’s owner and Managing Director is a Microsoft MCSE certified Business Intelligence (BI) Consultant, with over 30 years of experience working with data analytics and BI.

Latest Articles

PTR FAQs

See our FAQs or get in contact for more information on any of our services, solutions, or to start your PTR journey.

Ready to take your business to the next level?

Reach out to our team of experts and learn more about our consultancy and training services.