MDX

The rather-too-many ways to crossjoin in MDX

In my last post I made the point that it’s a bit too easy to write and MDX query that works, even if you don’t really understand why it works, and in this post I’m going to address a similar issue. In MDX one of the commonest set operations is a crossjoin, and while most people understand what this operation does there are so many ways of writing a crossjoin in MDX that it can hurt readability and make the language even more confusing for newcomers. So what are all these different ways of crossjoining, and which one is to be preferred?

First of all, let’s look at what a crossjoin actually does. Imagine we have two sets, each with two members in them: {A,B} and {X,Y}. If we crossjoin these two sets together, we get a set of tuples containing every possible combination of A and B and X and Y, ie the set {(A,X), (A,Y), (B,X), (B,Y)}.

As an example of this, let’s look at the first way of doing a crossjoin in MDX: the Crossjoin() function. Here’s a query against the Adventure Works cube that returns the crossjoin of the two sets {Married, Single} and {Female, Male} on the rows axis:

SELECT {[Measures].[Internet Sales Amount]} ON 0,
CROSSJOIN(
{[Customer].[Marital Status].&[M], [Customer].[Marital Status].&[S]}
,
{[Customer].[Gender].&[F],[Customer].[Gender].&[M]}
)
ON 1
FROM [Adventure Works]

Here’s the output:

As you’d expect, you get four rows for each of the four tuples in the set: {(Married, Female), (Married, Male), (Single, Female), (Single, Male)}.

What are the pros and cons of using the Crossjoin() function then? Well, one thing it’s worth stating is that all of the different ways of doing crossjoins in MDX perform just as well as the others, so it’s purely a question of readability and maintainability. On those criteria its main advantage is that it’s very clear you’re doing a crossjoin – after all, that’s the name of the function! However, because it carries an overhead in terms of the numbers of brackets and commas and the name of the function itself, which isn’t so good for readability, and this is why I generally don’t use it. When you’re crossjoining a lot of sets together, for example:

SELECT {[Measures].[Internet Sales Amount]} ON 0,
CROSSJOIN(
{[Customer].[Marital Status].&[M], [Customer].[Marital Status].&[S]}
,
{[Customer].[Gender].&[F],[Customer].[Gender].&[M]}
,
[Customer].[Education].[Education].MEMBERS
,
[Customer].[Total Children].[Total Children].MEMBERS
)
ON 1
FROM [Adventure Works]

…you might need to look a long way up to the top of the query to find out you’re doing a crossjoin.

The most popular alternative to the Crossjoin() function is the * operator. This allows you to crossjoin several sets by simply putting an asterisk between them, for example:

SELECT {[Measures].[Internet Sales Amount]} ON 0,
{[Customer].[Marital Status].&[M], [Customer].[Marital Status].&[S]}
*
{[Customer].[Gender].&[F],[Customer].[Gender].&[M]}
ON 1
FROM [Adventure Works]

It’s more concise than the Crossjoin() function and I think easier to read; it also has the advantage of being the most frequently-used syntax. However there are rare cases when it can be ambiguous because an asterisk is of course also used for multiplication. Consider the following calculated measure in the following query:

WITH
MEMBER MEASURES.DEMO AS
([Measures].[Internet Sales Amount]) * ([Customer].[Gender].&[F])
SELECT {[Measures].DEMO} ON 0,
{[Customer].[Marital Status].&[M], [Customer].[Marital Status].&[S]}
ON 1
FROM [Adventure Works]

Are we crossjoining or multiplying here? Actually, we’re multiplying the result of the two tuples together, rather than returning the result of the tuple ([Measures].[Internet Sales Amount], [Customer].[Gender].&[F]), but it’s not easy to tell.

The third way of doing a crossjoin is one I particularly dislike, and it’s the use of brackets and commas on their own as follows:

SELECT {[Measures].[Internet Sales Amount]} ON 0,
({[Customer].[Marital Status].&[M], [Customer].[Marital Status].&[S]}
,
{[Customer].[Gender].&[F],[Customer].[Gender].&[M]})
ON 1
FROM [Adventure Works]

This is for me the least readable and most ambiguous syntax: in my mind round brackets denote a tuple and here we’re getting a set of tuples. I’d therefore advise you not to use this syntax.

Last of all, for maximum MDX geek points, is the Nest() function. Almost completely undocumented and unused, a hangover from the very earliest days of MDX, it works in exactly the same way as the Crossjoin() function:

SELECT {[Measures].[Internet Sales Amount]} ON 0,
NEST(
{[Customer].[Marital Status].&[M], [Customer].[Marital Status].&[S]}
,
{[Customer].[Gender].&[F],[Customer].[Gender].&[M]}
)
ON 1
FROM [Adventure Works]

Of course you’d never want to use it unless you were either showing off or wanted to confuse your colleagues as much as possible…

In summary, I’d recommend using the * operator since it’s probably the clearest syntax and also the most widely-accepted. Equally importantly, I’d advise you to be consistent: choose one syntax, stick with it and make sure everyone on the project does the same.

20 thoughts on “The rather-too-many ways to crossjoin in MDX

  1. Hello Chris,

    Nice overview.

    When you want to show not all the tuples from a crossjoin result, but only some specific tuples from a crossjoin result, then you can also immediately specify them tuples. E.g.
    SELECT
    {[Measures].[Reseller Sales Amount]} on 0
    , {([Alabama],[Bikes]),([Maryland].[Clothing])} on 1
    FROM [Adventure Works];
    I agree, this is not a crossjoin anymore (see your definition above).

    Thx
    Franky L.

    1. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
      Chris Webb says:

      Yes, this is just explicitly specifying the set of tuples you want, not a crossjoin.

  2. Hello Chris,

    I dint get what you want to say here, if i use normal set also i will get same 4 rows , i mean to say,

    SELECT {} ON COLUMNS,
    ({Male, Female} * {Single, Married}) ON ROWS FROM [CUBE NAME]

    (OR)
    SELECT {} ON COLUMNS,
    ({Male, Female} , {Single, Married}) ON ROWS FROM [CUBE NAME]

    RESULT IS SAME

    Married Female
    Married Male
    Single Female
    Single Male

    My question is, when both provide same result then what is the use of cross join?

    1. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
      Chris Webb says:

      Hi Vinay,

      Neither of your examples show a ‘normal’ set – they both show different ways of performing a crossjoin on two sets and returning the same set of four tuples.

      Chris

  3. I am stuck in one MDX query.

    I want to implement cross join in MDX query.

    Example, I have 1 table for gender (M,F) and 1 for marital status (M,S).

    The below query gives me all combinations of Married Males and Females and single males and females.

    SELECT {[Measures].[Internet Sales Amount]} ON 0,
    {[Customer].[Marital Status].&[M], [Customer].[Marital Status].&[S]}
    *
    {[Customer].[Gender].&[F],[Customer].[Gender].&[M]}
    ON 1
    FROM [Adventure Works]

    But, I want to get only Married females and single males and also measures are different. I don’t need to show them on axis..The condition would be in where in MDX.

    Please revert if anyone knows it.

    1. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
      Chris Webb says:

      In this scenario, you don’t need a condition, you just need to know how to write the set of tuples you want. For example to get married females and single males you’d just say:

      SELECT {[Measures].[Internet Sales Amount]} ON 0,
      {
      ([Customer].[Marital Status].&[M], [Customer].[Gender].&[F]) ,
      ([Customer].[Marital Status].&[S], [Customer].[Gender].&[M])
      }
      ON 1
      FROM [Adventure Works]

  4. Hi Chris,

    Does a Dim1.Hier1.members * Dim2.Hier2.members * Dim3.Hier3.member implicitly get converted to a CROSSJOIN(CROSSJOIN(Dim1.Hier1.members, Dim2.Hier2.members), Dim3.Hier3.members)? Or does any optimization go on in the background when using the * way of cross-joining. Is there any difference in execution times?

    1. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
      Chris Webb says:

      One doesn’t get converted to the other – they’re just two different ways of writing the same thing and the engine handles them in the same way. There should be no difference in performance provided the sets are used in the same order.

      1. Please ignore my prev reply(I wanted to delete it, but I don’t see a delete button).

        So you mean no real difference between CROSSJOIN(, , ) and CROSSJOIN(CROSSJOIN(, ), )? Does the engine work on the crossjoins in an exactly similar fashion?

      2. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
        Chris Webb says:

        Yes, it should do.

      3. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
        Chris Webb says:

        But remember the order of the sets in the crossjoin can be important if two or more sets come from different hierarchies on the same dimension. So
        Crossjoin(a,b,c)
        is the same as
        Crossjoin(Crossjoin(a,b),c)
        but might not perform the same as
        Crossjoin(a,c,b)

      4. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
        Chris Webb says:

        This question is about Mondrian, not SSAS. I don’t know whether Mondrian might handle this differently.

  5. If I want to display members of 2 hierarchies on an axis, which is the better way of displaying them? Crossjoin or a tuple. e.g. (Dim1.Hier1.members, Dim1.Hier2.members) or (Dim1.Hier1.members * Dim1.Hier2.members). Is there any general rule about it?

    1. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
      Chris Webb says:

      Both of your examples are crossjoins – (Dim1.Hier1.members, Dim1.Hier2.members) is not a tuple, it is another way of writing a crossjoin. I generally prefer using the * and always avoid writing (Dim1.Hier1.members, Dim1.Hier2.members) because I think the latter is very confusing: it looks like a tuple but it isn’t.

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.