How to Keep Manual Changes with Power Query... It's Easy When You Know How!

Versatility
93 followers
0:00 Introduction to Self-Referencing Queries in Power Query
1:53 Setting Up the Initial Query and Preventing Data Overwrite
3:46 Adding Manual Notes Without Losing Data During Refresh
5:33 Merging New Transactions with Existing Data Using ID Matching
8:35 Finalizing the Self-Referencing Query and Testing Data Integrity
This video demonstrates how to create a self-referencing query in Power Query to prevent the overwriting of manual notes and changes. By linking a query to its own output table and merging it with new data, users can maintain a persistent record of manual edits while still appending new transactions from a source.
Whenever we refresh Power Query, it gets the data from the source, it transforms it,
and it replaces our existing data in the workbook. But what if our existing data
had manual information or manual notes that we wanted to maintain? Well the good news is
that we can create a self-referencing query so that our previous data remains as it was
and any new transactions are added into the query. And that's what we're going to do
in this video. So if you're ready let's get started. Here is our example file and if you
want to work along with this video you can follow the links in the video description box below to
get hold of this file. We currently have just a single table which is called new transactions.
It has columns for id, item, size, and value. And below that we have some additional values
that we will use as part of our example. Initially we want to get our new transactions
into Power Query and create a table. Now I should mention that in this example we are using a table
but it doesn't have to be. It could be any source, an Excel workbook, a CSV file, a SharePoint list,
any of those will work. But we're using a table because it makes it easier to demonstrate the
techniques on this video. To get this table into Power Query I'm simply going to select a cell in
the table and go to data and from table slash range. That will now load that table as a query.
Let's rename this query from new transactions to just transactions. We're now ready to load this
back into Excel. We'll go to close and load, close and load to, and we want to load this as a table
on the existing worksheet in cell B5. I'll then click OK. There is now a relationship between
these two tables. Now unfortunately because of the default settings my column widths have
changed. So let's change the setting so that doesn't happen. I'll click inside the table,
go to properties, and we have adjust column width. I'll uncheck that and click OK. I'll now
reapply our column widths and that means that each time we now refresh our query,
go to data and refresh all, and that doesn't change the column widths of that query. There's
just a small tip for you. Currently our new transactions feed into our transactions query.
So if we were to add a new row and click refresh all, that would then pull through. But any changes
we make, such as changing this value to 2000, when I click refresh all, that is then overwritten
because the original value didn't change. So how can we make it so the Power Query
gets the values from the original table and doesn't overwrite them? Well currently our table,
I'll go to table design, is called transactions. This table is called new transactions. So what
we want is to repoint the output of our query so that it references transactions. Let's get
into our transactions query. I'll go to the source step and let's change that to be transactions.
We'll then close and load and now our query table is referencing into itself. So if I change the
value to 200, when we refresh, you can see that doesn't change our value. But unfortunately,
this also doesn't get any new values. If I add them in and then go to data, refresh all,
it doesn't add those new values because there's no relationship between new transactions and
transactions. There are two things that we want to do with our example. Firstly, we want to create
the scenario where we can add any new transactions but it doesn't replace any of the manual changes
we made to the existing transactions. And secondly, we want to add a column so that we can
add manual information into our query. And when we refresh, it doesn't lose any of that manual
information. So let's go make a start and let's look at point two of adding a new column for
manual information. Now to do that, I could add a new column to our query table called notes.
When we do that, we've got a manual column. But when we feed this table into Power Query,
it will take notes as being in our table. It will then query itself, bring that back,
which means that when we click refresh all, it will add a notes to column. This notes to column
is the manual column. We don't need this column at all. So let's remove that from our output.
Let's also delete this column. Now when we click refresh, it doesn't add that manual column.
This column is no longer a manual column. It's part of our data set. So that means that if we
add notes, let's say this was out of stock, now available in small. When we click refresh all,
it doesn't lose our notes. We also maintain those. So now let's go look at how we can add
the additional transactions without replacing the previous changes we made. To do that,
we're going to select on our new transactions table again, and we're going to go from table
slash range. We're going to keep this as new transactions. We're also going to take our
transactions query, and we're going to duplicate that. And let's call this existing transactions.
So our goal is to take our existing transactions, find out what the new transactions are,
and then stack those together. So we have our existing and then our new. That means that in
our new transactions, we're going to go to the home ribbon and click merge queries. We want to
merge that with our existing transactions. And we're going to select ID in both of those queries.
Now it's important for this technique to work that we do have a unique ID for each row. If you
don't have a unique ID, you might be able to combine columns together to create a unique ID,
but it's essential that there is a unique ID because we use that to identify which are the
existing and new transactions. I just want to interrupt things here to tell you about our Excel
Academy. In there, we have the courses, tools, and masterclasses you need to truly master Excel
and save huge amounts of time. But we also know that's not enough. What happens if you have
questions? What happens if you get stuck? Well, in that Academy, we have an active community and
live Q&A calls. So that means anytime you don't know what to do, anytime you have questions,
you can ask them and myself and other members of the community will get you back on track. So to
find out more about the Excel Academy, just head on over to exceloffthegrid.com. With our two ID
columns selected, we'll come down to join kind and we're going to use left anti. These are the
rows that only exist in our first table. I'll select that and click okay, which means we now
only get the two new rows. It's added an additional column called existing transactions.
We don't need that, so I will select that and press delete. That means we now have the two
tables we need. Our existing transactions and our new transactions. Let's come back to our
transactions query and rather than our source, which was referencing our transactions table,
we're going to change this so that it equals our existing transactions. I'll then use an ampersand,
which will then stack those two tables together and we want the new transactions. When we commit
that, we now get both of those tables stacked together. Let's close and load this back to Excel.
We're to close and load, close and load two. And in here, we can close and load as a connection
only. I'll select that, click okay and that's it. We're now done. We have a self-referencing query
that adds new transactions. If I add three new rows and click refresh all, you can see we get
those at the end of our query. If I make a change to an existing row and click refresh all, it
doesn't change that previous transaction. Now what happens if we lose transactions in our original
table? I'm going to cut those, paste those elsewhere and then I'll add my new transactions.
So that means we only have transaction 15 and 16. If you think about it, all of these are our
existing transactions. We have our new transactions. So Power Query is simply going to stack these two
tables together. When I click refresh all, it doesn't overwrite or remove any previous values
but it has added the new values. So that means that if we take one of our rows and let's delete
that row, that when we click refresh all, that row won't be reinstated because it's not in our
new transactions and it's not in our existing list of transactions. And that is how we can create
a self-referencing query that adds any new transactions but doesn't overwrite any of our
manual changes. If you like this video then don't forget to subscribe and get notifications
so you don't miss any of our future videos. Thanks for watching. I'll catch you next time.
