Home > SSAS > Set Operations in MDX – UNION and EXCEPT

 

Set Operations in MDX – UNION and EXCEPT

August 1st, 2010

I just read an article MDX: Except written by Vincent Rainardi.

It shows set subtraction by usng the EXCEPT function (as you could derive from the title, no doubt). I have always been a fan of using the “-” and “+” operators instead of EXCEPT and UNION where possible because in my opinion they give us better visiblity of the intention we are putting behind our MDX expressions. However, EXCEPT and UNION have an advantage over “-” and “+” – the third parameter ALL.

In BOL we can see that both of these functions can be used like this: UNION/EXCEPT(set1, set2, ALL). If we skip the ALL keyword, we would get exactly what we would get with +/-. Some examples:

SET1: {a, b, c}
SET2: {c}

SET3 = UNION(SET1, SET2) = SET1+SET2 = {a, b, c}+{c} = {a, b, c}

But if we use ALL, we would get duplicates in our result set:

SET4 = UNION(SET1, SET2, ALL) = UNION({a, b, c}, {c}, ALL} = {a, b, c, c}

The difference here is the duplicates, which get preserved in SET4 because of ALL. And this is also where EXCEPT is different to “-”:

SET5 = EXCEPT(SET4, SET2) = SET4-SET2 = {a, b, c, c} – {a} = {b, c}

while

SET6 = EXCEPT(SET4, SET2, ALL) = {a, b, c, c} – {a} = {b, c, c}

As you can see, unlike in set math where a set cannot have dupicates, in MDX we can. Therefore, if we are in a situation where we need to preserve these, we have the option of using the UNION and EXCEPT functions with the ALL parameter.

I am using these concepts on every-day basis and I have found that mastering them gives me a very powerful way of solving many MDX problems. I hope that the examples are suitable and easy to understand – when I read the article after I have just written it I sound a bit like my math lecturer from uni (I wish he knew and taught MDX), who was a good guy, so I guess there is nothing wrong with that :)

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. Passing unCONSTRAINED Set and Member parameters between reports in Reporting Services
  2. How to use ITEM and when ITEM(0).ITEM(0) is redundant
  3. When Not To Write MDX and When Not To Use Analysis Services

 

SSAS , ,

  1. Eugene
    August 13th, 2010 at 00:29 | #1

    Sorry, but Except( {a, b, c, c}, {c}, all ) = {a, b}.

    And more:
    Except( {a, b, c, c}, {a}, all ) = {b, c, c}.
    Except( {a, b, c, c}, {a} ) = {b, c}.

  2. August 13th, 2010 at 08:26 | #2

    Thanks Eugene, article is updated.

Comments are closed.

Switch to our mobile site