Microsoft Learn

Tutorial Information

Product: Microsoft SQL Server Analysis Services

Level: Beginner

Estimated time: 45 minutes

Part of the Analysis Services Tutorials series.

Introduction to DAX

Data Analysis Expressions (DAX) is a formula expression language used in Analysis Services, Power BI, and Power Pivot in Excel. It's designed for working with data stored in tabular models and allows you to perform complex calculations and data manipulations.

This tutorial will guide you through the fundamental concepts of DAX, providing you with the building blocks to write your own formulas and unlock the full potential of your data models.

Why Learn DAX?

Key Concepts Covered:

Understanding DAX Syntax

DAX formulas typically start with an equals sign (=) and consist of one or more functions, along with arguments.

The basic syntax looks like this:

=FunctionName(Argument1, Argument2, ...)

For example, a simple sum calculation:

=SUM(Sales[SalesAmount])

Example: Creating a Calculated Column

Let's say we want to calculate the profit for each sale. We can add a calculated column to our 'Sales' table:

Profit = Sales[SalesAmount] - Sales[CostAmount]

This formula subtracts the `CostAmount` from the `SalesAmount` for each row in the `Sales` table, creating a new column named `Profit`.

Common DAX Functions

DAX provides a rich library of functions. Here are a few essential ones:

Aggregation Functions

Logical Functions

Aggregation Example: Total Sales

To calculate the total sales across all transactions, you would create a measure:

Total Sales = SUM(Sales[SalesAmount])

Measures are dynamic calculations that respond to the context of the report or query.

Context in DAX

Understanding context is crucial for writing effective DAX. There are two main types of context:

1. Row Context

Row context refers to the current row being evaluated. This is implicitly present when you create a calculated column, as the formula is evaluated for each row individually.

2. Filter Context

Filter context is the set of filters that have been applied to the data, either by the user interacting with a report or by DAX formulas themselves (e.g., using functions like CALCULATE()).

The CALCULATE() function is one of the most powerful in DAX, as it allows you to modify the filter context under which an expression is evaluated.

Sales in 2023 = CALCULATE( [Total Sales], Dates[Year] = 2023 )

This measure calculates the `Total Sales` but only for the rows where the `Year` in the `Dates` table is 2023.

Calculated Columns vs. Measures

It's important to distinguish between these two:

When to Use Which:

Next Steps

This tutorial provided a basic introduction to DAX. To further enhance your skills:

Continue your learning journey with more advanced tutorials on DAX and Analysis Services.