Introduction To DAX
Introduction To DAX
Introduction To DAX
What is DAX?
DAX stands for Data Analysis eXpressions, and it is the formula language used in
Power BI. DAX is also found in other offerings from Microsoft, such as Power Pivot and SSAS
Tabular.
DAX is a functional language, which means the full executed code is contained inside a
function.
There are two primary calculations you can create using DAX
Calculated Columns
Measures
Whether you're creating a New Column or Measure, it's important to know the general
format of table names in DAX.
'Table Name'[ColumnName]
If there are spaces in the table name (as shown above), the single quotes around the
table name are mandatory. If the table name has no spaces, the single quotes can be
omitted, so the syntax looks like the following. Column names must always include the square
brackets.
TableName[ColumnName]
Calculated Columns are useful when you want a calculation for every row in your
table.
You can create Calculated Columns in Power BI Desktop by selecting New Column from
the Modeling tab. It's best to be in Data view (rather than Report or Relationships view), since
you can see the New Column created and the Formula Bar is populated and ready for your
DAX formula.
Once you select the New Column button, the Formula Bar is populated with a basic
column name (which you change to suit your formula, of course) and the = operator, and the
new column appears in the data grid, as shown in the following image.
Use a calculated measure when you are calculating percentages or ratios, or you need
complex aggregations. To create a measure using a DAX formula, select the New Measure
button from the Modeling tab. Again, it's best to be in the Data view of Power BI Desktop
since it shows the Formula Bar and makes it easy to write your DAX formula.
With measures, you see a new measure icon appear in the Fields pane with the name
of the measure. The Formula Bar is again populated with the name of your DAX formula (this
time, with your measure).
The required elements for a calculated measure are the same as they are for a calculated
column
Implicit Measures are created when you drag raw numerical fields (like Sales, Profit,
Quantity) into the values field well of a visual and manually select the aggregation mode
(Sum, Average, Min/Max, etc.).
Explicit measures are created by actually entering DAX functions like below
Implicit measures are only accessible within the specific visualization in which it was
created, and cannot be referenced elsewhere.
Explicit measures can be used anywhere in the report and referenced with other DAX
calculations.
Recalculate on data source refresh or when Recalculate in response to any change to filters
changes are made to component columns within the report or when we Add / Remove
which are used to derive Calculated Columns another dimension to the visualization.
B) The equals sign operator (=) indicates the beginning of the formula. When calculated, it
will return a result.
F) The referenced column [SalesAmount] in the Sales table. With this argument, the SUM
function knows on which column to aggregate a SUM.
DAX Operators
The Data Analysis Expression (DAX) language uses operators to create expressions that
compare values, perform arithmetic calculations, or work with strings.
Types of Operators
Arithmetic Operators
To perform basic mathematical operations such as addition, subtraction, or
multiplication, combine numbers, and produce numeric results, use the following arithmetic
operators.
Comparison Operators
You can compare two values with the following operators. When two values are
compared by using these operators, the result is a logical value, either TRUE or FALSE.
Use the ampersand (&) to join, or concatenate, two or more text strings to produce a
single piece of text.
EX: State and Country = CONCATENATE (Orders [State], CONCATENATE (", ", Orders
[Country]))
Or
State and Country = Orders [State] & ", " & Orders [Country]
Logical Operators
Use logical operators (&&) and (||) to combine expressions to produce a single result.