Download presentation
Presentation is loading. Please wait.
Published byRalf Lindsey Modified over 8 years ago
1
Example Materials Power BI Desktop File (4/16): https://drive.google.com/file/d/0B4dDgyQg- 6WPbWY5UTRhaHYwdDA/view?usp=sharing
2
Intro to DAX Patterns
3
Eric Bragas – MCP, PSM I DesignMind - Business Intelligence Consultant
4
Agenda DAX Data Analysis Expressions Notation Functions Evaluation Contexts Measure Patterns Calculated Column Patterns
5
Data Analysis Expressions (DAX) What is DAX? DAX is a language that allows us to write dynamic expressions for relataional constructs, using familiar functions Powerful dynamic data analysis tool for relational data Expressions can traverse relationships! Available in PowerPivot, PowerBI, and SSAS Tabular Classic “Import Mode” What isn’t DAX? NOT a programming language
6
DAX Notation Table ‘Table’ Column ‘Table’[Column] Function fn ( ) Measure Measure Name := SUM ( ‘Table’[Column] ) Calculated Column Column Name = SUM ( ‘Table’[Column] )
7
Measures A measure is a formula/expression comprising functions applied to columns and tables Reusable aggregation evaluated differently, depending on how you use it Measures can be nested
8
Functions Logical IF( logical test,, ) SWITCH(,,, … ) – evaluates an expression against a list of values, and returns the result corresponding to the first matching value TRUE() – returns logical true Aggregate SUM( ) – adds all the numbers in a column DIVIDE(,, [, ] ) – basic division; optional value returned Statistical MAX( ) – returns the largest numeric value in a MIN( ) – returns the smallest value in a Text BLANK() – returns a blank Filter FILTER(, ) – returns a table representing a subset of another table or expression ALL( | ) – returns all the rows in a table, ignoring any filter context VALUES( ) – returns one column of the distinct values from the specified column or table CALCULATE(,,, … ) – evaluates an expression in a context that is modified by the specified filters
9
Functions (cont’d) Date and Time TODAY() – returns the current date NOW() – returns the current date and time in datetime format Time Intelligence* DATESBETWEEN(,, ) – returns a table of starting with the and continues until the. NEXTDAY( ) – returns a table that contains a column with the next dates following each of the passed FIRSTDATE( ) – returns the first date in the context of the specified column of dates LASTDATE( ) – returns the last date in the context of the specified column of dates SAMEPERIODLASTYEAR( ) – returns a table with a column of dates shifted one year back for each of the specified LASTNONBLANK(, ) – returns last value in the where the returns blank FIRSTNONBLANK(, ) – returns the first value in the where the returns blank *Requires Date Table
10
Evaluation Contexts Evaluation Contexts: Filter Context Four types of filter context: 1.Row Selection 2.Column Selection 3.Slicer Selection 4.Filter Selection Defines the subset of data a measure is calculated using aka “Which rows are selected based on which attribute values?” Applied before anything else Row Context All the columns in the Current Row “DAX is simple, it’s not easy, but it’s simple” - Alberto Ferrari
11
Basic Evaluation Context - Demo TotalSales = SUM ( Sales[SalesAmount] )
12
Anatomy of a Filter Context - PivotTable Rows Columns Filter Slicer
13
Anatomy of a Filter Context - Chart Column Filter Slicer
14
Measure Patterns Basic Cumulative YTD Year Over Year Semi-additive Disconnected Slicers
15
Basic Measures SUM( ‘Sales’[SalesAmount] ) AVERAGE( ‘Inventory’[InventoryOnHand] ) MIN( ‘Weather’[Temp] ) MAX ( ‘Weather’[Temp] ) Etc.
16
Basic Measure Demo – PowerPivot & PowerBI Asia Sales := CALCULATE ( [Total Sales], Geography[ContinentName] = "Asia" ) Asia Sales := CALCULATE ( [Total Sales], FILTER ( 'Geography', Geography[ContinentName] = "Asia" ) ) Boolean Table
17
Cumulative Total Measures Aggregates values of a column for the currently selected date and all previous dates within the specified range Can be used to derive balances from transactions eg. Inventory Stock Balances Cumulative Balances Does not require use of Time Intelligence functions
18
Cumulative Measure Demo - PowerBI Cumulative Energy Generated (Checked) = IF ( MIN ( 'Date'[Date] ) <= MAX ( ‘Output’[Date], ALL ( ‘Output’ ) ), CALCULATE ( SUM ( 'Output'[Energy Generated] ), FILTER ( ALL ( 'Date'[Date] ), 'Date'[Date] <= MAX ( 'Date'[Date] ) ) )
19
Year-to-Date Total TOTALYTD function applies the expression for all data from the start of the year to the currently selected date in the filter context Year To Date = TOTALYTD(, [, ] [, ] )
20
Year-to-Date Total Demo - PowerBI Total Energy Generated YTD = TOTALYTD ( SUM ( 'Output'[Energy Generated] ), 'Date'[Date] )
21
Year Over Year Use time intelligence to calculate an aggregate for the same period last year “Last Year” measure is used to compare to “Current Year” measure, and/or to derive a measure of the change year-over-year
22
Year-Over-Year Demo – PowerBI Total Energy Generated Last Year = CALCULATE ( [Total Energy Generated], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
23
Semi-Additive Measures Snapshot Fact Table with balance values, such as Inventory or Account Balances These scenarios disallow us from summing across time The solution is to sum across all attributes except for time by filtering for only a single point in time (eg. last date in the period) Several functions allow you to adjust filter context to a single point in time, within the original context period FIRSTDATE / LASTDATE FIRSTNONBLANK / LASTNONBLANK OPENING… / CLOSING…
24
Semi-Additive Measure Demo - PowerPivot Total On Hand Quantity LASTNONBLANK = CALCULATE ( SUM ( Inventory[OnHandQuantity] ), LASTNONBLANK ( 'Date'[Date], CALCULATE ( SUM ( Inventory[OnHandQuantity] ) ) ) )
25
Disconnected Slicers Allows you to use a slicer to modify measures Measure Switching Used to switch between a set of measure values in a container measure
26
Disconnected Slicers Demo - PowerBI Setup Steps: 1.Create/identify your target measures (eg. [Energy Exported] & [Energy Generated]) 2.Create disconnected table to use in slicer selection 3.Create background “value selection measure” using MAX() Hide this! 4.Create SWITCH measure to use in visualizations
27
Calculated Column Patterns Value Binning
28
Used to group similar values, or to bin values for analysis aka Histograms Can bin values based on equality, or inequality comparisons with the SWITCH() function Use Cases: Age groups Product Groups Any kind of frequency distributions
29
Value Binning Demo - PowerBI Inventory Age (bin) = SWITCH ( TRUE (), 'Inventory'[DaysInStock] < 20, "0-20", 'Inventory'[DaysInStock] < 50, "21-50", 'Inventory'[DaysInStock] < 80, "51-80", 'Inventory'[DaysInStock] < 100, "81-100", "101+" )
30
Summary DAX is dynamic because you can write measures that correctly evaluate under their current Evaluation Context Filter Context Row Context Functions are the building blocks of our measures and perform a myriad of tasks eg. altering Context, aggregating, logical operations, time intelligence, etc Time Intelligence functions require a Date table to operate Understanding Tabular Data Modeling will go a long way towards helping your understanding of DAX
31
Thanks!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.