Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL SERVER Becoming a More Effective DAX Developer with DAX Studio Notes f ...

$
0
0
[Note from Pinal]: This is a new episode of Notes from the Field series. I personally have no problem accepting that I do not know many topics. One of the such topics is DAX Studio. I always wanted to learn more and I finally got the opportunity to learn more.
SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

In this episode of the Notes from the Field series I asked SQLExpert Bill Anton a very crucial question How to become a more effective DAX developer with DAX Studio? Billwas very kind to answer the questions and provides plenty of information about how a novice developer canwork with DAX Studio.

If you’ve been doing any sort of development involving DAX, whether it’s with Power BI, Power Pivot, or Analysis Services Tabular, and you’re NOT using DAX Studio, then you’re doing it wrong!

But don’t worry. In this post, I’m going to walk you through this incredible tool and show you how it can improve your development experience and enable you to write better, more efficient DAX.

Disclaimer: DAX Studio is an incredible (and completely FREE) tool that significantly improves the developer experience when writing and performance tuning DAX code. This is not some blatant marketing tactic. Nor have I been asked to write this blog about it. I’m just a typical SSAS developer who has found this tool very helpful in my client engagements, so much so in fact, that I feel the need to tell others about it.

The Basics: Writing DAX

DAX is a functional programming language and whether you’re writing a query or a calculation, the amount of function nesting can get intimidating. Take the snippet below which calculates the total sales amount for the prior year:

Internet Total Sales PY := CALCULATE ( SUMX ( 'Internet Sales', 'Internet Sales'[Order Quantity] * 'Internet Sales'[Unit Price] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )

You’re probably thinking to yourself, “that doesn’t look so bad. What is this guy whining about?”

To show you what I mean, here’s the same DAX measure as originally written in SQL Server Data Tools (SSDT) which is the default tool for developing Analysis Services Tabular models.


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

Hold on, let me get you a magnifying glass…


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

Is that better? Not really?

Exactly! So imagine having to write the following DAX code in that silly little formula bar…

Open Orders := SUMX ( FILTER ( GENERATE ( SUMMARIZE ( 'Internet Sales', 'Internet Sales'[Order Date], 'Internet Sales'[Ship Date], "Rows", COUNTROWS ( 'Internet Sales' ) ), DATESBETWEEN ( 'Date'[Date], 'Internet Sales'[Order Date], 'Internet Sales'[Ship Date] - 1 ) ), 'Date'[Date] = EARLIER ( 'Date'[Date] ) ), [Rows] )

Right? I don’t want to do it either. But I have…


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

…and I see others in the field having to do it too.

Where’s the font colors indicating reserved words, functions, etc? And don’t even get me started on how annoying it can be when you’ve just spent 5-10-15+ minutes writing a new complex DAX calculation only to be greeted with an error message indicating you accidentally missed a closing parenthesis or closing column bracket. Hopefully you’re prepared to start over from scratch because after closing the error message you’re going to be left with a blank cell.


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

This type of scenario has quickly led to many developers writing DAX in a general text editor (e.g. Notepad++) before copying it over to the DAX formula bar in SSDT. And while that saves you from losing the 5-10-15+ minutes of rewriting the DAX code, you’re giving up intellisense, which, in a complicated model, is very VERY helpful.

Note : to be fair, most of my complaints are directed at the SSAS Tabular developer interface (i.e. SSDT for SQL Server 2014 and 2012). Power BI (and SSDT 2015 for SSAS 2016) is much better in these regards. For example, Power BI (and SSDT 2015 for SSAS 2016) includes syntax highlighting (i.e. color coding function/reserved words).

After spending a few weeks/months in the pits working with the SSDT built in DAX editor in SSDT, I eventually found my way over to using DAX studio which includes syntax highlighting, intellisense, formatting, and a ton of other helpful features!

So, for example, even if I write my code out in a single line like a robot…


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

…I can hit this handy little button…


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

…and get some super readable code…


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

The features you see above, again, super basic features that should be included in any development environment (syntax highlighting, automatic formatting, intellisense, etc) are enough of a reason for me to use DAX Studio for over 95% of the DAX code I write. And I haven’t even shown you the good stuff!

Advanced: Optimizing DAX

As mentioned in the previous section, DAX is a functional language and you, as a developer, have to be very explicit in telling the engine what you want it to do. This is quite different from SQL, a declarative language, where we “declare” to the optimizer what we want the output to be and it is up to the optimizer to determine the most efficient way to get there.

What this means is that the same SQL query can be written a number of different ways and still result in the same (or very similar) query execution plan… whereas the same DAX query or calculation can be written a number of different ways and performance can differ by orders of magnitude. Bottom line: as DAX developers you spend a good bit of time writing the same DAX calculation or query several different ways in order to achieve optimal performance. And DAX Studio includes several key features that help with this task.

Clear Cache

One of the initial and most basic optimization checks when writing DAX (or MDX for that matter) is measuring cold-cache performance… how fast does the query or calculation perform when it has to get all of the data from raw storage.

First of all, there’s no way to test query or calculation performance from within SSDT (the tool used develop tabular models)… which means you’re going to have to build the model SSDT and test calculation and/or query performance using another tool like SSMS (which, btw, doesn’t even have a DAX editor!). So in order to test cold-cache query performance, you’ll need two windows: one for the query and the other for the clear cache command. Did I mention already that SSMS doesn’t have a DAX editor?

With DAX Studio, there’s a built in button, right on the main ribbon tab, that allows you to clear the cache of the tabular database you’re connected to:


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

…and an output tab which will tell you when the cache was cleared…


SQL SERVER   Becoming a More Effective DAX Developer with DAX Studio   Notes f ...

And if that wasn’t easy enough, you can even set the default “Run” option to clear the cache before running the query everytime!


Viewing all articles
Browse latest Browse all 3160

Latest Images

Trending Articles