a question folding?” “Does your question fold?”… Perhaps somebody requested you these questions, however you had been like: “Question…Whaaaat?!”
Or, possibly you’ve heard about question folding in Energy BI, however didn’t know how you can benefit from it in real-life eventualities.
In the event you acknowledged your self in (not less than) one of many two conditions specified above, then please proceed studying this text.
Superb, you might be curious to search out out what a Question folding is. However, first issues first…Earlier than you proceed, we have to set up some theoretical foundations, which is able to put the Question folding function within the correct context.
Knowledge Shaping
and why it is likely one of the key ideas within the knowledge preparation part. Now, I want to increase on that in a (possibly) uncommon means:
I suppose you all know in regards to the e book written by Thomas Extra, known as “Utopia”.
In that story, the whole lot is ideal and everyone seems to be glad. In a super world, let’s name it “Knowledge Utopia”, we’ve clear, high-quality knowledge that simply flies into our stories “as-is”, with no need to carry out any sort of face-lifting or transformations alongside the way in which. Sadly, “Knowledge Utopia” can exist solely in books — the fact is crueler — as we’ve to cope with quite a few challenges whereas nurturing our knowledge.
That being stated, one of many key ideas that we’ve to soak up is Knowledge Shaping. Knowledge shaping is the method it’s best to carry out when you get accustomed to your knowledge, and develop into conscious of doable pitfalls inside the knowledge you might be planning to make use of in your enterprise intelligence resolution.
I’ve deliberately used the time period “Enterprise Intelligence” as a substitute of “Energy BI”, as this can be a common idea that ought to be used outdoors of Energy BI options too.
In easy phrases, knowledge shaping is the method of information consolidation, BEFORE it turns into a part of your knowledge mannequin. The important thing factor to remember is the phrase: BEFORE! So, one would carry out knowledge shaping earlier than the information goes into the report itself. Knowledge shaping will be completed at completely different locations, and, relying on the place you apply knowledge shaping strategies, at completely different deadlines in the course of the knowledge preparation course of.
WHERE must you carry out knowledge shaping?
Supply Database — That is the obvious selection and usually probably the most fascinating state of affairs. It’s based mostly on conventional knowledge warehousing rules of Extracting-Remodeling-Loading (ETL) knowledge. On this state of affairs, you outline what knowledge you wish to extract (not all knowledge from the database is required, and it’s normally not a good suggestion to import all the information). Then, you resolve in case your knowledge must be remodeled alongside the way in which, to fit your reporting wants higher — for instance, do you wish to carry out forex conversion, or do it’s essential conform nation and metropolis names?
Do you acknowledge the town within the following picture?
Sure, it’s New York. Or, is it NYC? Or, is it New York Metropolis? Which considered one of these three names is right? Sure, all of them are right — however for those who import the information into your knowledge mannequin like this, you’ll get incorrect outcomes — as every New York, NYC, and New York Metropolis will likely be handled as a separate entity. This, and plenty of extra potential caveats, must be solved in the course of the Knowledge Shaping part, and that’s why it’s essential to spend a while massaging your knowledge.
Energy Question
In the event you don’t carry out knowledge transformations on the supply facet, the following station is Energy Question — it’s the built-in instrument inside Energy BI, that lets you carry out all types of transformations to your knowledge. In line with Microsoft’s official documentation, you possibly can apply greater than 300 completely different transformations!
The important thing benefit of Energy Question is that you may carry out complicated knowledge transformations with little or no coding abilities! Moreover, all steps you’ve utilized in the course of the knowledge transformation course of are being saved, so each time you refresh your dataset, these steps will likely be mechanically utilized to form your knowledge and put together it for consumption by way of stories.
Underneath the hood of Energy Question is a Mashup engine, that allows your knowledge shaping to run easily. Energy Question makes use of a really highly effective M language for knowledge manipulation. And, now you might be most likely asking yourselves, what does all this story about knowledge shaping, Energy Question, Mashup engine, M language, and so forth. should do with Question folding? I don’t blame you, it’s a good query, however we’ll come again quickly to reply it.
What’s a Question folding?
For some knowledge sources, comparable to relational databases, but additionally non-relational knowledge sources, for instance, OData, AD, or Alternate, the Mashup engine is ready to “translate” M language to a language that the underlying knowledge supply will “perceive” — usually, it’s SQL.

By pushing complicated calculations and transformations on to a supply, Energy Question leverages the capabilities of the strong relational database engines, which can be constructed to deal with massive volumes of information in probably the most environment friendly means.
That means of Energy Question’s Mashup engine to create a single SQL assertion combining all M statements behind your transformations is what we name Question folding.
Or, let`s make it easy: if the Mashup engine is ready to generate a single SQL question that’s going to be executed on the information supply facet, we are saying that the question folds.
Knowledge sources that help Question folding
As already talked about, the obvious beneficiary of question folding is relational database sources, comparable to SQL Server, Oracle, or MySQL. Nevertheless, it`s not simply that SQL databases benefit from the question folding idea. Basically, any knowledge supply that helps some sort of querying language can probably benefit from question folding. These different knowledge sources are OData, SSAS, SharePoint lists, Alternate, and Entra ID.
Alternatively, if you use knowledge sources comparable to Excel information, BLOB storage information, flat information, and so forth. in your Energy BI datasets, the question can’t fold.
Knowledge Transformations that help Question folding
Nevertheless, in the case of knowledge sources that help question folding usually, it’s essential to understand that not all transformations will be folded and pushed to an information supply. So, simply to be clear, the truth that a SQL database helps question folding doesn’t essentially imply that your question will fold! There are some Energy Question transformations that merely can`t be pushed to a SQL database engine.
Fairly often, some refined variations within the Energy Question transformations will be decisive within the remaining end result, and whether or not your question will fold or not. I’ll present you a number of of these refined variations within the following sections.
Usually talking, the next transformations, when utilized in Energy Question, will be “translated” to a single SQL assertion:
- Eradicating columns
- Renaming columns
- Filtering rows, with static values or Energy Question parameters, as they’re handled as WHERE clause predicates in SQL
- Grouping and summarizing, that are equal to SQL’s Group by clause
- Merging of foldable queries based mostly on the identical supply, as this operation will be translated to JOIN in SQL. After I stated, merging of foldable queries — which means it would work if you’re becoming a member of two SQL server tables, nevertheless it is not going to work if you’re attempting to affix a SQL desk and an Excel file
- Appending foldable queries based mostly on the identical supply — this transformation pertains to the UNION ALL operator in SQL
- Including customized columns with easy logic. What does easy logic imply? Utilizing M features which have equivalents in SQL language, for instance, mathematical features, or textual content manipulation features
- Pivot and Unpivot transformations
Alternatively, some transformations that may stop the question from folding are:
- Merging queries based mostly on completely different sources, as defined beforehand
- Appending (union-ing) queries based mostly on completely different sources — comparable logic as within the earlier case
- Including customized columns with complicated logic or utilizing some M features that don’t have a counterpart in SQL
- Including index columns
- Altering a column knowledge sort. This one is a typical “it relies upon” case. I’ll present you quickly what it is determined by, however simply understand that altering a column knowledge sort will be each a foldable and a non-foldable transformation
Now, let’s look at why you will need to obtain this conduct — or, possibly it’s higher to say, why must you care if the question folds or not?
Why must you care about Question folding?
If you’re utilizing Import mode in Energy BI, the information refresh course of will work extra effectively when the question folds, each by way of refresh pace and useful resource consumption.
In case you are working with DirectQuery or Twin storage mode, as you might be focusing on the SQL database immediately, all of your transformations MUST fold — or your resolution is not going to work.
Lastly, question folding can also be of key significance for Incremental refresh — it’s so essential that Energy BI will warn you as soon as it determines that question folding can’t be achieved. It is not going to break your incremental refresh “per-se”, however with out question folding in place, an incremental refresh wouldn’t serve its principal function — to scale back the quantity of information that must be refreshed in your knowledge mannequin — as with out question folding, Mashup engine must retrieve all knowledge from the supply after which apply subsequent steps to filter the information.
With all these in thoughts, it’s best to have a tendency to realize question folding every time doable.
Sluggish report — don’t blame Question folding!
One essential disclaimer right here, and this is likely one of the key takeaways from this sequence of weblog posts: in case your report is gradual, or your visuals want lots of time to render, or your knowledge mannequin measurement is massive, question folding has nothing to do with it!
Provided that your knowledge refresh or incremental refresh is gradual and inefficient, it’s best to examine your Energy Question steps in additional depth.
All or nothing?
Just a few extra issues to remember concerning question folding. It’s not an all-or-nothing course of. Meaning you probably have, let’s say, 10 transformation steps inside Energy Question, and your question folds till the sixth step, you’ll nonetheless get some profit from partial question folding. Nevertheless, as soon as the question folding is damaged, it will possibly’t be achieved anymore.

To simplify, you probably have 10 transformation steps, and your question folding is damaged within the fifth step, all earlier steps will fold, however as soon as the folding is damaged, it will possibly’t be achieved once more, even you probably have transformations that help question folding by default in steps 6 to 10 — like in our instance the place filtering ought to be a foldable step, these steps is not going to fold. Maintain that in thoughts, and attempt to push all non-foldable steps down the pipeline as a lot as doable.
How have you learnt if the question folds?
Okay, now we’re not rookies anymore. We all know what question folding is, why we must always attempt to realize it, and a few refined methods that may make an enormous distinction.
Now, it’s time to learn to verify if the precise question folds or not. The primary and most blatant means is to right-click on the step and verify what the View Native Question choice seems like.
If it’s greyed out, this step most likely doesn’t fold. Alternatively, if you’ll be able to click on on this feature, that implies that your question will fold. I suppose you might be possibly confused with the phrase: PROBABLY!

However, that’s the correct phrase, as you possibly can’t be 100% positive that if the View Native Question choice is disabled, your question doesn’t fold. I’ll present you later how this feature can trick us into pondering that the question folding was damaged, though, in actuality, folding really happens.
As a substitute, if you wish to make sure in case your question folds or not, you should use the Question Diagnostics function inside Energy Question Editor, or SQL Server Profiler, like a great outdated and dependable option to verify the queries despatched to a database by the Energy BI engine.
Moreover, there’s a cool function in Energy Question On-line, the place every step is marked with the icon that exhibits if that step folds, doesn’t fold, or is unknown. As I stated, this function is obtainable solely in Energy Question On-line at this second, so let’s hope that the Energy BI crew will implement it within the Desktop model quickly.

The satan is within the particulars…
Superb…You’ve most likely heard in regards to the saying that the satan is within the particulars. Now, it’s time to know how little nuances could make an enormous distinction in our knowledge transformation course of.
Let’s begin with one of the vital curious instances in Energy Question editor…
Satan #1 — Merge Be part of
This one could be very attention-grabbing, as you’ll hardly assume what is going on within the background. Let’s say that I wish to mix two of my queries into one. I’ll use the Journey Works pattern database, and I have to merge the FactInternet Gross sales and DimCustomer tables.
I’ll take away a number of the columns from my truth desk, and hold solely the CustomerKey column, as this can be a overseas key to a DimCustomer desk, and the Gross sales Quantity column. I’ll be part of the DimCustomer desk as it’s, with none further steps earlier than merging.

Merging tables is equal to JOIN operation in SQL. Basically, we select the column on which we wish to carry out MERGE operation, and the kind of be part of (left, outer, or interior).

The issue is that by default, if you’re merging two queries, Energy Question will generate a nested be part of assertion, which might’t be correctly translated in SQL.

If I’m going to the Instruments tab and click on on Diagnose Step, I can see that the Mashup engine fired two separate queries to my underlying SQL Server database — in different phrases, these two queries couldn’t be executed as a single SQL assertion, and that implies that question didn’t fold!

How will we remedy this? Let’s simply select a clean question and write our M code by hand to realize precisely the identical consequence.

The important thing factor is that we are going to use an identical, however nonetheless completely different M perform: Desk.Be part of.

All perform arguments are precisely the identical as beforehand, and let’s now verify the result.
You keep in mind as soon as I instructed you that when the View Native Question is greyed out, your question most likely doesn’t fold, nevertheless it’s not 100% right. And, this can be a good instance. In the event you check out View Native Question, it nonetheless exhibits that our question doesn’t fold…

…however let’s go to Diagnostics and verify if that’s true.

Oh, boy, we had been tricked — this step certainly folded! As you possibly can see within the illustration above, we’ve a single SQL question generated and despatched to a SQL Server supply database to be executed.
So, we discovered two devils on this instance — the primary one was a be part of sort, which we had been in a position to remedy by tweaking the mechanically generated M code. And, the opposite one was the inaccurate conduct of the View Native Question choice. I’ll present you within the subsequent a part of the sequence yet one more instance when View Native Question lies.
Question folding in Energy BI — methods, lies & final efficiency take a look at
I assume you at the moment are accustomed to the idea of question folding in Energy BI, and particularly with its significance for knowledge refresh and incremental refresh processes. We’ve additionally began to scratch some attention-grabbing behaviors of Energy Question transformations, and on this remaining a part of the article, I’ll present you a number of extra attention-grabbing findings.
Lastly, we’ll wrap it up with the last word efficiency take a look at — I’ll present you the precise numbers behind two an identical queries — one folds, and the opposite doesn’t!
Altering Knowledge sorts
One of the vital frequent transformations in Energy Question is altering knowledge sort. It’s a well known greatest follow to make use of correct knowledge sorts in your knowledge mannequin — for instance, for those who don’t want hours, minutes, and seconds degree of granularity in your stories, try to be higher off eliminating them and altering the information sort of that column from Date/Time to Date solely.
Nevertheless, the highway to hell is paved with good intentions:)…So, let me present you one refined distinction that may trigger your question to develop into rattling gradual, though you’ve caught with the advice to make use of a correct knowledge sort!

As you possibly can spot within the illustration above, my OrderDate column is of Date/Time knowledge sort. And, I wish to swap it to Date solely. There are (not less than) two doable choices to do that — the primary one is to right-click on the column, increase the drop-down for the Change Sort choice (like I did within the illustration), and choose Date sort (just under the Date/Time):

Just a few essential issues occurred right here, so let me clarify every of these:
- Within the Utilized Steps pane, you possibly can discover that our transformation step had been recorded
- Within the column itself, you possibly can see that the time portion disappeared
- After I’ve opened the View Native Question dialog field, you possibly can see that the Mashup engine properly translated our transformation to a T-SQL CONVERT() perform
- The M method utilized to this transformation step is: Desk.TransformColumnTypes()
Let’s now look at the opposite choice to alter knowledge sort of our column:

Slightly below our earlier Change Sort choice, there’s a Remodel choice. When you increase the drop-down, you possibly can see the Date Solely transformation. Let’s click on on it and verify what occurs:

Seems fairly comparable, does it? However, let’s stroll by all of the issues that occurred now:
- As a substitute of the Modified Sort step, we now have a step known as Extracted Date
- The column itself seems precisely the identical as within the earlier instance — no time half in there
- Ooops, the question doesn’t fold anymore! As you possibly can see, the View Native Question choice is greyed out!
- This time, M method utilized is: Desk.TransformColumns()
So, one single completely different phrase within the M method (Desk.TransformColumnTypes vs Desk.TransformColumns) affected our question so arduous that it couldn’t be translated to SQL!
Takeover from this story: watch out, and be careful if you’re selecting choices for altering knowledge sorts!
Liar, Liar…
I’ve promised within the earlier a part of the article that I’ll present you yet one more instance when the View Native Question choice can idiot you into pondering that question folding was damaged, even when in actuality it’s not true…
Let’s say that we wish to hold solely the highest X rows from our desk. In my case, I wish to protect the highest 2000 rows from my truth desk:

As soon as I’ve utilized this step and checked the View Native Question, I can notice that my question folds, as my transformation was translated to a TOP clause in SQL:

Now, let’s say that I wish to apply Absolute worth transformation on my Gross sales Quantity column. Usually, this transformation simply folds, as there may be an ABS perform in T-SQL:

Nevertheless, if I right-click on this step, I’ll see that the View Native Question choice is greyed out, so I might assume that this step broke my question folding!

Let’s verify this in our Question Diagnostics instrument:

Oh, my God! This step folded certainly! So, we had been tricked by the View Native Question choice once more!
The important thing takeover right here is: everytime you’re assuming {that a} particular transformation step will be folded (like on this instance, once we knew that SQL has an ABS perform to help our transformation), double-check what actually occurs underneath the hood!
The last word efficiency take a look at
Okay, if I didn’t handle to persuade you to this point, why it’s best to attempt to realize question folding, let me now pull my final ace up my sleeve!
I wish to present you the distinction in knowledge refresh efficiency between the queries that return precisely the identical outcomes — considered one of them folds, and the opposite doesn’t!
Take a look at #1 Question folding ON
For this testing, I’ll use the FactOnlineSales desk from the Contoso pattern database. This desk has round 12.6 million rows, and it’s good to exhibit the magnitude of significance of the question folding idea.
Within the first instance, I’ve utilized 9 completely different transformation steps, and all of them are foldable, as you possibly can see within the following illustration:

Don’t take note of the SQL code that the Mashup engine generated: if you’re a SQL skilled, in fact, you could possibly write far more optimum SQL code — nonetheless, understand that with auto-generated scripts by the Mashup engine, you aren’t getting the most optimum SQL — you might be simply getting right SQL!
I’ll hit Shut & Apply and activate my stopwatch to measure how a lot time my knowledge refresh lasts.

This question took 32 seconds to load 2.8 million information in my Energy BI report. Knowledge was loaded in batches of 100.000–150.000 information, which is an effective indicator that the question folding is in place.
Take a look at #2 Question folding OFF
Now, I’ll return to Energy Question Editor, and deliberately break question folding on the third step (keep in mind the instance above with altering Date/Time sort to Date), utilizing the transformation for which I do know that isn’t foldable:

Reality to be stated, I’ll obtain a partial folding right here, as first two steps will fold, however all subsequent steps after the Extracted Date transformation is not going to fold!
Let’s activate the stopwatch once more and verify what occurs:

The very first thing to note: this question took 4 minutes and 41 seconds to load into our Energy BI report, which is roughly 10 occasions extra than in our earlier case when the question folded. This time, batches of loaded knowledge had been between 10.000 and 20.000 information.
However, what’s much more regarding — you possibly can see that the overall variety of information loaded was virtually 11 million!!! As a substitute of two.8 million within the earlier instance! Why is it occurring? Effectively, within the earlier sections, I defined that when the Mashup engine can’t translate M language to SQL, it wants to drag ALL the information (from the second when the question folding was damaged), and THEN apply transformations on the entire chunk of imported knowledge!
The ultimate result’s precisely the identical — we’ve 2.830.017 information in our Energy BI report — however, with question folding in place, all essential transformations had been carried out on the SQL database facet, and the Mashup engine bought an already ready knowledge set. Whereas within the second state of affairs, after we broke the question folding, the Mashup engine pulled the entire remaining rows (approx. 11 million), and solely after that was it in a position to apply different transformation steps.
And, this was only a fundamental instance, with one single desk, and never so huge by way of knowledge quantity! Merely think about the magnitude of implications on a bigger dataset, with a number of tables in it.
Conclusion
Effectively, we lined so much on this article. We discovered in regards to the knowledge shaping idea, we launched Energy Question fundamentals, and we additionally discovered what question folding is and why we must always do our greatest to realize it.
I’ve additionally shared with you some fundamental examples and neat methods on how you can obtain question folding in some frequent use instances.
In the long run, please bear in mind that the question folding is a piece in progress, and folk from the Energy BI crew are consistently enhancing this function. So, it will possibly occur that a number of the points with question folding I’ve proven you listed here are resolved within the meantime. Subsequently, be sure you keep updated with the newest enhancements.
Thanks for studying!