Excel Dax Tutorial PDF
Excel Dax Tutorial PDF
Audience
This tutorial has been designed for all those readers who depend heavily on MS-Excel to
prepare charts, tables, and professional reports that involve complex data. It will help all
those readers who use MS-Excel regularly to analyze data. Professionals who use data
modeling and data analysis for reporting and decision-making purposes will benefit from
this.
Prerequisites
This tutorial is an extension to Excel Power Pivot tutorial, hence it is a good idea to brush
up on the Excel Power Pivot tutorial before you delve into DAX. Knowledge of Excel
Functions and Excel Formulas is not necessary for this tutorial, as DAX is entirely for the
Data Model in the Power Pivot window.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com.
i
DAX
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
DAX Formulas.......................................................................................................................................... 2
DAX Syntax.............................................................................................................................................. 3
DAX Formulas.......................................................................................................................................... 4
ii
DAX
iii
DAX
How DAX Handles Blanks, Empty Strings, and Zero Values? .................................................................. 50
7. DAX ─ OPERATORS............................................................................................................. 52
iv
DAX
v
DAX
Dependencies...................................................................................................................................... 107
vi
DAX
Date Based on Date Parts Extracted from a Text Date ......................................................................... 129
23. DAX SCENARIOS ─ CONDITIONAL VALUES & TESTING FOR ERRORS ................................. 135
Applying a Filter to Show only the Top Few Items ............................................................................... 141
vii
1. DAX ─ OVERVIEW DAX
DAX stands for Data Analysis Expressions. DAX is a formula language and is a collection of
functions, operators, and constants that can be used in a formula or expression to calculate
and return one or more values. DAX is the formula language associated with the Data Model
of Excel Power Pivot.
It is not a programming language, but is a formula language that allows the users to define
custom calculations in calculated columns and calculated fields (also known as measures).
DAX helps you create new information from the data that is already present in your Data
Model. DAX formulas enable you to perform data modeling, data analysis, and use the results
for reporting and decision making.
DAX includes some of the functions that are used in Excel formulas, but with modified
functionality and additional functions that are designed to work with relational data and
perform dynamic aggregation.
Importance of DAX
The foundation of DAX is the Data Model that is the Power Pivot database in Excel. Data Model
consists of tables between which relationships can be defined so as to combine the data from
different sources. The data connections to the Data Model can be refreshed as and when the
source data changes. Data Model makes the use of the Power Pivot xVelocity in-memory
analytics engine (VertiPaq) that makes the data operations to be as quick as possible in
addition to accommodating several thousands of rows of data. For more information on Data
Model, refer to the tutorial – Power Pivot.
DAX in conjunction with Data Model enables several power features in Excel – Power Pivot,
Power PivotTables, Power PivotCharts and Power View. You can use DAX to solve a number
of basic calculations and data analysis problems.
DAX is also useful in Power BI to create a new Power BI Desktop file and import some data
into it. Further, DAX formulas provide capabilities such as analyzing growth percentage across
product categories and for different date ranges, calculating year-over-year growth compared
to market trends and many others as well.
Learning how to create effective DAX formulas will help you get the most out of your data.
When you get the information you need, you can begin to solve real business problems that
affect your bottom line. This is the power in Power BI and DAX will help you get there.
It would be a good idea to brush up on this tutorial before you delve into DAX as this tutorial
is more on the DAX language wherein you write formulas for the analysis of data in the Data
Model and report those results.
This tutorial also introduces DAX Functions that are like Excel Functions, but with some
variations. A comparison of Excel Functions and DAX Functions is provided to help you
distinguish both. Similarly, Excel formulas and DAX formulas are compared and the similarities
and differences are discussed. A good understanding of these differences would help you in
writing effective DAX formulas efficiently.
Knowledge of Excel Functions and Excel Formulas is not necessary for this tutorial, as DAX is
entirely for the Data Model in the Power Pivot window. You will get into an Excel worksheet
only to view the Power PivotTables, Power Pivot Charts and Power View visualizations that are
based on Data Model. However, if you are an Excel professional with good amount of
knowledge in Excel Functions and Formulas, better make a note of what is mentioned in the
previous section and the details given in the course of this tutorial.
Calculated Columns
Calculated columns are the columns that you can add to a table in the Data Model, by means
of a DAX formula. You have already learnt about them in Excel Power Pivot tutorial, but you
will learn in detail in the chapter – Calculated Columns as DAX is all about calculated columns,
calculated fields, and DAX functions.
The calculated fields were named as measures in the Excel versions prior to Excel 2013. They
are renamed back to measures in Excel 2016. In this tutorial, we will refer them as calculated
fields. But, note that the terms - calculated fields and measures - are synonymous and refer
to the same in all aspects.
You can edit a calculated field after it is defined and stored. You can change the DAX formula
used in the definition or you can rename the calculated field. You will learn about this in the
chapter – Editing a Calculated Field. You can delete a calculated field. Refer to the chapter
– Deleting a Calculated Field.
DAX Formulas
DAX formulas form the heart of the DAX language. You can create calculated fields and
calculated columns by defining them with DAX formulas. You can write DAX formulas for the
9
DAX
data analysis operations. DAX formulas do not refer to the individual cells or range of cells in
the table, but refer to the tables and columns in the Data Model. A column in a table in the
Data Model must contain the same data type.
DAX formulas contain the tables, columns, calculated columns, calculated fields, DAX
operators, and DAX functions. Refer to the chapter – DAX Formulas to learn in detail.
DAX Syntax
As is the case with any language, DAX, the formula language also has a syntax. Your DAX
formulas should follow DAX syntax, or else, you will either get errors at design time or at run
time or you will receive incorrect results.
DAX Operators
DAX is a formula language and hence makes the use of the operators in defining the formulas.
DAX has the following types of operators –
DAX Functions
Excel 2013 has 246 DAX functions that you can use in DAX formulas. You will learn about
these functions at the category level in the chapter – DAX Functions. However, for details
10
DAX
on each DAX function syntax, parameters, usage and return values, you have to refer to our
tutorial on – DAX Functions. The section names used for the description of each DAX function
is given in the chapter – Understanding DAX Functions.
As DAX functions are required in writing the DAX formulas and the results of the DAX functions
used depend on the context they are used, you might have to go back and forth between
these two tutorials to get a grasp on DAX that you will use in Data Modeling with DAX and
Power BI.
DAX Formulas
DAX is a formula language and you have to get the most of it in writing the DAX formulas.
Refer to the chapter - DAX Formulas to learn about the formula syntax and how to create
them easily and correctly.
The results of the DAX formulas change whenever the data is refreshed and whenever the
DAX formulas are recalculated. You have to understand the difference between data refresh
and recalculation. Refer to the chapter - Updating the Results of DAX Formulas.
Data in the Data Model is expected and subjected to change from time to time. This is because
the data is used for data analysis activities that require up-to-date data at any point of time.
To understand the different ways of refreshing data, refer to the chapter - Updating Data in
Data Model.
You will understand the different types of DAX formula recalculation in the chapter -
Recalculating DAX Formulas.
DAX formula recalculations have to consider data dependencies and follow a specific order.
Otherwise, you might get errors or erroneous results. Refer to the chapter - Troubleshooting
DAX Formula Recalculation for details.
You will get an insight into some of the common DAX formula errors and you will learn how
to fix those errors, in the chapter - DAX Formula Errors.
11
DAX
DAX Scenarios
If you start learning a new language, the best way of getting acquainted to the language is
by understanding where to use what. Similarly, DAX being a formula language meant for data
analysis, you need to understand the various scenarios where it can be used.
DAX Scenarios
Scenarios - Performing Complex Calculations
Scenarios - Working with Text and Dates
Scenarios - Conditional Values and Testing for Errors
Scenarios - Using Time Intelligence
Scenarios - Ranking and Comparing Values
12
2. DAX ─ CALCULATED COLUMNS DAX
A calculated column is a column that you add to an existing table in the Data Model of your
workbook by means of a DAX formula that defines the column values. Instead of importing
the values in the column, you create the calculated column.
You can use the calculated column in a PivotTable, PivotChart, Power PivotTable, Power
PivotChart or Power View report just like any other table column.
For example, you can create one calculated column to extract Year from the existing column
– Date, with the DAX formula –
=YEAR ([Date])
YEAR is a DAX function and Date is an existing column in the table. As seen, the table name
is enclosed in brackets. You will learn more about this in the chapter – DAX Syntax.
When you add a column to a table with this DAX formula, the column values are computed
as soon as you create the formula. A new column with the header CalculatedColumn1 filled
with Year values will get created.
Column values are recalculated as necessary, such as when the underlying data is refreshed.
You can create calculated columns based on existing columns, calculated fields (measures),
and other calculated columns.
13
DAX
14
DAX
As seen in the above screenshot, the rightmost column has the header – Add Column.
15
DAX
The pointer will appear in the formula bar. That means you are adding a column with a DAX
formula.
16
DAX
As can be seen in the above screenshot, the rightmost column with the header – Add Column
is highlighted.
Press Enter.
It will take a while (few seconds) for the calculations to be done. Please wait.
The new calculated column will get inserted to the left of the rightmost Add Column.
17
DAX
As shown in the above screenshot, the newly inserted calculated column is highlighted. Values
in the entire column appear as per the DAX formula used. The column header is
CalculatedColumn1.
18
DAX
As seen in the above screenshot, the name of the calculated column got changed.
19
DAX
You can also rename a calculated column by right-clicking on the column and then clicking on
Rename in the dropdown list.
Just make sure that the new name does not conflict with an existing name in the table.
As you can see in the above screenshot, the dropdown list has the possible data types for the
columns. In this example, the default (Auto) data type, i.e. the Whole Number is selected.
Changing or deleting relationships between the tables. This is because the formulas
that use columns in those tables will become invalid.
The formula contains a circular or self-referencing dependency.
20
DAX
Performance Issues
As seen earlier in the example of Olympics results, the Results table has about 35000 rows
of data. Hence, when you created a column with a DAX formula, it had calculated all the
35000+ values in the column at once, for which it took a little while. The Data Model and the
tables are meant to handle millions of rows of data. Hence, it can affect the performance
when the DAX formula has too many references. You can avoid the performance issues doing
the following –
If your DAX formula contains many complex dependencies, then create it in steps
saving the results in new calculated columns, instead of creating a single big formula
at once. This enables you to validate the results and assess the performance.
Calculated columns need to be recalculated when data modifications occur. You can
set the recalculation mode to manual, thus saving frequent recalculations. However, if
any values in the calculated column are incorrect, the column will be grayed out, until
you refresh and recalculate the data.
21
DAX
22