INF: MDX: How to Test for the Top Most Level in a Hierarchy (304529)



The information in this article applies to:

  • Microsoft SQL Server 2000 Analysis Services
  • Microsoft SQL Server OLAP Services 7.0

This article was previously published under Q304529

SUMMARY

When you traverse a dimension through a Multidimensional Expressions (MDX), you may have to determine if you are at the top most level of the hierarchy. This article shows you four MDX expressions that allow you to find out if you are at the top most level of the hierarchy.

MORE INFORMATION

The examples print the word "Top" next to the All Promotions member and "Not Top" next the rest of the members, so that you can see which member is on the top most level.

The following two MDX expressions use the IIF function to test if you are at the top level. These expressions work regardless if there is a (All) level in the dimension.
with member measures.test as'iif ([Promotions].CurrentMember.Parent is null,"Top","Not Top")'

Select {[Measures].test} on columns,
[Promotions].members on rows
from sales
				
-or-
with member measures.test as'iif ([Promotions].CurrentMember.Level.Ordinal = 0,"Top","Not Top")'

Select {[Measures].test} on columns,
[Promotions].members on rows
from sales
				


If your dimension has an (All) level, you can test explicitly for the top level. For example:
with member measures.test as'iif ([Promotions].CurrentMember.level.name = "(All)","Top","Not Top")'

Select {[Measures].test} on columns,
[Promotions].members on rows
from sales
				
However, it would be better to compare the objects by using the IS comparison operator:
with member measures.test as'iif ([Promotions].CurrentMember.Level is [Promotions].[(All)],"Top","Not Top")'

Select {[Measures].test} on columns,
[Promotions].members on rows
from sales
				
NOTE: It is always better to compare objects such as Null or [Promotions].[(All)] by using the IS operator rather than comparing two strings. Comparing objects is faster and the equal (=) operator for string comparison is case sensitive and less flexible.

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbinfo KB304529