MDX Language Reference

This section provides a comprehensive reference for the Multidimensional Expressions (MDX) language used with SQL Server Analysis Services.

Tip: MDX is a powerful query language for multidimensional data, enabling complex analysis and data retrieval.

Overview

Multidimensional Expressions (MDX) is a query language syntax that supports the creation and manipulation of data stored in OLAP (Online Analytical Processing) cubes. MDX combines aspects of SQL and spreadsheet functions, providing a robust syntax for defining calculations, aggregations, and data extraction from multidimensional data models.

Key Concepts

Basic Syntax Examples

Selecting Data

A basic MDX query selects data from a cube:

SELECT {[Measures].[Sales Amount]} ON COLUMNS,
    {([Date].[Calendar Year].&[2022], [Product].[Category].&[Bikes]),
     ([Date].[Calendar Year].&[2023], [Product].[Category].&[Clothing])} ON ROWS
FROM [Adventure Works]

Using Functions

MDX provides a rich set of built-in functions for calculations and data manipulation. For example, using the Aggregate function:

WITH
    MEMBER [Measures].[Total Sales] AS 'Aggregate({[Date].[Calendar Year].&[2022], [Date].[Calendar Year].&[2023]})'
SELECT
    [Measures].[Total Sales] ON COLUMNS
FROM [Adventure Works]

MDX Function Categories

Category Description Example Function
Set Functions Manipulate sets of members or tuples. NON EMPTY, HEAD, TAIL
String Functions Perform operations on string data. UPPER, LEFT, LEN
Numeric Functions Perform mathematical operations. SUM, AVG, ROUND
Time-Based Functions Work with time-based data and calculations. ParallelPeriod, SamePeriodLastYear
Aggregate Functions Calculate aggregations over sets. Aggregate, Count

MDX Statements

Resources