DAX For Power BI: Sample Manual - First Two Chapters
DAX For Power BI: Sample Manual - First Two Chapters
13.2 Creating a Custom Calendar 103 14.2 Returning a Single Value 115
The CALENDARAUTO and CALENDAR 103 The VALUES Function 115
Functions Testing for a Single Value 116
Adding Extra Columns 104 The SELECTEDVALUE Function 116
Financial Years 105
14.3 Concatenating Values 117
13.3 Finishing the Calendar 106
The CONCATENATEX Function 117
Marking as a Date Table 106 More Complex Expressions 117
Changing Default Aggregations 106
Setting Sort-By Columns 107 14.4 Filtered Values 118
Creating Hierarchies 107 Testing for Filtered Values 118
Hiding Fields 108 Testing for Cross Filtered Values 118
Creating a Relationship 108 Selecting the Top N Rows 119
13.4 Using a Custom Calendar 109 14.5 Disconnected Slicers 120
Creating Visuals 109 Creating a Disconnected Table 120
Time Intelligence Functions 109 Creating a Disconnected Slicer 121
Referencing the Selected Value 121
13.5 Multiple Date Fields 110
Using Multiple Calendars 110 14.6 Formatting with Measures 122
Using a Single Calendar 111 Calculating Colours with Measures 122
Changing the Active Relationship 111 Using Measures in Conditional 123
The USERELATIONSHIP Function 112 Formatting
Choosing Colours with Slicers 123
13.6 Special Dates 113
In Power BI you can use the DAX (Data Analysis eXpressions) language to create calculated
columns, measures and tables. You can see an example of each below.
A calculated column is like a formula in an Excel table. The results of a calculated column are stored
in the data model.
As we will see in this manual, a calculated column is evaluated for each row of a table; DAX uses
the row context to access the correct values for each separate calculation.
A measure is a formula which calculates a value when you place it in a visual. The visual provides
a filter context which tells the measure which values it can use in the calculation.
Although most of the tables in your Power BI data model will be created by importing data, you can
also use DAX to calculate tables. It’s common to use this technique to create custom calendars.
Power BI provides several ways to create calculated columns, measures and tables. One way is to
click the relevant tool on the Home tab of the ribbon while in the Data view.
Select the Data view tool on the left of the Power BI window.
In addition to Power BI, you can write DAX in the following applications:
Application Description
Power Pivot Power Pivot is an add-in for Excel which allows you to combine data from
multiple sources and present this in a pivot table or chart.
SQL Server Analysis Services SSAS Tabular Model allows you to combine data from lots of different data
(SSAS) Tabular sources, apply security to it to control who sees what and then allow
employees of your organisation to share the resulting data model.
This courseware uses data from the (fictitious) Wise Owl subsidiary Construct-a-Creature (a retail
chain loosely modelled on Build-a-Bear, but with a wider range of animals available for purchase).
You can see the tables and relationships of the database in the diagram below:
There are four tables representing a geographical dimension to do with where sales took place. These tables give
the shopping centre, type of centre, town and region.
There are also four tables representing a product dimension. These give The Sales table stores how many
details of the product sold (for example, a frog is an amphibian which of each product were sold in each
lives in a fresh water habitat in a watery environment). transaction.
The amount of help available for DAX both within Power BI and from third parties has increased
dramatically since the product was first released.
You’ll see various popups appear to help you as you write DAX in Power BI.
You can find more descriptive help for DAX on a range of websites, as shown in the table below:
A calculated column is a type of calculation you can create using DAX in Power BI. This chapter
shows you the basics of writing DAX using calculated columns.
You can see some of the key features of calculated columns in the table below:
Feature Description
Created in data tables You create a calculated column in a table in the data model. The calculated
column can refer directly to any column in the same table.
Calculated immediately A calculated column produces its results as soon as you enter it. The values
are updated whenever the data model is refreshed.
Stores data in the model A calculated column stores its results in the data model. Each calculated
column you create increases the storage space required by the model.
Uses row context The expression in a calculated column is evaluated for each row in the table.
The row context provides the expression with access to values on the same
row in the table.
You can create a calculated column in any of the three Power BI views but, if you want to see the
results of your calculation, it’s best to select the Data view.
You can choose New column from the You can right-click the table in the Data
Table tools or Home tabs of the ribbon panel and choose New column to add a
to add a calculated column. calculated column.
Entering a Formula
After choosing to create a calculated column you can enter your DAX code in the formula bar below
the ribbon. The example below multiplies one column by another to create a new value:
2) To reference a column in the same table, simply begin typing the column name.
You can also click the tick When you complete the formula, DAX calculates
to commit the formula. an answer for each row of the table.
Your calculated column will appear in the table in the Data pane with a special symbol to indicate its
status.
Once you’ve created a calculated column, you can modify it in the same way as any other column
in your data model, as shown in the diagram below.
You can apply formatting to the column You can assign a data category to the column
using these tools. using this drop-down list.
You can use a calculated column to populate visuals in your report, just as for any other column in
your data model.
This section shows you various ways to refer to columns and tables in your DAX formulae.
When you select a column name from the Intellisense list, it will be automatically qualified with the
name of the table to which it belongs. You don’t have to include the table name, however.
Although you don’t always have to include the table name when referencing a column, it makes
sense to do so for the following reasons:
• If you always include the table name you don’t have to remember when it is or isn’t required.
• It allows you to spot when you’re referring to a column as opposed to, say, a measure.
• When a column name exists in multiple tables it makes the reference unambiguous.
Table Names
If the name of a table contains spaces, or it conflicts with another DAX keyword, you must enclose
the table name in single quotes.
Wise You can always enclose a table name in single quotes, even when it isn’t required.
Owl’s
Hint
In a calculated column you can only directly reference other columns which belong to the same table.
This is a problem when your calculation needs to refer to columns in other tables!
The key to solving the above problem is that the Product and Sales tables have a relationship. This
allows us to use the RELATED function to reference a column in a related table.
You can reference columns from a table at the 1 end of a relationship, regardless of how many steps
it takes. In our model, the Sales table sits at the many end of every relationship:
A calculated column in
the Sales table can
reference any other
column in the data
model using the
RELATED function.
Although you can write a DAX formula as a continuous stream of code, there are several things you
can do to make your code more readable.
Wise It’s not important to understand what the code in this section does – instead, focus
Owl’s
Hint
on the techniques used to make it more readable.
You can break a formula onto multiple lines and add tab spaces to make it more readable. You can
use the following keys to add new lines and indenting to a formula:
You can see an example of a formula with new lines and indenting in the diagram below:
Comments
You can use comments to annotate your code. You can add a comment at the end of any line after
the = sign in a formula.
Keyboard Shortcuts
You can use a range of keyboard shortcuts to help you edit your DAX code. You can see some of
these in the table below:
Microsoft Excel
VBA macros
Office
Office Scripts
Microsoft Access
Power Apps
SQL
Reporting Services
SQL Server
Report Builder
Integration Services
Analysis Services
Visual C#
Coding and AI
VB programming
AI tools
Python