watching Jeffrey Wang as a stay stream visitor with Reid Havens, and one of many dozen fantastic issues that Jeffrey shared with the viewers was the checklist of optimizations that the DAX engine performs when creating an optimum question plan for our measures.
And, the one which caught my consideration was concerning the so-called “Sparse measures”:
To make it easy, when you outline the measure, Components Engine in VertiPaq will add an implicit NonEmpty filter to the question, which ought to allow the optimizer to keep away from full cross-join of dimension tables and scan solely these rows the place data for the mixture of your dimension attributes actually exist. For people coming from the MDX world, the NonEmpty operate might look acquainted, however let’s see the way it works in DAX.
The factor that almost all resonated with me was when Jeffrey suggested towards changing BLANKs with zeroes (or no matter express values) in Energy BI calculations. I’ve already written how one can deal with BLANKs and change them with zeroes, however on this article, I wish to give attention to the potential efficiency implications of this resolution.
Setting the stage
Earlier than we begin, one necessary disclaimer: the advice to not change BLANK with 0 is simply that — a suggestion. If the enterprise request is to show 0 as a substitute of BLANK, it doesn’t essentially imply that you must refuse to do it. In most situations, you’ll most likely not even discover a efficiency lower, however it’s going to depend upon a number of various factors…
Let’s begin by writing our easy DAX measure:
Gross sales Amt 364 Merchandise =
CALCULATE (
[Sales Amt],
FILTER ( ALL ( 'Product'[ProductKey] ), 'Product'[ProductKey] = 364 )
)
Utilizing this measure, I wish to calculate the full gross sales quantity for the product with ProductKey = 364. And, if I put the worth of this measure within the Card visible, and activate Efficiency Analyzer to test the occasions for dealing with this question, I get the next outcomes:
DAX question took solely 11ms to execute, and as soon as I switched to DAX Studio, the xmSQL generated by the Components Engine was fairly easy:
And, if I check out the Question plan (bodily), I can see that the Storage Engine discovered just one present mixture of values to return our information:
Including extra elements…
Nevertheless, let’s say that the enterprise request is to investigate information for Product Key 364 on a day by day stage. Let’s go and add dates to our report:
This was once more very quick! I’ll now test the metrics inside the DAX Studio:
This time, the question was expanded to incorporate a Dates desk, which affected the work Storage Engine wanted to do, as as a substitute of discovering only one row, this time, the quantity is totally different:
After all, you’ll not discover any distinction in efficiency between these two situations, because the distinction is only some milliseconds.
However that is only the start; we’re simply warming up our DAX engine. In each of those instances, as you might even see, we see solely “stuffed” values — that mixture of rows the place each of our necessities are glad — product secret is 364 and solely these dates the place we had gross sales for this product — for those who look completely within the illustration above, dates should not contiguous and a few are lacking, comparable to January twelfth, January 14th to January twenty first and so forth.
It’s because Components Engine was good sufficient to eradicate the dates the place product 364 had no gross sales utilizing the NonEmpty filter, and that’s why the variety of data is 58: now we have 58 distinct dates the place gross sales of product 364 weren’t clean:
Now, let’s say that enterprise customers additionally wish to see these dates in-between, the place product 364 hadn’t made any gross sales. So, the concept is to show 0$ quantity for all these dates. As already described within the earlier article, there are a number of other ways to exchange the BLANKs with zeroes, and I’ll use the COALESCE
()Â operate:
Gross sales Amt 364 Merchandise with 0 = COALESCE([Sales Amt 364 Products],0)
Mainly, the COALESCE
operate will test all of the arguments supplied (in my case, there is just one argument) and change the primary BLANK worth with the worth you specified. Merely mentioned, it’s going to test if the worth of the Gross sales Amt 364 Merchandise is BLANK. If not, it’s going to show the calculated worth; in any other case, it’s going to change BLANK with 0.
Wait, what?! Why am I seeing all of the merchandise, after I filtered every thing out, besides product 364? Not to mention that, my desk now took greater than 2 seconds to render! Let’s test what occurred within the background.
As a substitute of producing one single question, now now we have 3 of them. The primary one is precisely the identical as within the earlier case (58 rows). Nevertheless, the remaining queries goal the Product and Dates tables, pulling all of the rows from each tables (The product desk incorporates 2517 rows, whereas the Dates desk has 1826). Not simply that, check out the question plan:
4.6 million data?! Why on Earth does it occur?! Let me do the mathematics for you: 2.517 * 1.826 = 4.596.042…So, right here we had a full cross-join between Product and Dates tables, forcing each single tuple (mixture of date-product) to be checked! That occurred as a result of we pressured the engine to return 0 for each single tuple that may in any other case return clean (and consequentially be excluded from scanning)!
It is a simplistic overview of what occurred:
Imagine it or not, there may be a chic answer to point out clean values out-of-the-box (however, not with 0 as a substitute of BLANK). You possibly can simply merely click on on the Date area and select to Present gadgets with no information:
This may show the clean cells too, however with out performing a full cross-join between the Product and Dates tables:
We are able to now see all of the cells (even blanks) and this question took half the time of the earlier one! Let’s test the question plan generated by the Components Engine:
Not all situations are catastrophic!
Reality to be mentioned, we may’ve rewritten our measure to exclude some undesirable data, however it could nonetheless not be an optimum method for the engine to eradicate empty data.
Moreover, there are specific situations wherein changing BLANKs with zero won’t trigger a major efficiency lower.
Let’s look at the next state of affairs: we’re displaying information concerning the whole gross sales quantity for each single model. And I’ll add my gross sales quantity measure for product 364:
As you may anticipate, that was fairly quick. However, what is going to occur after I add my measure that replaces BLANKs with 0, which brought on havoc within the earlier state of affairs:
Hm, seems to be like we didn’t should pay any penalty when it comes to efficiency. Let’s test the question plan for this DAX question:
Conclusion
As Jeffrey Wang prompt, you must keep away from changing blanks with zeroes (or with another express values), as it will considerably have an effect on the question optimizer’s capability to eradicate pointless information scanning. Nevertheless, if for any purpose it’s essential to substitute a clean with some significant worth, watch out when and learn how to do it.
As regular, it relies on many alternative points — for columns with low cardinality, or if you’re not displaying information from a number of totally different tables (like in our instance, once we wanted to mix information from Product and Dates tables), or visible sorts that don’t have to show numerous distinct values (i.e. card visible) — you may get away with out paying the efficiency value. However, for those who use tables/matrices/bar charts that present a whole lot of distinct values, make certain to test the metrics and question plans earlier than you deploy that report back to a manufacturing surroundings.
Thanks for studying!