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];

 
 

NonEmpty和Non Empty的区别[转]的更多相关文章

  1. 【转载】NonEmpty和Non Empty的区别

    转载来源:http://www.ssas-info.com/analysis-services-articles/50-mdx/2196-mdx-non-empty-vs-nonempty One o ...

  2. [JS][jQuery]清空元素html("")、innerHTML="" 与 empty()的区别 、remove()区别

    清空元素html("").innerHTML="" 与 empty()的区别 一.清空元素的区别     1.错误做法一:           $(" ...

  3. EL表达式中null和empty的区别

    下面通过一个例子看看看null和empty的区别,建立一个test.jsp文件,内容如下: <%@page pageEncoding="utf-8" %> name:$ ...

  4. php 中 isset 和empty 的区别

    昨天终于开始学习php了,这个对于我来说听着很熟悉,但是学起来很陌生的东西,尤其是课上能听明白 但是真正写起了手生,都不知道手该往哪里放了,天哪~~~ 其中课上有讲到 isset和empty的区别,现 ...

  5. C#中string.Empty ,"" , null 区别

    引言 String类型作为使用最频繁的类型之一,相信大家都非常熟悉,对于string赋予空值,通常有以下三种方式: String str1=null; String str2=””; String s ...

  6. PHP isset()与empty()的区别详解

    通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言. 庞大的函数库支持着PHP语言功能的实现. 有关PHP函数isset()与empty()的相关用法. PHP的isset()函数 一 ...

  7. isset() 与 empty() 的区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  8. isset与empty 的区别

    isset()与empty()函数的区别,isset()只需要验证一个值是否存在: 而empty()不但需验证这个值是否存在,还需检验它的值是否非空和非0: 注:isset()只检验一个变量是否已经设 ...

  9. phpisset()和empty()函数区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

随机推荐

  1. AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识

    链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...

  2. 【跟我一起学Python吧】Python的包管理工具

    刚开始学习Python时,在看文档和别人的blog介绍安装包有的用easy_install, setuptools, 有的使用pip,distribute,那麽这几个工具有什么关系呢,看一下下面这个图 ...

  3. 使用jQuery Mobile实现通讯录

    jQuery Mobile 通讯录 拨打电话作者:方倍工作室 地址: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/ ...

  4. Android Application Project 工程目录下各个文件的意思

    (1) src:源文件,主要是完成java代码的编写 (2) gen:ADT即系统自动生成的JAVA文件(即源代码目录),程序员千万不要去修改 (3) gen->[Package Name]-& ...

  5. 【转】Maven实战(三)---插件动态打包

    原博文出于:http://blog.csdn.net/liutengteng130/article/details/41622013    感谢! maven把项目的构建划分为不同的生命周期(life ...

  6. 提高Scrum站会效率的一个小工具

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:提高Scrum站会效率的一个小工具.

  7. MT4平台上mql4实现的基于macd指标的智能交易EA

    屌丝命苦,拼爹拼不过,拼后台没有,技术宅一枚,情商有问题,不会见人说人话见鬼说鬼话,所以在国庆熬着混着,工作也没啥大起色,想想就郁闷,难不成一辈子就只能这样了? 苦思冥想,想得一条路,那就是程序化交易 ...

  8. spring事务注解

    @Transactional只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能. Spring使用声明式事务处理,默认 ...

  9. SQL SERVER 2008/2012/2012R2/2014 设置开启远程连接(sa配置)

    本文方案适用于Microsoft Sql Server 2008/2012/2012 r2/2014版本,以下简称MSSQLSERVER. MSSQL默认是不允许远程连接,并且禁用sa账户的.如果想要 ...

  10. 腾讯云centos6.5下部署django环境

    基于腾讯云CentOS6.5的环境 首先说下需要用到的软件 1.gcc环境 腾讯云默认是没有gcc编译器的,需要手动安装一下:yum install gcc 2.python环境 因为我用的cento ...