MDX : Non Empty v/s NonEmpty

MDX : Non Empty v/s NonEmpty

User Rating: / 50 
PoorBest

Written by Jason Thomas   
Friday, 07 May 2010 00:44

Reposted from Jason Thomas blog with the author‘s permission.

The last few months, recession worries have allayed and it is common to see the Talent Acquisition guys of our company stalking us techies to take interviews for lateral hires at odd hours. Now spending a beautiful Saturday morning + afternoon taking interviews is not my cup of tea but then their persistence paid off and finally I agreed to come for taking the interviews. That is when I thought of giving my readers a sneak peek of my list of interview questions, one by one per post.

One of my favourite questions in MDX is the difference between Non Empty and NonEmpty because even though many people use them daily to remove NULLS from their queries, very few understand the working behind it. Many times, I have even got answers like “there is a space between Non and Empty, that is the difference”. The objective of this post is to clearly differentiate between the two.

Let us say my initial query is

SELECT  
  { 
    [Measures].[Hits] 
   ,[Measures].[Subscribers] 
   ,[Measures].[Spam] 
  } ON COLUMNS 
,{ 
    [Geography].[Country].Children 
  } ON ROWS 
FROM [Blog Statistics];

This will give the following output

NON EMPTY

Non Empty is prefixed before the sets defining the axes and is used for removing NULLs. Let us see what happens when we add Non Empty on the Rows axis.

SELECT  
  { 
    [Measures].[Hits] 
   ,[Measures].[Subscribers] 
   ,[Measures].[Spam] 
  } ON COLUMNS 
,NON EMPTY  
    { 
      [Geography].[Country].Children 
    } ON ROWS 
FROM [Blog Statistics];

The output is shown below

You will notice that Chile (CL) has been filtered out while rows like UK, Canada, etc are still there even if they have NULLs for some of the measures. In short, only the rows having NULL for all the members of the set defined in the column axis is filtered out. This is because the Non Empty operator works on the top level of the query. Internally, the sets defined for the axes are generated first and then the tuples having NULL values are removed. Now that we know how NON EMPTY works, it shouldn’t be hard for us to tell the output of the below query

SELECT  
  NON EMPTY  
    { 
      [Measures].[Hits] 
     ,[Measures].[Subscribers] 
     ,[Measures].[Spam] 
    } ON COLUMNS 
,{ 
    [Geography].[Country].Children 
  } ON ROWS 
FROM [Blog Statistics];

The output is shown below

NONEMPTY()

The NonEmpty() returns the set of tuples that are not empty from a specified set, based on the cross product of the specified set with a second set. Suppose we want to see all the measures related to countries which have a non-null value for Subscribers

SELECT  
  { 
    [Measures].[Hits] 
   ,[Measures].[Subscribers] 
   ,[Measures].[Spam] 
  } ON COLUMNS 
,{ 
    NonEmpty 
    ( 
      [Geography].[Country].Children 
     ,[Measures].[Subscribers] 
    ) 
  } ON ROWS 
FROM [Blog Statistics];

This will give the following output

As you can see, the NonEmpty operator takes all the rows having a not NULL value for Subscribers in the rows and then displays all the measures defined in the column axis. Basically what happens internally is that NonEmpty is evaluated when the sets defining the axis are evaluated. So at this point of time, there is no context of the other axes. What I said now can be better understood from the following example

Now, we write the below query

SELECT  
  {[Date].[Month].[March]} ON COLUMNS 
,{ 
    [Geography].[Country].Children 
  } ON ROWS 
FROM [Blog Statistics] 
WHERE  
  [Measures].[Hits];

Output is given below

Think for a while and predict which all rows would be returned when the NonEmpty operator is applied on the rows

SELECT  
  {[Date].[Month].[March]} ON COLUMNS 
,{ 
    NonEmpty([Geography].[Country].Children) 
  } ON ROWS 
FROM [Blog Statistics] 
WHERE  
  [Measures].[Hits];

If you guessed just IN, US, GB and AU, please go back and read once again. If you replied All rows except Chile, full marks to you, you have been an attentive reader. The reason is because NonEmpty is evaluated when the set defining the axis is evaluated (here, Country) and at that point of time, NonEmpty is evaluated for each member of the country against the default member of the Date dimension (which would be ALL generally). As you can see, we already have values for CA and AP for other months and hence they will not be filtered out.

Optimizing Non Empty by using NonEmpty

Ok, now you know how Non Empty and NonEmpty works internally and we can apply this knowledge to optimize our queries. Suppose there is a complex logic in our axes like finding all the countries that have 30 or more hits in any month. The query is given below

SELECT  
  {[Measures].[Hits]} ON COLUMNS 
,{ 
    Filter 
    ( 
        [Geography].[Country].Children 
      *  
        [Date].[Month].Children 
     , 
      [Measures].[Hits] > 30 
    ) 
  } ON ROWS 
FROM [Blog Statistics];

Now my time dimension will have 10 years of data, which means around 120 (10*12) members for the month attribute and my country attribute may have let’s say, 100 members. Now even though I just have 3 months of data for 10 countries for hits, the filter function will need to go through all the combinations of country and month (120*100 combinations). Instead of that, we can just use the NonEmpty operator and bring down the combinations to less than 30 (3 months*10 countries) by using the below query

SELECT  
  {[Measures].[Hits]} ON COLUMNS 
,{ 
    Filter 
    ( 
      NonEmpty 
      ( 
          [Geography].[Country].Children 
        *  
          [Date].[Month].Children 
       ,[Measures].[Hits] 
      ) 
     , 
      [Measures].[Hits] > 30 
    ) 
  } ON ROWS 
FROM [Blog Statistics];


 
Jason has been working with Microsoft BI tools since he joined the IT industry in 2006. Since then he has worked across SSAS, SSRS and SSIS for a large number of clients. He is currently working for Mariner and is based out of Charlotte, NC. His personal blog can be found at http://road-blogs.blogspot.com


Comments (10)

10Wednesday, 22 January 2014 11:30

Rajendra

Nice explanation

9Thursday, 02 January 2014 18:56

AVANADE Guy

Agreed. Nice explanation. The impact on performance is extremely important when work with attributes/levels with thousands of members. I recently had a NON EMPTY query that was running around 19 minutes tuned to under 10 seconds with NONEMPTY.

8Thursday, 03 January 2013 05:00

Preeti

Nice explanation

7Tuesday, 06 March 2012 10:49

Sree1234

Very Usefull ....Here we have one more like ‘non empty behavior‘ it is good to have example for ‘non empty behavior‘.

6Sunday, 21 August 2011 10:16

Soniya

Very informative ... thanks

5Tuesday, 09 August 2011 13:04

Sudhanshu Sekhar Padhan

One helpful article to avoid confusion between Non Empty and Nonempty. Thanks

4Tuesday, 16 November 2010 12:41

165083

Could you provide information on how to implement the non empty in cube design

3Tuesday, 16 November 2010 09:21

roopesh babu v

kudos .. really a nice article .. thx for it ..

2Friday, 14 May 2010 13:39

Jason Tom Thomas

Could you please let me know which example you are referring to, maybe I could post the explanation in the comments section itself. 
If you are speaking of the image with mouseover as Nonempty second example, I am actually considering it as the source for my queries below.

1Tuesday, 11 May 2010 16:47

Saviour Faire

Interesting article, but some of the examples, code and samples, do not seem to make sense because the explanations were not provided. 
For example, the last "NON EMPTY" example query and results were not explained, and do not make sense. ie: "spam" column not shown and "Chile" is shown but empty, but these results not explained. As a student of ssas and mdx, based on the author‘s explanation provided I would be lead to believe a different result would have happened. 
As a student of the software, my interest as been piqued and I‘ll investigate further.

MDX : Non Empty v/s NonEmpty

时间: 2024-10-10 08:32:11

MDX : Non Empty v/s NonEmpty的相关文章

empty、isset、is

直接上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?php $a=0; $b='0'; $c=0.0; $d=''; $e=NULL; $f=array(); $g='\0'; $h=' ';//space $i=true; $j=false; $k;// a variable declared, but without a value $_arr=get_defined_vars(); array_spl

empty、isset、is_null的比较

直接上代码 <?php $a=0; $b='0'; $c=0.0; $d=''; $e=NULL; $f=array(); $g='\0'; $h=' ';//space $i=true; $j=false; $k;// a variable declared, but without a value $_arr=get_defined_vars(); array_splice($_arr,0,7); foreach($_arr as $k=>$v){ var_dump(!is_null($v

对oracle hash join trace的研究

SQL> select * from v$version; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production PL/SQL Release 11.2.0.1.0 - Production CORE    11.2.0.1

数据转入到excel表格中

 header("Content-type:application/vnd.ms-excel");  $filename='LBTX'.time();  header("Content-Disposition:attachment; filename=$filename.xls");  echo '编号'.chr(9).'流水号'.chr(9).'用户名'.chr(9).'手机号'.chr(9).'openid'.chr(9).'可提余额'.chr(9).'提现金额

无权最短路 - 宽度优先搜索

2017-09-13 21:54:52 writer:pprp 图论全部都忘记了,重新学一下吧,之前学的实在是太烂了 测试数据如下: 7 12//顶点个数, 路径个数3 11 41 22 42 54 34 54 64 73 65 76 73//起始点 代码如下: /* @theme:无权最短路径问题 @complexity:O(|E| + |V|) @writer:pprp @begin:21:10 @end:21:53 @error: @declare: breadth first searc

支付宝支付

/*   支付 */  public function pay()  {    header("content-type:text/html;charset=utf8");    //echo 1;die;    // ******************************************************配置 start*************************************************************************

maximal-rectangle——找出最大矩形的面积

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. PS:将其化解为柱状图求最大体积的问题.先用动态规划的思路,将矩形中垂直方向的连续1的数量进行计算. 而后逐行扫描,并在扫描时将其转化为柱状图求体积. 即若高度一直增加,则持续压入栈,当出现h[i]<栈顶高度时,则出栈,并计算出栈元素的所在矩形的体积.持续计算直到

php模板引擎的原理与简单实例

模板引擎其实就是将一个带有自定义标签的字符串,通过相应的规则解析,返回php可以解析的字符串,这其中正则的运用是必不可少的,所以要有一定的正则基础.总体思想,引入按规则写好的模板,传递给标签解析类(_HtmlTag)进行解析,再把解析好的字符串传递给php进行解析渲染输出首先定义了一个_HtmlTag类: class _HtmlTag{ protected $taglist = "if|elseif|else|loop|for|while|=|:=|:e|:html|:"; prote

JavaScript实用功能代码片段

1.原生JavaScript实现字符串长度截取 1 function cutstr(str, len) { 2 var temp; 3 var icount = 0; 4 var patrn = /[^\x00-\xff]/; 5 var strre = ""; 6 for (var i = 0; i < str.length; i++) { 7 if (icount < len - 1) { 8 temp = str.substr(i, 1); 9 if (patrn.e