Introduction to DAX Language

antoniosch 1,023 views 41 slides Apr 05, 2020
Slide 1
Slide 1 of 41
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41

About This Presentation

Introduction to DAX Language


Slide Content

Please mute
your mic

AntoniosChatzipavlis
Data Solutions Consultant & Trainer
Since 1999
30+
Years in a Field
20+
Experience with
60+
Certifications
SQLschool.gr
Founder

A community for Greek professionals who use the
Microsoft Data Platform
Connect/Explore/ Learn
@antoniosch-@sqlschool
./sqlschoolgr-./groups/sqlschool
yt/c/SqlschoolGr
SQLschool.gr Group
[email protected]
Join us
Articles
SQL Saturday Nights
SQL Server in Greek
Webcasts
News
Resources

Connect/Explore/ Learn
V.10
2010-2020

Presentation Content
Introduction to
DAX
Language
•Overview
•Language Fundamentals
•Context
•Queries

Overview

•DAX is the programming language of Microsoft Power BI,
Microsoft Analysis Services, and Microsoft Power Pivot for
Excel.
-Created in 2010, with the first release of PowerPivot for Microsoft Excel
2010
•DAX has been designed to enhance data modeling,
reporting, and analytics.
•DAXis described as a functional language
-Calculations primarily use functions to generate results.
•Unlike T-SQL, there is no concept of INSERT, UPDATE, or
DELETE for manipulating data in a data model.
What is DAX?
Data Analysis eXpressions

A data model is a collection of data,
calculations, and formatting rules that
combine to create an object that can be
used to explore, query, and better
understand an existing dataset.
What is a Data Model?
FACT
MEASURES
DIMENSION
DIMENSION
DIMENSIONDIMENSION
DIMENSION

Components of a DAX Data Model
Data Tables Columns RelationshipsMeasures Hierarchies

Language Fundamentals

•DAX formulas start with the equal
sign (=) followed by an expression
•Expressions can contain functions,
operators, constants, and references
to columns
•Measure names must be enclosed in
brackets
•Table names containing spaces must
be enclosed in single quotation
marks
•Column references
-'table name'[column name]
-[column name]
DAX Syntax
CALCULATE [Total Sales]'Channel'[ChannelName]“Store”

Comments in DAX
= 'Channel'[ChannelPrice] * 'Channel'[ChannelDisc] -–Single line comment
= 'Channel'[ChannelPrice] -'Channel'[ChannelTax] // Single line comment
= IF ( 'Channel'[ChannelDisc] > 0 ,
/*
Multi-line
comment
*/
“Has discount”
)

DAX Data Types
DAX
Data Type
Power BI
Data Type
Analysis Services
Data Type
SQL Server
Data Type Stored as
Integer Whole Number Whole Number INT 64-bit integer
Decimal Decimal Number Decimal Number FLOAT 64-bit double floating
Currency Fixed Decimal NumberCurrency MONEY 64-bit integer / 10.000
DateTime DateTime, Date, TimeDate DATETIME 64-bit double floating
Boolean True/False True/False BIT Boolean
String Text Text NVARCHAR(MAX) Unicode string
Variant - - -
Binary Binary Binary VARBINARY(MAX) Binary

DAX Operators
Operator Type Symbol Use Example
Parenthesis ( ) Precedence order and grouping of arguments (5+2)*3
Text concatenation & Concatenation of strings “Athens” & [Country]
Arithmetic
+ Addition 4+3
- Subtraction / Negation 5-3 / -3
* Multiplication 4*3
/ Division 4/2
^ Exponents 2^6
Comparison
= Equal to [Country] = “Greece”
<> Not equal to [Country] <> “Greece”
> Greater than [Amount] > 100
>= Greater than or equal to [Amount] >= 100
< Less than [Amount] < 100
<= Less than or equal to [Amount] <= 100
Logical
&& AND condition between two Boolean expressions [Country]=“Greece” && [Amount]>0
|| OR condition between two Boolean expressions [Country]=“Greece” || [Amount]>0
IN Inclusion of an element in a list [Country] IN {“Greece”,”Cyprus”}
NOT Boolean negation NOT [Amount] > 100

•Variables are used to store results from DAX expressions.
•Variables can be used in any type of DAX calculation
including calculated columns, measures, and tables.
•Variables are not strictly typed and can represent any type
of object in DAX.
•Variables automatically assume the type of the object being
assigned.
•You must use a RETURN statement to close a layer of
variable scope.
•You can declare multiple variables within the same layer of
scope and you can use them with a single RETURN
statement.
•Nested variables can initialize a new layer of scope when
you use them anywhere within an expression, but all layers
of scope are ended when you use a RETURN statement.
DAX Variable
VAR varname= expression
RETURN expression
VAR myVar1 = 1
VAR myVar2 = myVar1 + 2
RETURN myVar2 * 2
Nested Measure =
VAR Level1a = 10
VAR Level1b =
VAR level2a = Level1a
VAR Leval2b = level2a * 3
RETURN Leval2b
RETURN Level1b

DAX Table constructors
Define anonymous tables directly in the code.
{ “Red”, “Blue”, “Green” }
Or
{ (“Red”), (“Blue”), (“Green”) }
Single Column
{
( 1,“Red” ),
( 2,“Blue” ),
( 3,“Green” )
}
Multi Column

DAX Conditional Statements
IF ( 20 < 30, "second", "third" ) -> "second"
IF ( 20 < 15, "second", BLANK() ) -> BLANK()
IF ( 20 < 15, "second" ) -> BLANK()
IF ( Customer[Status] = "A", "Platinum",
IF ( Customer[Status] = "B", "Gold",
IF ( Customer[Status] = "C", "Silver",
IF ( Customer[Status] = "D", "White", "None" )
)
)
)
SWITCH
(
Customer[Status],
"A", "Platinum",
"B", "Gold",
"C", "Silver",
"D", "White",
"None"
)
SWITCH
(
TRUE (),
Products[UnitPrice] < 10, "LOW",
Products[UnitPrice] < 50, "MEDIUM",
Products[UnitPrice] < 100, "HIGH",
"VERY HIGH"
)

Error Handling in DAX Expression
Conversion Errors
DAX automatically converts
values between strings and
numbers whenever the
operator requires it
Arithmetical
Operations Errors
DAX detects this errors and it
blocks any further
computation of the expression
and raises an error.
Empty or Missing
Values
DAX handles missing values,
blank values, or empty cells in
the same way, using the value
BLANK.

•BLANK value assigned to a column when
the data source contains a NULL value
•The BLANK value is automatically
converted in case it is compared with
other values
-In a numeric expression, a blank is automatically
converted into 0
-In a string expression, a blank is automatically
converted into an empty string
•ISBLANK() function checks whether a
value is blank and returns TRUE or FALSE
BLANK Function
BLANK is notthe T-SQL NULL
Sample Result
BLANK()=0 TRUE
BLANK() =“” TRUE
BLANK()-10 -10
BLANK()+18 18
5 / BLANK() INFINITY
0 / BLANK() NaN
BLANK() * 8 Blank value
BLANK() / 8 Blank value
BLANK() || BLANK() FALSE
BLANK() && BLANK() FALSE
( BLANK() = BLANK() )TRUE
( BLANK() = TRUE ) FALSE
( BLANK() = FALSE ) TRUE

IFERROR Function
Evaluates an expression and returns a specified value if the expression returns an error;
otherwise returns the value of the expression itself.
IFERROR ( <Value> , <ValueIfError> )
IFERROR ( A, B )
Equivalent of
IF ( ISERROR ( A ), B , A )

ISERROR Function
Checks whether a value is an error, and returns TRUE or FALSE.
IFERROR ( <Value> )
IF ( ISERROR ( A ), B , A )
Equivalent of
IFERROR ( A, B )

•Date and time functions
•Time-intelligence functions (DAX)
•Filter functions
•Information functions
•Logical functions
•Math and Trigonometry functions
•Other functions
•Parent and Child functions
•Statistical functions
•Text functions
DAX Functions
More than 200 functions in several categories

•DAX provides several functions
•Functions operate according to the filter context.
•Function with X suffix are suitable to aggregate the results of a more
complex expression
•Function with A suffix exist only for maintaining the Excel syntax
DAX Functions
How to use DAX Functions
<FunctionName> ( Table[Column] )
<FunctionNameX> ( Table, <expression )
<FunctionNameA> ( Table, <expression )

Context

Cardinality Cross Filter Directions
•Many to One (*:1)
•One to One (1:1)
•One to Many (1:*)
•Single
•Both
Relationships in Data Model
The Cornerstone of context
Relationships exist to join tables together so that you can work with them
as if they were one

Calculated Columns Measures
Calculated columns defined within tables
in the data model by adding new columns.
The expression assigned to a calculated
column is evaluated for every row of the
table
A measure is a DAX expression that is
evaluated in a context made by a set of
rows of the data model.
Context Calculations
Sales[GrossMargin] =
Sales[SalesAmount] -
Sales[TotalProductCost]
Sales[Total Sales] =
SUM ( Sales[SalesAmount] )

Contextisthelayeroffilteringthatis
appliedtocalculations,oftendynamically
toproducearesultspecifictoeveryvalue
inapivottableorvisual,includingrow
andcolumntotals.
Context
Row
Transition
Filter

Filter Context
The filter context filter

Row Context
The Row Context iterates

Row Context and Relationships
Products[NumberOrders] = COUNTROWS ( RELATEDTABLE ( Sales ) )
Products[ListAmount] = SUMX ( RELATEDTABLE ( Sales ),
Sales[OrderQuantity] * Products[ListPrice]
Sales[ListAmount] = Sales[OrderQuantity] * RELATED (Products[ListPrice])
Sales[ListAmount] = Sales[OrderQuantity] * Products[ListPrice]
RELATED Function
RELATEDTABLE Function

Context Transition
Context transition performed in a filter context under which it evaluates its expression.

Queries

EVALUATE Statement
DEFINE MEASURE
VAR
EVALUATE
ORDER BY
START AT

Relationship Queries
For SQL Developers

Search Queries
For SQL Developers

Any
questions

Thankyou!
@antoniosch-@sqlschool
./sqlschoolgr-./groups/sqlschool
./c/SqlschoolGr
SQLschool.gr Group
AntoniosChatzipavlis
Data Solutions Consultant & Trainer

A community for Greek professionals who use the Microsoft Data Platform
Copyright © SQLschool.gr. All right reserved. PRESENTER MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION