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.
WholeNumberDecimalNumber ( floating point number )Boolen( TRUE / FALSE )Text( String )Currency( Fixed Decimal Number )DateTimeDateTimeString( 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
quotesDAX converts string to integers andaddthe numbers. DAXknows that+is used toAddnumbers.
- 5 & 9 = 59 ( Concatenation )
- Here due to
&DAX will consider Integers as string andconcatenatethe strings.
There are more than 200 DAX functions, there are 9 categories in DAX function.
DateandTimeTime IntelligenceInformationLogicalMathematicalStatisticalTextParent/ChildOther
- A
Data Modelconsists ofData,CalculationsandFormattingrules and it combines to create an object. - This object helps to
ExploreandUnderstandtheDataset
DataTablesColumnsRelationshipsMeasuresHierarchies
There are 3 types of Calculations in DAX
- Calculated
Columns - Calculated
Measures - Calculated
Tables
- Column computed using a
DAXlanguage. - Calculation happens
rowbyrowand stored in the model. - Consumes
memoryin the model.
- Computes at
aggregateorreportlevel. - Useful to calculate
percentage,ratioandaggregations - Columns cannot be directly referenced in the Measure, it will be always surrounded by some
Aggregatefunction. - Consumes
CPUat query time.
- Creates new table or slice the subset from some existing table.
- Consumes
memoryin the model.
DataorFactTable : Containsquantitativevalues ( cost, quantity and prices )LookuporDimensionTable : Providesdescriptiveattributes about each dimension.ForeignKey : Containsmultipleinstances of each value, and are used to match thePrimarykeys in relatedLookuptables.PrimaryKey : Uniquely identifies eachrowof a table, and matchForeignkeys in relatedFacttables.Cardinality: Theuniquenessof values in the column.
- Use
Starschema (One to Many) relationship. - Always create a relationship with
one wayfilters. - Only include the data you need for analysis.
- Split out individual
DateandTimecomponents fromDateTimefield. Disablethe refreshing of Data if that do not need refresh everytime from the Power Query Editor.
- When we increase the number of
columnsthe number ofrowsalso increases because the combination increases. - The limit of excel is
Millionrows. - When then limit exceeds we need
Power PivotorPower BI
- The
VARkeyword introduces variables in an expression. Variablesmake the calculation easier to understand.- Writing any complex or
nestedexpression usingDAXfunctions, variables can help to break these complex calculations into smaller, more useful sections. Reducecomplexity, Easy toDebug, Improvereadabilityand Improveperformance- The results ( defined value or evaluated expressions ) of
VARstatement is returned byRETURNstatement.
VAR Pi = 3.14 // Defined
VAR AreaOfCircle = SUMX ( Math, Pi * Math[Radius] * Math[Radius] ) // Expression
RETURN AreaOfCircle
- The
RETURNkeyword consumes variables defined in previousVARstatements. - It access to all expressions and results defined in the
VARstatements before or aboveRETURN.
VAR Name = "Kirankumar"
RETURN Name
Contextis howDAXapply layers offiltersto calculations and tables.- Used in the calculations so that they
returnrelevant 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 Contextis related to currentrows- If you create a
Calculated Column, theRow Contextinvolves the values of all theColumns( entireRow) - If that table has a
relationshipwith other table, then it includes all therelatedvalues from thatTablefor thatRow - In
Iterativefunctions inDAXover table, eachRowhas its ownRow Context
- Applying
Filterson set of values ofColumnsorTablesusingDAXcalculations. Filter Contextapplies on the top ofRow ContextandQuery Context- Move from
Oneside toManyside. - e.g. Filter on category will automatically filter the sub categories and further it will filter the products.
RowsorColumns- By
Slicer - Through
Filter Pane - To the
Calculated Measure
- Combination of
Row ContextandFilter Contextcreates a final query forDAX - Users explicitly mention
Row ContextandFilter ContextforDAX DAXimplicitly creates theQuery Contextfrom thatRow ContextandFilter Context
Individualfunctions : Left to Right ( Startting from first parameter and following the order )
IF(Logical, Return IF True, Return IF False)
Nestedfunctions : 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)]
)