An (Advanced) Introduction to DAX

 
Eugene Meidinger
An (Advanced) Introduction to DAX
@sqlgene
www.sqlgene.com/powerbi/
emeidinger@all-lines-tech.com
About me
Business Intelligence developer
Worked for All-Lines for 5 years
Spoken at Pittsburgh SQL User Group and various SQL Saturdays
Help lead the 
Pittsburgh Power BI User Group
Pluralsight Author
Went from SQL newb to SQL pro
undefined
What is the goal of this talk?
Focus on core concepts
Building the basic mental model
What is difficult to understand?
undefined
DAX
 is NOT X
DAX is not Excel
DAX is not SQL
DAX is not MDX
DAX is painful if you don’t get this
The concepts are harder than the syntax
undefined
Learning Curve (The Problem With DAX)
Difficulty
Functionality
Evaluation contexts
Scalar
functions
Filtering
Calculated columns
Measures
Nesting evaluation contexts
Time Intelligence
undefined
This Course’s Focus
Difficulty
Functionality
Evaluation contexts
Filtering
Calculated columns
Measures
Time Intelligence
Nesting evaluation contexts
Scalar
functions
undefined
Overview
Calculated
 columns vs measures
Columnar storage
Aggregations
Filtering
Filter contexts
Iterators
undefined
There are two types of business logic
1.
Calculated columns
2.
Measures
undefined
Calculated
 columns
Represents a single value per row
Computed
 at time of creation/refresh
Results are materialized and stored with the table
Attached to a specific table
Normally can only see the row they exist in
Relates to the idea of row context
More on this later
Can be used in filters or values/results areas
undefined
Example Calculated
 Column
GrossMargin
 = Sales[Price] – Sales[Cost]
undefined
Measures
Represents
 a single value per data model
Computed at run
 time
Results are dynamic, based on filters
This is called the filter context
Not attached to any table
Sees all the data at once
undefined
Example Measure
TotalQuantity
 := 
SUM(Sales[Quantity])
undefined
Implicit measures
If you
 use a calculated column as a
value/result it creates an implicit measure
undefined
DAX is good at two things
1.
Aggregations
2.
Filtering
undefined
What is an aggregation?
Aggregation is combining a group of values into one value
Examples
SUM
AVERAGE
MIN
MAX
DISTINCTCOUNT
undefined
undefined
DAX
 stores data as columns
Super
 efficient for simple aggregations
Many aggregate functions take columns as parameters
DAX is optimized for single-column operations
Columns are the fundamental
 unit of measure
undefined
undefined
undefined
Basic units of measure
1.
Column
2.
Table
A set of columns with the same length and sort order
3.
Scalar Value
4.
Row
A table filtered down to a single row
Also called a row context
undefined
Compression and encoding
Value Encoding
Dictionary Encoding
Run-length encoding
Sorting
undefined
undefined
DAX is good at two things
1.
Aggregations
2.
Filtering
undefined
Two types of Filtering
Implicit filtering
Explicit filtering
undefined
Implicit
 Filtering
Slicers
Cell Location
undefined
Explicit
 Filtering
GreenQuantity := CALCULATE(SUM(Sales[Quantity]), 
   
 
                                             Sales[Color]="Green")
Explicit filtering supersedes implicit filtering
undefined
Filtering Context
A
 combination of all the user filters
Basic filters are associated with specific, individual columns
CALCULATE allows you to overwrite the filter context
undefined
Removing Filtering
The ALL() function removes
 filters
Can be used on a whole table, or specific columns
AllColors:=CALCULATE(SUM(Sales[Quantity]), ALL(Sales[Color]))
undefined
Applying complex filtering
This causes an error:
CALCULATE(SUM(Sales[Quantity]), Sales[Price] - Sales[Cost] <= 1)
Need to use something called an “iterator”
FILTER() takes in a table and an expression
Returns a filtered table
undefined
LowMargin:=CALCULATE(SUM(Sales[Quantity]),FILTER(Sales,
Sales[Price] - Sales[Cost] <= 1))
Example FILTER()
undefined
Iterators
Process tables row
 by agonizing row
Expensive to process
Rows are filtered one at a time.
Called
 a row context
Often a filter context AND a row context is applied
Necessary to refer to multiple fields in the same row
SUM(Sales[Price] - Sales[Cost]) raises an error
undefined
Example Iterator
AverageGrossMargin1:=AVERAGE(Sales[GrossMargin])
AverageGrossMargin2:=AVERAGEX(Sales, Sales[Price] - Sales[Cost])
Requires a table parameter to “iterate” through
Iterators are expensive
undefined
Key concepts
1.
Calculated columns are materialized values in a table
2.
Measures look at all the data plus the filter context
3.
Explicit filtering overrides implicit filtering
4.
DAX is optimized for single-column operations
5.
Rows don’t exist, but row contexts do
6.
Multi-column operations require iterators, which are expensive
7.
CALCULATE() + FILTER() / ALL() can be used to apply advanced
filtering
undefined
Book Recommendations
undefined
Questions?
Contact Info
@sqlgene
www.sqlgene.com/powerbi/
emeidinger@all-lines-tech.com
undefined
Slide Note
Embed
Share

In this insightful session, Eugene Meidinger delves into the fundamental concepts of DAX (Data Analysis Expressions) while outlining the complexities that make it distinct from Excel or SQL. Explore the learning curve, types of business logic, and practical examples to enhance your understanding of DAX. Discover the nuances of calculated columns, measures, filtering, and more, as Eugene guides you through building a solid mental model of DAX.

  • DAX
  • Data Analysis Expressions
  • Business Intelligence
  • Eugene Meidinger
  • Power BI

Uploaded on Feb 18, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. An (Advanced) Introduction to DAX Eugene Meidinger @sqlgene www.sqlgene.com/powerbi/ emeidinger@all-lines-tech.com

  2. About me Business Intelligence developer Worked for All-Lines for 5 years Spoken at Pittsburgh SQL User Group and various SQL Saturdays Help lead the Pittsburgh Power BI User Group Pluralsight Author Went from SQL newb to SQL pro

  3. What is the goal of this talk? Focus on core concepts Building the basic mental model What is difficult to understand?

  4. DAX is NOT X DAX is not Excel DAX is not SQL DAX is not MDX DAX is painful if you don t get this The concepts are harder than the syntax

  5. Learning Curve (The Problem With DAX) Evaluation contexts Nesting evaluation contexts Time Intelligence Difficulty Filtering Scalar functions Measures Calculated columns Functionality

  6. This Courses Focus Evaluation contexts Nesting evaluation contexts Time Intelligence Difficulty Filtering Scalar functions Measures Calculated columns Functionality

  7. Overview Calculated columns vs measures Columnar storage Aggregations Filtering Filter contexts Iterators

  8. There are two types of business logic 1. Calculated columns 2. Measures

  9. Calculated columns Represents a single value per row Computed at time of creation/refresh Results are materialized and stored with the table Attached to a specific table Normally can only see the row they exist in Relates to the idea of row context More on this later Can be used in filters or values/results areas

  10. Example Calculated Column GrossMargin = Sales[Price] Sales[Cost]

  11. Measures Represents a single value per data model Computed at run time Results are dynamic, based on filters This is called the filter context Not attached to any table Sees all the data at once

  12. Example Measure TotalQuantity := SUM(Sales[Quantity])

  13. Implicit measures If you use a calculated column as a value/result it creates an implicit measure

  14. DAX is good at two things 1.Aggregations 2.Filtering

  15. What is an aggregation? Aggregation is combining a group of values into one value Examples SUM AVERAGE MIN MAX DISTINCTCOUNT

  16. Quantity 4 1 3 2 7 5 1 SUM(Quantity) 23

  17. DAX stores data as columns Super efficient for simple aggregations Many aggregate functions take columns as parameters DAX is optimized for single-column operations Columns are the fundamental unit of measure

  18. State PA OH WV PA OH WV OH Color Blue Green Green Red Red Red Red Quantity 1 5 2 3 5 6 2

  19. Color Blue Green Green Red Red Red Red Quantity 4 1 3 2 7 5 1 State PA OH WV PA OH WV OH

  20. Basic units of measure 1. Column 2. Table A set of columns with the same length and sort order 3. Scalar Value 4. Row A table filtered down to a single row Also called a row context

  21. Compression and encoding Value Encoding Dictionary Encoding Run-length encoding Sorting

  22. Color Blue Green Green Red Red Red Red Color Blue,1 Green,2 Red, 4

  23. DAX is good at two things 1.Aggregations 2.Filtering

  24. Two types of Filtering Implicit filtering Explicit filtering

  25. Implicit Filtering Slicers Cell Location

  26. Explicit Filtering GreenQuantity := CALCULATE(SUM(Sales[Quantity]), Sales[Color]="Green") Explicit filtering supersedes implicit filtering

  27. Filtering Context A combination of all the user filters Basic filters are associated with specific, individual columns CALCULATE allows you to overwrite the filter context

  28. Removing Filtering The ALL() function removes filters Can be used on a whole table, or specific columns AllColors:=CALCULATE(SUM(Sales[Quantity]), ALL(Sales[Color]))

  29. Applying complex filtering This causes an error: CALCULATE(SUM(Sales[Quantity]), Sales[Price] - Sales[Cost] <= 1) Need to use something called an iterator FILTER() takes in a table and an expression Returns a filtered table

  30. Example FILTER() LowMargin:=CALCULATE(SUM(Sales[Quantity]),FILTER(Sales, Sales[Price] - Sales[Cost] <= 1))

  31. Iterators Process tables row by agonizing row Expensive to process Rows are filtered one at a time. Called a row context Often a filter context AND a row context is applied Necessary to refer to multiple fields in the same row SUM(Sales[Price] - Sales[Cost]) raises an error

  32. Example Iterator AverageGrossMargin1:=AVERAGE(Sales[GrossMargin]) AverageGrossMargin2:=AVERAGEX(Sales, Sales[Price] - Sales[Cost]) Requires a table parameter to iterate through Iterators are expensive

  33. Key concepts 1. Calculated columns are materialized values in a table 2. Measures look at all the data plus the filter context 3. Explicit filtering overrides implicit filtering 4. DAX is optimized for single-column operations 5. Rows don t exist, but row contexts do 6. Multi-column operations require iterators, which are expensive 7. CALCULATE() + FILTER() / ALL() can be used to apply advanced filtering

  34. Book Recommendations

  35. Questions? Contact Info @sqlgene www.sqlgene.com/powerbi/ emeidinger@all-lines-tech.com

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#