Column Name Expression Source Table New Column GROUP BY.

34

Transcript of Column Name Expression Source Table New Column GROUP BY.

Querying with DAXDBI-B412

Alberto Ferrari – [email protected]

Who’s Speaking?BI Expert and Consultant

Founder of www.sqlbi.comProblem SolvingComplex Project AssistanceDataWarehouse Assesments and DevelopmentCourses, Trainings and Workshops

Book Writer

Microsoft Business Intelligence Partner

SSAS Maestros – MVP – MCP

AgendaIntroduction to DAX as a Query LanguageEVALUATE ExamplesDAX functions useful in queries

Optimizing DAX queriesStorage Engine and Formula EngineUsing the profiler to understand execution timeExamples of query optimizationGeneral hints and tips

Querying with DAX

EVALUATE example

EVALUATE 'Sales Territory'

ORDER BY 'Sales Territory'[Sales Territory Group], 'Sales Territory'[Sales Territory Country], 'Sales Territory'[Sales Territory Region]START AT

"North America", "United States"

Using FILTER

EVALUATE

FILTER ( FILTER ( 'Internet Sales', [Ship Date] > [Due Date] ), 'Internet Sales'[Unit Price] > 2 )

Using CALCULATETABLE

EVALUATE

CALCULATETABLE ( 'Sales Territory', [Sales Territory Country] = "United States" )

ORDER BY 'Sales Territory'[Sales Territory Country]

Using ADDCOLUMNS

EVALUATE

ADDCOLUMNS ( 'Internet Sales', "Margin", [Sales Amount] - [Total Product Cost])

Column Name

Expression

Using SUMMARIZE

EVALUATE

SUMMARIZE ( 'Internet Sales', 'Internet Sales'[Order Date], "Sales Amount", SUM ('Internet Sales'[Sales Amount])

)

Source Table

New Column

GROUP BY

Using SUMMARIZE with ROLLUP

EVALUATE

SUMMARIZE ( 'Internet Sales', ROLLUP ( 'Date'[Calendar Year] ), "Sales Amount", SUM ( 'Internet Sales'[Sales Amount] ) )

Compute sub-totals at the Year

level

Using CROSSJOIN

EVALUATE

ADDCOLUMNS ( CROSSJOIN ( DISTINCT ( 'Product'[Color] ), DISTINCT ( 'Product'[Size] ) ), "Products", COUNTROWS ( RELATEDTABLE ( Product ) )

)

Using GENERATEEVALUATE

GENERATE ( VALUES ( 'Product Category'[Product Category Name] ), CALCULATETABLE ( VALUES ('Product SubCategory'[Product SubCategory Name] ) ))

Top 3 products per categoryGENERATE ( VALUES ( 'Product Category'[Product Category Name] ), FILTER ( CALCULATETABLE ( ADDCOLUMNS ( TOPN ( 3, VALUES ( 'Product'[Product Name] ), [Internet Total Sales] ), "Sales", [Internet Total Sales] ) ), [Sales] > 0 ))

Optimizing DAX

Optimizing DAXAnalyze query plansUsing the profilerAnalysis of execution time and, if needed, query plans

Understand the engine behaviorStorage Engine and Formula EngineUsing the cacheBe creative: DAX likes original ideas

SQL Server Profiler

Using the profiler to look at the execution time

Elements of a DAX query profileVertiPaq SE QuerySimple pseudo-SQL queries executed by the VertiPaq engineRetrieve datasets from the storageAt least two events for each query (internal rewrite)

DAX Query PlanComplex operatorsWorks on datasets generated by the VertiPaq SE queriesNo fancy graphics, only a plain string, hard to read

Formula Engine and Storage EngineFormula Engine (DAX query plan)Evaluates complex expressionsJoins together temporary tablesFilters data at the high levelSingle-threaded per query

Storage Engine (VertiPaq queries)Queries the xVelocity engine (VertiPaq)Performs natural joins using relationshipsCalculates very simple mathMulti-threaded

Formula Engine

Formula Engine Storage Engine

Storage Engine is your best friend

Events in Progress

Looking at different versions of a query with the profiler

Events in Progress: final results

Optimizing DAX leads to great resultsIt requires a lot of timeUseful to stop when it is “good enough”

Extreme optimizations need to work on the whole model

VersionFormula Engine

Execution Time Notes

Naïve 22,078 22,175 First query you will write

Column-wise 906 925 Query written by a seasoned DAX coder

DatesBetween

156 167 Original idea (from Chris Webb) you will find in on a blog post

Generate 78 80 Required 4/5 days of hard optimization

DAX caches VertiPaq SE queries only. FE is not cached

CallBackDataIdComplex conditions in VertiPaqCan be resolved by CallBackDataIdStorage Engine calls back Formula EngineOne instance of FE for each SE threadGood parallelismStill not cached, anyway

Better than pure Formula EngineYet, it is often better to use SE onlyBeware, “often” does not mean “always”

Be Creative

Different databases require different kind of optimization

Optimizing DAX queriesSpot expensive operationsUsing the profiler, search for long-running eventsIf needed, spend time understanding the complete flowThe query plan is hard to readFor complex queries use the logical plan

Use the VertiPaq EngineTry to push most of the calculations to the Storage EngineScales better on multi-core computersTakes advantage of the cache system

Break the rules!Very often, creative solutions lead to great resultsDAX is young, imagination and expertize is needed

DBI-H318 Developing a Power Pivot Data Model in Excel 2013

Related content

DBI-B319 Improving Power Pivot Data Models for Microsoft Power BI

Find Me Later At Microsoft booth

27 Hands on Labs + 8 Instructor Led Labs in Hall 7

DBI Track resources

Free SQL Server 2014 Technical Overview e-book

microsoft.com/sqlserver and Amazon Kindle StoreFree online training at Microsoft Virtual Academy

microsoftvirtualacademy.com Try new Azure data services previews!Azure Machine Learning, DocumentDB, and Stream Analytics

Resources

Learning

Microsoft Certification & Training Resources

www.microsoft.com/learning

Developer Network

http://developer.microsoft.com

TechNet

Resources for IT Professionals

http://microsoft.com/technet

Sessions on Demand

http://channel9.msdn.com/Events/TechEd

Please Complete An Evaluation FormYour input is important!TechEd Schedule Builder CommNet station or PC

TechEd Mobile appPhone or Tablet

QR code

Evaluate this session

© 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.