Introduction to DAX (Data Analysis Expressions)
DAX is a formula expression language used in Power BI, Analysis Services, and Power Pivot in Excel. It is designed for working with tabular data models and allows you to create custom calculations, derive new information from existing data, and perform complex data analysis.
On This Page
Why DAX?
DAX provides the power to transform raw data into meaningful insights. Its primary benefits include:
- Advanced Calculations: Create complex business logic and financial calculations.
- Data Modeling: Define relationships and enhance your data model's analytical capabilities.
- Performance: DAX is highly optimized for analytical queries.
- Consistency: Ensures consistent calculations across reports and applications using the same data model.
Key DAX Concepts
Understanding these core concepts is crucial for effective DAX development.
Measures
Measures are formulas that are evaluated at query time, based on the current filter context. They are not stored in the table itself but are dynamically calculated. Measures are typically used for aggregations like sums, averages, counts, and more complex business calculations.
Syntax: MeasureName = DAX Expression
Calculated Columns
Calculated columns are new columns added to a table where each row's value is computed during data import or refresh. They are stored in the model and consume memory. Calculated columns are useful for categorizations, concatenations, or simple transformations that apply to every row.
Syntax: NewColumnName = DAX Expression
Row Context
Row context refers to the current row being processed in an iteration. This is most commonly encountered when using iterator functions (like SUMX, AVERAGEX) or when defining calculated columns.
Filter Context
Filter context is the set of filters that are applied to the data model at the time a DAX expression is evaluated. This context is created by slicers, filters in visuals, and other DAX expressions. Measures operate within this context.
Evaluation Context
DAX evaluation context is a fundamental concept that combines row context and filter context. Understanding how DAX engines evaluate expressions within these contexts is key to writing correct and efficient formulas.
Getting Started Writing DAX
Let's dive into writing your first DAX formulas.
Basic Syntax
DAX formulas generally follow a pattern:
- Formulas start with an equals sign (
=). - Functions are typically used to perform operations.
- Arguments are enclosed in parentheses (
()). - Arguments are separated by commas (
,). - Table and column names are enclosed in single quotes (
') if they contain spaces or special characters, otherwise they are not strictly required but good practice.
Common Functions
Here are a few fundamental DAX functions:
- Aggregation Functions:
SUM(),AVERAGE(),COUNT(),MIN(),MAX(). - Iterator Functions:
SUMX(),AVERAGEX(),FILTER(). These iterate over tables. - Logical Functions:
IF(),AND(),OR(). - Time Intelligence Functions:
CALCULATE(),TOTALYTD(),SAMEPERIODLASTYEAR(). - Relationship Functions:
RELATED(),RELATEDTABLE().
Example 1: Simple Measure (Total Sales)
Let's assume you have a table named Sales with a column named Amount. To calculate the total sales, you would create a measure like this:
Total Sales = SUM(Sales[Amount])
This measure will sum up all values in the Amount column of the Sales table. When placed in a visual, it will respect any filters applied to the visual or report.
Example 2: More Complex Measure (Sales Year-to-Date)
To calculate sales from the beginning of the year up to the current date:
Sales YTD =
TOTALYTD(
SUM(Sales[Amount]),
'Date'[Date]
)
This example uses the TOTALYTD function, which requires a measure (SUM(Sales[Amount])) and a date column from a dedicated Date table (recommended).
Important Note:
It is highly recommended to have a dedicated Date table in your data model for time intelligence calculations. This table should be marked as a Date table in your model.
Next Steps
Once you're comfortable with the basics, you can explore more advanced topics:
- The
CALCULATE()function is arguably the most powerful function in DAX. - Mastering different filter context modifications.
- Understanding iteration vs. aggregation.
- Exploring specific time intelligence scenarios.
- Learning about variables in DAX for readability and performance.
Continue your learning journey by exploring DAX function references and practical examples in your specific analytical scenarios.