Modeling scenarios for DAX Ana María Bisbé York Business Intelligence Consultant MS Partner on Power BI anabisbe@amby.net http://amby.net/ @ambynet Modeling scenarios for DAX
Thanks to all Sponsors
Say thanks to our volunteers They spend their FREE time to give you this event. Because they are crazy. Because they want YOU to learn from the BEST IN THE WORLD. João Sarmento Eduardo Piairo Diamantino Falcão
Agenda Intro Decisions to take: Evaluation context Implicit vs Explicit Measures Calculated columns vs measures Measures vs Variables Calculated columns vs Calculated Tables Calculated columns vs Measures vs Context transition Evaluation context Definition Test the quality of data Modify current filter context
DAX - Introduction DAX – Data Analysis Expressions Tabular models Power Pivot SQL Server Analysis Services Tabular Power BI Functional language Execution flows as function calls From inner to outer most Single line execution
Is it easy to write DAX expressions?
Demo !!!
Is it easy to write DAX expressions?
Decisions to take !!
Calculated column vs Measures Design of the model Calculated column, (preferably during the Query step) Headers of rows and/or columns of a table or matrix, discrete axis in charts or slicers Measures (with DAX) Examine the result as value, in the central panel of the pivot table Use this value as data source for the KPI Value depends on the user-defined context Affected by table filters, charts and slicers
Explicit measures vs Columns Implicit measures are created when a numeric column is selected. The default aggregate function is Sum Good practices: Create the measure It’s easier to read and to maintain the code From them on, we can always select the Total Quantity measure. We do not need the table name to refer to the measure, the measures belong to the model. Explicit measures are wrapped in a CALCULATE, although we don’t write it CALCULATE creates the transition from the row context into filter context. This avoids the use of navigation functions, which once again improves reading and maintenance. When exporting the model to Excel, the measures are available for the ”pivot table values”, numeric columns, in the other hand, are not.
Calculated columns vs measures Two types of calculations in DAX Calculated columns It is evaluated row by row It consumes more resources, calculates for every row of the column Immediate calculation and storage in column of the tabular model Useful for filters and segmentation. Best Practices: Creating calculated columns in a query. Do not create calculated columns for numeric values Measures It is evaluated by using on a table/chart It consumes less resources, only calculated for active elements. Dynamic calculation, on the fly Written always in DAX, same syntax, different context
Calculated columns vs measures Calculations from columns Calculations from measures
Measures vs Variables Variables More readable code Less evaluation for each function No additional measure is created
Model types Unique table Snowflake Star Excel classic, before Excel 2013 Snowflake Normalized There are links between the search tables or directly to the facts tables. Star Denormalized model All search tables (dimensions) point directly to the facts table
Relationships between tables and DAX Combined filters from different tables All tables are pointing to Facts Tables
Relationships between tables and DAX Crossfilter direction Single direction in the model Need to filter in both directions Both directions in the calculation or in the model?
Relationships between tables and DAX Crossed filter direction: bidirectional The filter from Products propagates to Sales(1:M) The filter propagates from Sales to Customers(M:1) Caution – could be very dangerous
Relationships between tables and DAX Crossfilter direction Single direction in the model Both directions in the calculation !! DAX to the rescue !!!
Calculated columns vs Calculated tables Table with a single column Calculated column
Calculated columns vs Calculated tables Tables created with columns, single DAX expression
Evaluation context
DAX Contexts Different results of a measure, depending on context The result depends on: The code The filter context in which is evaluated
Testing context with FILTER Given a context Create a temporary table to test the value Calculate the aggregated in a measurement
Testing context with FILTER Given a context Calculate the aggregated of a measure, using directly the filter expression
Modifying context with DAX Replace the existing FILTER context With functions CALCULATE or CALCULATETABLE Ignore existing context With function ALL
Modifying context with DAX Replace or Ignore existing context Only when it corresponds Analysis done by category Nothing to replace or ignore ignorar
Modifying context with DAX Contexts and comparisons Compare in a single category Exclude total value
Modifying context with DAX – April 2017 Quick measures – (preview versión)
Modifying context with DAX – Abril 2017 Surprise! Power BI Desktop writes DAX Quick measures
Resources Books Tools
DAX - Recap It is easy to start writing DAX expressions for tabular models Very powerful, and delicate at the same time Measures and CALCULATE are our best allies #HappyDAXing
Questions? Ana María Bisbé York anabisbe@amby.net http://amby.net/ @ambynet Thanks!!! #sqlsatPorto #sqlsat685