Questions tagged [mdx]

MDX (Multi-Dimensional eXpressions) is a query language used by various OLAP servers, notably Microsoft SQL Server Analysis Services.

MDX is an OLAP query language used on Microsoft SQL Server Analysis Services (SSAS) and a number of other OLAP servers. The term is short for Multi-Dimensional eXpressions.

Although bearing a superficial resemblance to SQL, MDX works to a fundamentally different paradigm, and there are many significant anisomorphisms between the languages. For example, MDX has no explicit concept of a row within the language.

MDX is primarily concerned with calculations and operations that manipulate sets. A result set is a multi-dimensional structure, rather than rows and columns (although it is possible to emulate a tabular result). Queries select a set of dimension members to display on query axes. A special measures dimension allows measures to be selected.

The main parts of a MDX query are:

  • A set of precursor definitions , with statements that define calculations, synthetic dimension members and sets. A MDX query does not necessarily have to have with statements, but they are normally used on queries of non-trivial complexity.

  • a select statement selecting sets on each axes. A default measure may appear in each cell, or it may be explicitly selected as a part of the query on the axis. The set may be a set of tuples and can be hierarchical in nature.

  • a slicer (or where) clause that restricts the query. It operates similarly to a definition of an axis with a single, but has slightly different semantics.

Anatomy of a MDX query:

Here is an example of a MDX query taken from a question on DBA.SE. We can see that although the syntax bears a superficial resemblance to SQL the underlying semantics are very different.

with 
member [Measures].[Delta]
    as [Measures].[Price] -
       (parallelperiod ([Date].[Calendar].[Month]
                       ,1
                       ,[Date].[Calendar].currentmember)
       ,[Measures].[Price])

This is defining a calculated member with a time-aware function (parallelperiod). A time hierarchy can be explicitly defined on a date dimension, which is then accessible to date-time aware functions such as parallelperiod, ytd and so forth. The measure is the difference between the value in a curent period and previous period.

member [Measures].[Rising]
    as count (filter ([Reseller].[Reseller].Children
                      ,[Measures].[Delta] > 0))
      ,solve_order = 20

member [Measures].[Static]
    as count (filter ([Reseller].[Reseller].Children
                      ,[Measures].[Delta] = 0))
      ,solve_order = 20

member [Measures].[Falling]
    as count (filter ([Reseller].[Reseller].Children
                      ,[Measures].[Delta] < 0))
      ,solve_order = 20

These measures apply filters to members of the Reseller dimension based on the value of a computation. Note that the filter is happening at a slice (aggregate) level rather on a row by row basis.

select {[Measures].[Rising]
       ,[Measures].[Static]
       ,[Measures].[Falling]}
    on columns 

The select statement is selecting three measures on columns using the Measures pseudo dimension, producing a tabular result set. By selecting dimension members across the columns you can generate a cross-tabulation.

      ,[Product].[Product].Children
    on rows

And on the rows we are slicing by the products using the Children operator, which takes a specific level in a hierarchy and returns the children of that level.

  from [SalesTrends]

The cube we are selecting on is called SalesTrends

 where ([Date].[Calendar].[Month].[201202])

This is a slicer. Note that the expression is not an equality test. (i.e. not an evaluation of foo=bar). It is a definition of a dimension member.

67 questions
13
votes
3 answers

Good example of MDX vs SQL for analytical queries

Can anybody show me a good example of MDX's advantages over regular SQL when doing analytical queries? I would like to compare an MDX query with an SQL query that gives similar results. Wikipedia says: While it is possible to translate some of…
Leftium
  • 769
  • 1
  • 7
  • 13
10
votes
1 answer

MDX Query for RANK regardless of hierarchy

I'm hoping to add a calculation to my SSAS Cube (SSAS 2012) that will show the relative rank of whatever the current member is against all other members in that grouper, without specifying the groupers in advance. For example, say I have a…
JNK
  • 18,064
  • 6
  • 63
  • 98
6
votes
1 answer

Time as a measure

Is it possible to have times as measures in a cube? We're trying to view employee start times by day, aggregating as an average over time. Even with a No Aggregation measure type I'm getting an error when deploying saying that StartTime is a String…
Jeff
  • 313
  • 2
  • 8
5
votes
2 answers

8000 character limit in OPENQUERY against Analysis Server

I have a query like SELECT column1, column2 FROM OPENQUERY(AnalysisServerName, 'MDX QUERY ...'). It is inside a stored procedure. A MDX query is dynamically built in it and the length of the query much longer than 8000 characters (can reach up to…
5
votes
1 answer

Custom aggregation for non-leaf level nodes to avoid double counting

I have a fact table with the following columns: WorkerName OrderId NumberOfPackagesPerOrder Note that neither WorkerName nor OrderId is unique in this fact table. Multiple users could work on a single OrderId Also note that…
user1234883
  • 151
  • 5
4
votes
0 answers

MDX: How do I combine/merge/union two Measures dimensions with calculated members?

I am trying to combine two dimensions in an MDX query. SELECT NON EMPTY ( [XXX].[YYY].children ) ON ROWS, NON EMPTY { [Measures].[Dimension1A], [Measures].[Dimension1B], [Measures].[Dimension2A], [Measures].[Dimension2B] } ON…
Neo
  • 465
  • 3
  • 6
  • 12
4
votes
1 answer

OLAP linked server fails connection test

I am trying to create a new linked server to an Analysis Services database and I would like to use this linked server to import data into a SQL Server table. example: SELECT * into raw.example FROM openquery( Test1, 'MDX CODE/etc etc' Below is…
VS1SQL
  • 41
  • 3
4
votes
1 answer

How can I get the measures of the last month with MDX?

I would like to get the values of the measures of the previous month. That's what I have: SELECT [Measures].[Recuent distinct] ON ROWS, TAIL(NonEmptyCrossJoin([Dim Date].[Calendar].[Month Name].Members,1),1) ON COLUMNS FROM [BDID] …
Miguel
  • 365
  • 3
  • 5
  • 14
3
votes
2 answers

MDX calculated member to band a measure

I am trying to add a calculated member to an MDX query which will create a set of "Age Bands" for people in my database. I have a measure which contains the age of each person. I have started with this MDX SELECT NON EMPTY {[Measures].[Count of…
Jamiec
  • 131
  • 1
  • 2
3
votes
1 answer

BI - Cube Concept and MDX Query Help

In a Mondrian Schema, I have a fact table for Sales, a dimension table Time and a dimension table Client. I need to create a Saiku report where i can show how many clients joined per year. I have a the Time dimension and a join_date in Client (which…
lucassig
  • 41
  • 3
3
votes
2 answers

Equivalent of SQL Not Exists in MDX (or finding another way to accomplish same result)

I am trying to help a contractor implement a cube in Multi-Dimensional SSAS 2017 and do not have very much experience in MDX. I have a table that looks something like this: The goal is to only include rows in the query that have the lowest [seq]…
schiznig
  • 31
  • 2
3
votes
1 answer

Need to filter dimension where two attributes are the same

I've obfuscated the scenario but the general idea still applies, so if the schema seems retarded, it probably is in this example... Let's say I have a Student dimension, and in it are two attributes - Major and Minor (subjects). I want to be able to…
James Love
  • 245
  • 2
  • 8
3
votes
2 answers

How to calculate a percentage over a dimension attribute and get correct aggregates?

We have a fact table with the weight trasported by truck/driver/day. And a dimension with the max weight per truck. We would like to have the % of weight transported, so we started with create MEMBER CURRENTCUBE.[Measures].[% WT] AS…
DeepButi
  • 161
  • 5
3
votes
1 answer

Getting formatted MDX values using OPENQUERY

Is there any way to convert a value in MDX to a string using its FORMAT_STRING? I have code similar to the following: SELECT * FROM OPENQUERY(DWDB001AS, ' WITH MEMBER [Measures].[ROH Total Sold Gross] AS [Measures].[ROH Total Sold Dollars] -…
3
votes
1 answer

Overriding a row FORMAT_STRING with a column FORMAT_STRING in MDX

Does anyone have an idea how you could override the format string on a row with a format string on a column? I have a query like the following: WITH MEMBER [Measures].[ROH Total Sold Gross] AS [Measures].[ROH Total Sold Dollars] -…
1
2 3 4 5