0:00 Removing Hard Coding in Power Query
3:15 Dynamic Data Combination and Column Handling
6:57 Preventing Query Failures with Null Handling
8:01 Robust Filtering with Case-Insensitive Logic
9:48 Advancing to Advanced Power Query Techniques
This guide explains how to eliminate hard-coding in Power Query to build robust, \"unbreakable\" data pipelines. It covers techniques for handling dynamic column expansion, managing missing fields during type conversion, and overcoming case sensitivity to ensure queries remain functional even when source data changes.
Your Power Query breaks because there is too much hard coding inside of that.
In this video,
I'm going to teach you that how do you remove the hard coding inside of Power
Query so that your queries become unbreakable.
You're going to like this a lot. Let's start.
You know what's the hardest thing about learning Power BI? It is translation.
The ability to translate the DAX logic or the M logic or
data modeling into your own scenario,
your own industry with your own data is really hard.
And if you learn that,
that is when you have truly learned how to use Power BI into your own
business setting.
And that is the reason why we are launching Power BI case studies.
These are case studies in HR, in logistics, in finance,
in supply chain, every possible industry that you can imagine.
We're going to build a case study right from scratch,
understand the moving parts of that industry, how the data works,
how the business logic works,
and then how do you transform that into a working solution that you can
implement into your own business setting.
Hard coding most likely happens when you're trying to combine the data from
multiple tables and you're trying to expand those tables.
I've got your case here. Take a look at my screen.
So in this folder right here in the data folder,
I have three Excel files and these three Excel files are connected inside of
Power Query. These are not Excel files, but CSE files,
but nonetheless I have connected them inside of my Power Query and I can take a
look at the data.
Now the objective is to be able to combine the data from all the three files.
Let's just take a look at what is the roadblock that we hit.
So first off,
I'm just going to go over to the extension right here and I'll say that I only
want to take a look at the CSV files. That's part one.
Then the next thing that I'm going to do is I'm not going to blatantly combine
the button right here using the combine button,
because that is going to create a lot of helper queries,
which I don't really want at the moment.
I am just going to transform these CSV files into readable
tables and then expand those tables. To be able to create a new step,
I'll click on the FX button right here and now I'm going to start to write some
piece of code. So I'm going to say that, Hey, you know what?
I am trying to transform one column of a table.
So table dot transform columns. I'll start the bracket.
As the first part of the formula, it asks me,
what table are you trying to work with?
So I'm trying to work with this entire table and which is also there in the
previous step. Then it says, which column do you want to transform?
So if I just go back to the previous step,
the column that I'd like to transform is the content column.
There are binaries.
That means that there are CSVs in that column and I'd like to transform that
into a readable table. How do I say all of that?
I'm going to come back to the step.
I'm going to say that I would like to transform the content column.
So content is the name of the column.
Then I just want to pick up every single value of the column.
So therefore each, and I'll just say, Hey,
why don't you just convert that into a CSV dot document that
enables us to read the contents of the CSV.
And if I close the bracket and press enter, you can see that we now have a table.
This is awesome. You'll see that the headers are not promoted.
So I'm just going to promote the headers as well.
So I'll just wrap this around in the table dot promote headers function,
start the bracket and close the bracket all the way towards in the end.
And then press enter. We are good to go. If you now take a look at the table,
the tables are promoted with the headers. All seems to be good at this stage.
If I happen to click on the expand button right here,
you're going to see that all of these names of the columns,
which are inside of the tables are going to get hard coded.
If I click right here,
the names are hard coded and I don't really want that hard coding.
How do I solve for that?
The next thing that I'm going to do is I'm going to somehow capture all of these
names of the columns inside of a list,
and then feed that rather than actually moving that in a hard coded way.
So I'm going to go back and I'm going to see that I have three tables.
I'd like to combine the data of three tables. If the column names are the same,
then I can only pick up any particular table.
But if the column names are likely to be different,
then I can just combine the data so I can make another step in between.
So click and insert a step in between, call this as column names.
And what I'm going to do is I'm going to combine the data of all of these three
tables. So I'll just say, Hey,
all of these three tables are there in the content column and I'd like to combine
them. So table.combine, not only would I like to combine the tables,
but I'd like to just not combine the tables and the data.
I just want to extract the columns off of it.
So the formula for that is table.column names, start the bracket,
close the bracket, press enter.
And what you get is the columns of all the three tables.
Now this would have been different in case you had more number of columns in
any one of the table, but right now they aren't. So now this particular step,
column names is the step which contains dynamic column names,
and then you can feed that into your query.
So I can just come here and I can just maybe remove all of this part and I can
just say, Hey, this is going to be column names.
It is one more thing that we need to change in this query is that the expansion
is happening not on the column names step, not on this step,
but technically this step, which is where we converted the CSVs into a readable
table. So I will just reference that step once again,
which is nothing but custom one.
That's pretty much it and the table gets combined.
All the rest of the columns are there and this is pretty neat.
Another thing that happens quite often is that maybe you don't want to
carry the side columns along when you're expanding the tables.
Let me help you understand at this stage where we had all the CSVs converted
into a table. This is a table. This is a table. This is a table.
Maybe my need is perhaps to only get the data of
these three tables.
And I've got nothing to do with the rest of the columns because right now,
if you take a look at the work that we have done in order not to hard code our
columns, you can see that alongside the columns of the three tables that were
nested, we also have a couple of other columns right here, name,
extension, date, access, date, modified, and all of all of that.
In case you don't really want to have any of that,
you just want to have the data of the three tables.
Then we can play a trick and this entire thing becomes even more simpler.
So I'm going to come to the last step.
I'm going to click on the new step right here and reference it to the custom one
step, which is where we had the three nested tables.
Assume for one instance that we just want to combine the data off of these three
tables. What do I do?
I am going to write the square bracket and get the content column,
which contains the three tables. And as soon as I do that,
you're going to see that the entire column converts into a list.
So you can see that this is now into a list.
And now I can just wrap this around in the table dot combined function.
And if I start the bracket, you can see that it is asking you, Hey,
give me multiple tables that you would like to combine,
but I'll only accept those tables.
Should you give me in the form of a list and that is nothing but a list with
three tables, the order matches, the data type matches,
and this is going to work feed what power query eats.
That's the motto of my book on power query. In case you haven't checked it out,
do check it out. You're going to love it. Nonetheless,
I'm going to close the bracket in the end and press enter and all the tables get
combined and there are no extra columns in there. And this is all good.
The next most common thing that breaks the queries is the change type step.
And if you happen to remove any column off of that,
and the column is not found, this is going to break the query. Take a look.
So I've got all of these columns and sure enough,
I would like to apply the data type.
And in order to apply the data types to all the columns at once,
I can click on any particular column, press control A,
go over to the transform tab, and I can use this detect data type button.
Click right here.
And the data type is automatically detected for every single column.
Should you want to change anything? You can change it, but for now, be good.
Now let's just say that I've got the change type step right here.
And obviously inside of that,
you can see that every single column name that was supposed to be applied,
a date or a text or whatever that is, the name of the column is hard coded.
Obviously now that is fine because that might be the last step of the query,
but in anything changes in the query, this is going to break the step. All right.
So I'm going to go over to the previous step and for no good reason,
I will delete the amount column, press delete. Yes.
I would like to insert a step in between a removed column step is inserted.
And if I go over to the next step, the step is broken and the query tanks.
What do we do? Now there are two ways in which you can handle this.
The first method is that you can go ahead and just ignore the data type setting.
Now this particular feature was earlier available in power query in power BI,
but now it is also available in Excel power query.
So if you take a look at the table dot transform column types step,
you have the table,
you have the type declarations that is like to every single column.
Then in the end, if you put in a comma,
you're going to see that you have a culture in here.
You can declare a record and you can say that, Hey,
in case you have a missing column, ignore that. How do you write all of that?
I'm going to initiate a square bracket and inside of the square bracket,
I will start writing missing field.
M is uppercase and F is uppercase missing field or one word.
And then I'll say equals to, and then I'll write the enumeration,
which is the formula that I will use missing field dot ignore.
So right here, missing field dot ignore. That's it. Commit on this,
press enter and the query starts to work.
Now you might ask me a question that, Hey,
the query actually broke for a good reason because you know,
the column was missing and the query broke.
And now I can know that something was bad in the query and I can go fix it.
Well,
what you can do if you do not really want your queries to be broken,
to be able to fix them is rather than actually using the ignore keyword,
you can go ahead and use the use null keywords.
I believe I can get the IntelliSense use null keyword.
It is going to pop the column,
but all the values of the column are going to be null.
And obviously you will understand that something has gone wrong.
Now this particular keyword missing field dot ignore use nulls is quite helpful
and it is just not available at the change type step,
but also at a couple of other steps. I've done a detailed video on that.
Should you want to take a look, please go check out that video as well.
One more place where the query breaks is case sensitivity in Power Query.
So in case you filter for any particular value and the case doesn't match,
it is going to break.
I'm going to show you a nice technique to ignore the case sensitivity in case
you'd like to make your queries a little more robust.
See this product column and maybe I'd like to apply a filter to keep the
products which are starting with the word MO.
So like a monitor or a mouse,
all of those need to be kept and the rest can be removed.
Now how would you go about that?
You are going to click on that particular column,
hit the filter text filters and we have something called as begins with,
and I'm going to go ahead and feed that value MO right here.
But intentionally this time I'm feeding the lowercase M and O,
which obviously is not going to work with the mouse or the monitor,
which is uppercase. Click on okay. And I get nothing.
Is my filtering criteria correct? Yes, but the case is not matching.
Now should I change that into uppercase M and then if I now commit enter,
it is going to work, but that's not what I want.
I want the query to be robust even if I write a lowercase or the uppercase.
How can you do that?
So I'm going to invert it back to a lowercase and in the text dot starts with
formula. The first part is where are you searching?
Second part is what are you searching? And the third part,
if I just maybe move in a comma in here,
you can see that we have a comparer function and I can just say ignore the case
sensitivity. So if you just write ignore comparer, ordinal, ignore case,
this is going to start ignoring the case sensitivity on this particular thing.
It is available in most of the text functions which are performing searches.
So in case you are even searching on a list or a slightly more complicated
object, this should work in case you're working with any text based functions.
That's it. Press enter and it still works.
MO still works and you have uppercase monitors and lowercase monitors.
All good.
Next I'd like to talk about some very interesting Power Query tricks that are
going to take your level from a basic user to more advanced user.
But for that you'll have to come along with me to a new video. Let's go.