a question folding?” “Does your question fold?”… Possibly somebody requested you these questions, however you have been like: “Question…Whaaaat?!”
Or, perhaps you’ve heard about question folding in Energy BI, however didn’t know the best way to make the most of it in real-life eventualities.
If you happen to acknowledged your self in (at the least) one of many two conditions specified above, then please proceed studying this text.
Fantastic, you’re 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 can put the Question folding characteristic within the correct context.
Knowledge Shaping
I suppose you all know concerning the ebook written by Thomas Extra, known as “Utopia”.
In that story, all the pieces is ideal and everyone seems to be happy. In a really perfect world, let’s name it “Knowledge Utopia”, we’ve got clear, high-quality information that simply flies into our experiences “as-is”, while not having to carry out any sort of face-lifting or transformations alongside the best way. Sadly, “Knowledge Utopia” can exist solely in books — the truth is crueler — as we’ve got to take care of quite a few challenges whereas nurturing our information.
That being stated, one of many key ideas that we’ve got to soak up is Knowledge Shaping. Knowledge shaping is the method you must carry out when you get conversant in your information, and turn out to be conscious of attainable pitfalls inside the information you’re planning to make use of in your enterprise intelligence answer.
I’ve deliberately used the time period “Enterprise Intelligence” as an alternative of “Energy BI”, as this can be a common idea that ought to be used outdoors of Energy BI options too.
In easy phrases, information shaping is the method of knowledge consolidation, BEFORE it turns into a part of your information mannequin. The important thing factor to remember is the phrase: BEFORE! So, one would carry out information shaping earlier than the info goes into the report itself. Knowledge shaping may be executed at totally different locations, and, relying on the place you apply information shaping methods, at totally different closing dates through the information preparation course of.
WHERE must you carry out information shaping?
Supply Database — That is the obvious alternative and generally probably the most fascinating state of affairs. It’s based mostly on conventional information warehousing rules of Extracting-Remodeling-Loading (ETL) information. On this state of affairs, you outline what information you wish to extract (not all information from the database is required, and it’s normally not a good suggestion to import all the info). Then, you resolve in case your information must be remodeled alongside the best way, to fit your reporting wants higher — for instance, do you wish to carry out foreign money conversion, or do it is advisable 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 certainly one of these three names is appropriate? Sure, all of them are appropriate — however when you import the info into your information mannequin like this, you’re going to get incorrect outcomes — as every New York, NYC, and New York Metropolis shall be handled as a separate entity. This, and plenty of extra potential caveats, have to be solved through the Knowledge Shaping section, and that’s why it’s vital to spend a while massaging your information.
Energy Question
If you happen to don’t carry out information transformations on the supply aspect, 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 information. In response to Microsoft’s official documentation, you possibly can apply greater than 300 totally different transformations!
The important thing benefit of Energy Question is that you may carry out complicated information transformations with little or no coding expertise! Moreover, all steps you’ve utilized through the information transformation course of are being saved, so each time you refresh your dataset, these steps shall be robotically utilized to form your information and put together it for consumption by way of experiences.
Beneath the hood of Energy Question is a Mashup engine, that allows your information shaping to run easily. Energy Question makes use of a really highly effective M language for information manipulation. And, now you’re in all probability asking yourselves, what does all this story about information 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 information sources, resembling relational databases, but in addition non-relational information sources, for instance, OData, AD, or Alternate, the Mashup engine is ready to “translate” M language to a language that the underlying information supply will “perceive” — generally, it’s SQL.

By pushing complicated calculations and transformations on to a supply, Energy Question leverages the capabilities of the sturdy relational database engines, which are constructed to deal with massive volumes of knowledge in probably the most environment friendly means.
That capability 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 info supply aspect, 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, resembling SQL Server, Oracle, or MySQL. Nonetheless, it`s not simply that SQL databases make the most of the question folding idea. Basically, any information supply that helps some sort of querying language can probably make the most of question folding. These different information sources are OData, SSAS, SharePoint lists, Alternate, and Entra ID.
However, while you use information sources resembling Excel recordsdata, BLOB storage recordsdata, flat recordsdata, and so forth. in your Energy BI datasets, the question can’t fold.
Knowledge Transformations that help Question folding
Nonetheless, with regards to information sources that help question folding normally, it’s vital to understand that not all transformations may be folded and pushed to a knowledge 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 delicate variations within the Energy Question transformations may be decisive within the last final result, and whether or not your question will fold or not. I’ll present you just a few of these delicate variations within the following sections.
Typically talking, the next transformations, when utilized in Energy Question, may 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 may be translated to JOIN in SQL. After I stated, merging of foldable queries — meaning it can work if you’re becoming a member of two SQL server tables, but it surely won’t work if you’re making an attempt to hitch 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 capabilities which have equivalents in SQL language, for instance, mathematical capabilities, or textual content manipulation capabilities
- Pivot and Unpivot transformations
However, some transformations that can stop the question from folding are:
- Merging queries based mostly on totally different sources, as defined beforehand
- Appending (union-ing) queries based mostly on totally different sources — related logic as within the earlier case
- Including customized columns with complicated logic or utilizing some M capabilities that don’t have a counterpart in SQL
- Including index columns
- Altering a column information kind. This one is a typical “it relies upon” case. I’ll present you quickly what it will depend on, however simply understand that altering a column information kind may be each a foldable and a non-foldable transformation
Now, let’s study why you will need to obtain this habits — or, perhaps it’s higher to say, why must you care if the question folds or not?
Why must you care about Question folding?
While you’re utilizing Import mode in Energy BI, the info 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’re concentrating on the SQL database straight, all of your transformations MUST fold — or your answer won’t work.
Lastly, question folding can be of key significance for Incremental refresh — it’s so vital that Energy BI will warn you as soon as it determines that question folding can’t be achieved. It won’t break your incremental refresh “per-se”, however with out question folding in place, an incremental refresh wouldn’t serve its important objective — to cut back the quantity of knowledge that must be refreshed in your information mannequin — as with out question folding, Mashup engine must retrieve all information from the supply after which apply subsequent steps to filter the info.
With all these in thoughts, you must have a tendency to realize question folding each time attainable.
Sluggish report — don’t blame Question folding!
One vital disclaimer right here, and this is among the key takeaways from this sequence of weblog posts: in case your report is sluggish, or your visuals want a variety of time to render, or your information mannequin dimension is massive, question folding has nothing to do with it!
Provided that your information refresh or incremental refresh is sluggish and inefficient, you must examine your Energy Question steps in additional depth.
All or nothing?
A couple of extra issues to remember relating to 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. Nonetheless, as soon as the question folding is damaged, it will probably’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 probably’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 won’t fold. Maintain that in thoughts, and attempt to push all non-foldable steps down the pipeline as a lot as attainable.
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 delicate tips 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 to be like.
If it’s greyed out, this step in all probability doesn’t fold. However, if you’ll be able to click on on this feature, that implies that your question will fold. I suppose you’re perhaps confused with the phrase: PROBABLY!

However, that’s the right 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, although, in actuality, folding truly happens.
As an alternative, while you wish to make sure in case your question folds or not, you need to use the Question Diagnostics characteristic inside Energy Question Editor, or SQL Server Profiler, like a very good previous and dependable strategy to verify the queries despatched to a database by the Energy BI engine.
Moreover, there’s a cool characteristic 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 characteristic is out there solely in Energy Question On-line at this second, so let’s hope that the Energy BI workforce will implement it within the Desktop model quickly.

The satan is within the particulars…
Fantastic…You’ve in all probability heard concerning 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 information transformation course of.
Let’s begin with one of the vital curious circumstances in Energy Question editor…
Satan #1 — Merge Be part of
This one could be very fascinating, 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 few of the columns from my truth desk, and hold solely the CustomerKey column, as this can be a international key to a DimCustomer desk, and the Gross sales Quantity column. I’ll be a part of the DimCustomer desk as it’s, with none extra 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 a part of (left, outer, or internal).

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

If I am 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 can we clear up this? Let’s simply select a clean question and write our M code by hand to realize precisely the identical end result.

The important thing factor is that we’ll use an analogous, however nonetheless totally different M operate: Desk.Be part of.

All operate arguments are precisely the identical as beforehand, and let’s now verify the end result.
You keep in mind as soon as I informed you that when the View Native Question is greyed out, your question in all probability doesn’t fold, but it surely’s not 100% appropriate. And, this can be a good instance. If you happen to 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 have been tricked — this step certainly folded! As you possibly can see within the illustration above, we’ve got 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 a part of kind, which we have been in a position to clear up by tweaking the robotically generated M code. And, the opposite one was the inaccurate habits 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 — tips, lies & final efficiency check
I assume you at the moment are conversant in the idea of question folding in Energy BI, and particularly with its significance for information refresh and incremental refresh processes. We’ve additionally began to scratch some fascinating behaviors of Energy Question transformations, and on this last a part of the article, I’ll present you just a few extra fascinating findings.
Lastly, we’ll wrap it up with the final word efficiency check — I’ll present you the precise numbers behind two an identical queries — one folds, and the opposite doesn’t!
Altering Knowledge varieties
One of the vital frequent transformations in Energy Question is altering information kind. It’s a widely known greatest apply to make use of correct information varieties in your information mannequin — for instance, when you don’t want hours, minutes, and seconds stage of granularity in your experiences, try to be higher off eliminating them and altering the info kind of that column from Date/Time to Date solely.
Nonetheless, the highway to hell is paved with good intentions:)…So, let me present you one delicate distinction that may trigger your question to turn out to be rattling sluggish, although you’ve caught with the advice to make use of a correct information kind!

As you possibly can spot within the illustration above, my OrderDate column is of Date/Time information kind. And, I wish to swap it to Date solely. There are (at the least) two attainable choices to do that — the primary one is to right-click on the column, increase the drop-down for the Change Kind choice (like I did within the illustration), and choose Date kind (slightly below the Date/Time):

A couple of vital 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() operate
- The M system utilized to this transformation step is: Desk.TransformColumnTypes()
Let’s now study the opposite choice to alter information kind of our column:

Slightly below our earlier Change Kind 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 related, does it? However, let’s stroll by way of all of the issues that occurred now:
- As an alternative of the Modified Kind step, we now have a step known as Extracted Date
- The column itself seems to be 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 system utilized is: Desk.TransformColumns()
So, one single totally different phrase within the M system (Desk.TransformColumnTypes vs Desk.TransformColumns) affected our question so onerous that it couldn’t be translated to SQL!
Takeover from this story: watch out, and be careful while you’re selecting choices for altering information varieties!
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 understand 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’s an ABS operate in T-SQL:

Nonetheless, if I right-click on this step, I’ll see that the View Native Question choice is greyed out, so I’d 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 have 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 may be folded (like on this instance, once we knew that SQL has an ABS operate to help our transformation), double-check what actually occurs beneath the hood!
The last word efficiency check
Okay, if I didn’t handle to persuade you thus far, why you must attempt to realize question folding, let me now pull my final ace up my sleeve!
I wish to present you the distinction in information refresh efficiency between the queries that return precisely the identical outcomes — certainly one of them folds, and the opposite doesn’t!
Check #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 reveal the magnitude of significance of the question folding idea.
Within the first instance, I’ve utilized 9 totally 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, after all, you might write rather more optimum SQL code — nevertheless, understand that with auto-generated scripts by the Mashup engine, you aren’t getting the most optimum SQL — you’re simply getting appropriate SQL!
I’ll hit Shut & Apply and activate my stopwatch to measure how a lot time my information refresh lasts.

This question took 32 seconds to load 2.8 million data in my Energy BI report. Knowledge was loaded in batches of 100.000–150.000 data, which is an effective indicator that the question folding is in place.
Check #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 kind to Date), utilizing the transformation for which I do know that’s not foldable:

Fact 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 won’t 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 instances extra than in our earlier case when the question folded. This time, batches of loaded information have been between 10.000 and 20.000 data.
However, what’s much more regarding — you possibly can see that the entire variety of data loaded was nearly 11 million!!! As an alternative of two.8 million within the earlier instance! Why is it taking place? Effectively, within the earlier sections, I defined that when the Mashup engine can’t translate M language to SQL, it wants to tug ALL the info (from the second when the question folding was damaged), and THEN apply transformations on the entire chunk of imported information!
The ultimate result’s precisely the identical — we’ve got 2.830.017 data in our Energy BI report — however, with question folding in place, all crucial transformations have been carried out on the SQL database aspect, and the Mashup engine obtained an already ready information 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 information quantity! Merely think about the magnitude of implications on a bigger dataset, with a number of tables in it.
Conclusion
Effectively, we lined rather a lot on this article. We realized concerning the information shaping idea, we launched Energy Question fundamentals, and we additionally realized 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 tips on the best way to obtain question folding in some frequent use circumstances.
In the long run, please bear in mind that the question folding is a piece in progress, and people from the Energy BI workforce are continuously bettering this characteristic. So, it will probably occur that a few of the points with question folding I’ve proven you listed below are resolved within the meantime. Subsequently, remember to keep updated with the newest enhancements.
Thanks for studying!