Microsoft Learn

Documentation for DAX (Data Analysis Expressions)

Introduction to DAX

Data Analysis Expressions (DAX) is a formula expression language used in Power BI, Analysis Services, and Power Pivot in Excel. It's designed for working with data model data, enabling you to create custom calculations and retrieve data from your models.

DAX formulas are similar to spreadsheet formulas, but they operate on tables and columns within your data model. Mastering DAX is crucial for unlocking the full potential of these tools and building powerful analytical solutions.

Core Concepts

Evaluation Context

The evaluation context is one of the most fundamental concepts in DAX. It determines how a DAX expression is evaluated. There are two primary types of evaluation contexts:

Filter Context

The filter context defines the set of filters applied to the data model before a DAX expression is evaluated. This context is created by slicers, filter panes, row and column headers in PivotTables, and other filters applied within DAX formulas themselves. It's how you specify "what data do I want to see?".

Row Context

The row context refers to the current row being processed in a table. This is common when iterating over tables, such as in calculated columns or when using iterator functions. It allows you to perform calculations based on the values in the current row.

Tip: Understanding how filter context and row context interact is key to writing correct and efficient DAX. Functions like CALCULATE are used to modify the filter context.

DAX Functions

DAX provides a rich library of functions categorized by their purpose. Here are some common categories:

Aggregation Functions

These functions perform calculations across a set of values, such as summing, averaging, or counting. Examples include:

Time Intelligence Functions

These functions are specifically designed for performing calculations on data that spans across dates and times. They are essential for financial reporting and trend analysis. Examples include:

Logical Functions

These functions perform conditional tests and return different results based on those tests. Examples include:

Text Functions

These functions manipulate text strings. Examples include:

Iterator Functions

These functions iterate over each row of a table and perform an expression for each row. They are powerful for complex calculations that require row-by-row logic. Examples include:

-- Example of SUMX to calculate total sales amount per product
TotalSalesPerProduct =
SUMX(
    Products,
    Products[Quantity] * Products[Price]
)

Creating Measures

Measures are dynamic calculations that respond to the context in which they are used. They are typically used in visualizations to aggregate data. Measures are created using DAX formulas and do not consume memory for each row in the underlying table.

Note: Measures are evaluated at query time, making them ideal for aggregations that need to be filtered by user interactions.
-- Example Measure: Total Sales Amount
Total Sales = SUM(Sales[SalesAmount])

Calculated Columns

Calculated columns are new columns added to a table, where the values are computed based on a DAX formula row by row during data refresh. They consume memory and are stored within the data model.

-- Example Calculated Column: Full Name
FullName = Customers[FirstName] & " " & Customers[LastName]

Best Practices

Performance Tuning

Efficient DAX is crucial for responsive reports and analyses. Key considerations include:

Important: DAX performance is an ongoing area of learning. Tools like DAX Studio and Performance Analyzer in Power BI can help identify bottlenecks.