Data Analysis Expressions
Microsoft Reference | SQLBI | DAX Guide | DAX Formatter | DAX Do | DAX Patterns
Programming language that resembles Excel
- Power Pivot
- Power BI
- Designed for data models and business calculations.
DAX
is a functional language, the execution flows with function calls.
It means, calculations mostly use functions
to generate the results.
DAX
is designed for enhancing Data Modeling
, Reporting
and Analytics
capability.
In Excel
we consider Cell
Reference but in Power BI reference is given either to Table
or to Column
SUM (
FILTER (
VALUES ( 'Date'[Year] ), // Distinct Year
'Date'[Year] < 2005 ), // Year before 2005
IF (
'Date'[Year] >= 2000, // Condition | Expression
[Sales Amount] * 100, // If TRUE
[Sales Amount] * 90 // If FALSE
)
)
)
Selection of the accurate data type helps to reduce the size
of a data model and improve performance
when to refresh
data and use of any report.
Whole
NumberDecimal
Number ( floating point number )Boolen
( TRUE / FALSE )Text
( String )Currency
( Fixed Decimal Number )DateTime
Date
Time
String
( Unicode String )Variant
( Used for expressions that returns different data type )
Variant
data type is only used for Measure
and in general DAX
expressions.
IF ( [Age] >= 18, 1, "Not Allowed" ) // Variant
Arithmetic Operator | |
---|---|
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Exponent | ^ |
Any argumnet passed as string
is automatically converted into a number
+
: Adds two numbers, Any argument passed as a string is automatically converted into a number ( e.g. "5"
+ "5"
= 10
)
/
: Divides Numerator with Denominator ( Return Error
if Denominator is 0
)
/
Operator and DIVIDE
function is different ( DIVIDE
does not return error if Denominator is 0
)
Comparison Operator | ||
---|---|---|
Equal to | = | [City] = 'Mumbai' |
Strictly equal to | == | [Price] == BLANK() |
Not equal to | <> | [City] <> "Mumbai" |
Greater than | > | [Age] > 18 |
Less than | < | [Age] < 18 |
Greater than or Equal to | >= | [Age] >= 18 |
Less than or Equal to | <= | [Age] <= 18 |
DAX is case insensitive
, while comparing dax
& DAX
are equal.
=
: Compares two value ( Returns TRUE
if the value is BLANK()
or 0
or Empty String ""
)
==
: Strictly
Equal to ( Return TRUE
only if value is actually BLANK() and FALSE
if the value is 0
, ""
or any other value )
Logical Operator | |
---|---|
&& | [State] = "MH" && [Country] = "IND" |
|| | [State] = "MH" || [Country] = "IND" |
IN | [Region] IN {"AMS","APJ","EMEA"} |
AND
( A, B ) or A &&
B ( Return TRUE
only if both are TRUE
and FALSE
if any one is FALSE
)
OR
( A, B ) or A ||
B ( Return TRUE
if any one is TRUE
and FALSE
only if both are FALSE
)
Text Operator | |
---|---|
& | [City] & " " & [State] ( Concatenate ) |
&
: Concatenates two Strings ( "Hello" &
" " &
"World" : Hello World )
Operator Overloading
: Results are based on the operators
used.
e.g.
- "5" + "4" = 9 ( Arithmetic Operation )
- Here even if we try to add numbers within
quotes
DAX converts string to integers andadd
the numbers. DAX
knows that+
is used toAdd
numbers.
- 5 & 9 = 59 ( Concatenation )
- Here due to
&
DAX will consider Integers as string andconcatenate
the strings.
There are more than 200
DAX functions, there are 9
categories in DAX function.
Date
andTime
Time Intelligence
Information
Logical
Mathematical
Statistical
Text
Parent
/Child
Other
- A
Data Model
consists ofData
,Calculations
andFormatting
rules and it combines to create an object. - This object helps to
Explore
andUnderstand
theDataset
Data
Tables
Columns
Relationships
Measures
Hierarchies
There are 3
types of Calculations in DAX
- Calculated
Columns
- Calculated
Measures
- Calculated
Tables
- Column computed using a
DAX
language. - Calculation happens
row
byrow
and stored in the model. - Consumes
memory
in the model.
- Computes at
aggregate
orreport
level. - Useful to calculate
percentage
,ratio
andaggregations
- Columns cannot be directly referenced in the Measure, it will be always surrounded by some
Aggregate
function. - Consumes
CPU
at query time.
- Creates new table or slice the subset from some existing table.
- Consumes
memory
in the model.
Data
orFact
Table : Containsquantitative
values ( cost, quantity and prices )Lookup
orDimension
Table : Providesdescriptive
attributes about each dimension.Foreign
Key : Containsmultiple
instances of each value, and are used to match thePrimary
keys in relatedLookup
tables.Primary
Key : Uniquely identifies eachrow
of a table, and matchForeign
keys in relatedFact
tables.Cardinality
: Theuniqueness
of values in the column.
- Use
Star
schema (One to Many) relationship. - Always create a relationship with
one way
filters. - Only include the data you need for analysis.
- Split out individual
Date
andTime
components fromDateTime
field. Disable
the refreshing of Data if that do not need refresh everytime from the Power Query Editor.
- When we increase the number of
columns
the number ofrows
also increases because the combination increases. - The limit of excel is
Million
rows. - When then limit exceeds we need
Power Pivot
orPower BI
- The
VAR
keyword introduces variables in an expression. Variables
make the calculation easier to understand.- Writing any complex or
nested
expression usingDAX
functions, variables can help to break these complex calculations into smaller, more useful sections. Reduce
complexity, Easy toDebug
, Improvereadability
and Improveperformance
- The results ( defined value or evaluated expressions ) of
VAR
statement is returned byRETURN
statement.
VAR Pi = 3.14 // Defined
VAR AreaOfCircle = SUMX ( Math, Pi * Math[Radius] * Math[Radius] ) // Expression
RETURN AreaOfCircle
- The
RETURN
keyword consumes variables defined in previousVAR
statements. - It access to all expressions and results defined in the
VAR
statements before or aboveRETURN
.
VAR Name = "Kirankumar"
RETURN Name
Context
is howDAX
apply layers offilters
to calculations and tables.- Used in the calculations so that they
return
relevant results for every value. - Produce a result related to each and every value of a visual or pivot table including rows and columns total.
Row Context
is related to currentrows
- If you create a
Calculated Column
, theRow Context
involves the values of all theColumns
( entireRow
) - If that table has a
relationship
with other table, then it includes all therelated
values from thatTable
for thatRow
- In
Iterative
functions inDAX
over table, eachRow
has its ownRow Context
- Applying
Filters
on set of values ofColumns
orTables
usingDAX
calculations. Filter Context
applies on the top ofRow Context
andQuery Context
- Move from
One
side toMany
side. - e.g. Filter on category will automatically filter the sub categories and further it will filter the products.
Rows
orColumns
- By
Slicer
- Through
Filter Pane
- To the
Calculated Measure
- Combination of
Row Context
andFilter Context
creates a final query forDAX
- Users explicitly mention
Row Context
andFilter Context
forDAX
DAX
implicitly creates theQuery Context
from thatRow Context
andFilter Context
Individual
functions : Left to Right ( Startting from first parameter and following the order )
IF(Logical, Return IF True, Return IF False)
Nested
functions : Inside Out ( Start from innermost function and work outward )
= SUMX (
FILTER (
RELATED ( )
)
)
Helps us to identify missing
data. ( Quality assurance and testing )
IFERROR()
: IFERROR(Value, ValueIfError)
Error Check =
IFERROR (
1/0,
BLANK()
)
ISBLANK()
: ISBLANK(Value)
IF (
ISBLANK (
[Sales (Last Year)]
),
"No Sales",
[Sales (Last Year)]
)