Power Query – Absolute and relative references

Power Query - Absolute and relative references - featured image

In Excel, we can easily create absolute and relative references.  A simple $ symbol here and there is all it takes.  But Power Query is less flexible, therefore getting values from other rows is not as simple.  So, in this post, we cover how to get Power Query absolute and relative references.

Download the example file

I recommend you download the example file for this post.  Then you’ll be able to work along with examples and see the solution in action, plus the file will be useful for future reference.

Download Icon
Download the file: 0014 Power Query – Absolute and relative references.xlsx

Absolute references

Look at the Absolute references tab in the example file.  In this worksheet, there are five scenarios to compare against a base.

Absolute row reference - Excel scenario

As shown in the screenshot above, the formula in cell C2 is:

=B2-$B$2

$B$2 is an absolute cell reference. When we copy the formula down, each cell consistently references cell B2.

Next, let’s see how we achieve a similar result in Power Query.  Having loaded the data, the Preview Window displays this:

Absolute references - loaded into Excel

Power Query has already made some transformations for us.  There is no need to change any of these.

First load applied steps

Right-click on the first row in the Profit column, select drill down from the menu.

Right-click Drill Down

Look at the formula bar (click View -> Formula Bar if it is not visible), it contains the syntax for referencing a specific row and column within Power Query.

Drill Down Formula Bar

The M code is:

=#"Changed Type"{0}[Profit]
  • #”Change Type” is the name of the previous transformation step
  • {0} references the first row of data.  In Power Query, counting always starts at zero.
  • [Profit] is the name of the column.

This gives us the syntax to get the absolute reference for any item in the Power Query window.  Delete this step; we no longer need it.

Now we will add a custom column to replicate Excel’s calculation (as shown above).  Click Add Column -> Custom Column.

Add column - Custom column

In the Custom Column dialog box, enter the following formula.

=[Profit] - #"Changed Type"{0}[Profit]

Enter a new column name, such as Vs Base (PQ), then click OK.

Custom Column dialog box

That’s it.  Look at the screenshot below.

Power Query - With absolute reference

We now have a formula that is always referencing a specific row of a specific column.  In the screenshot above, the Vs Base (PQ) is calculating the same value as we calculated in Excel in the Vs Base column.

100 Excel VBA Macros

Do you know the fastest way to learn foreign languages?  It is to read, write, speak, and think in that language as often as possible.  Apart from speaking, programming languages are no different.  The more you immerse yourself in that language, the faster you will pick it up.

100 Excel Macros Book

Therefore, what most people like you need is lots of examples that you can practice.  That is why the 100 Excel VBA Macros eBook exists.  It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers.  The book contains:

  • 100 example codes to practice reading and writing macros that will embed the language into your thinking.
  • An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
  • Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
  • Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.

Relative References

Look at the Relative references tab in the example file.  In this example, we have five years of profits and a base year.  The goal is to create a column showing the increase in Profit since the prior year.

The prior year is a relative reference, as the cell changes for each formula (i.e., in our example, the prior year is always one row above).

Relative Reference example in Excel

As shown in the screenshot above, the formula in cell C3 is:

=B3-B2

As the formula is copied down, the cells referenced in the formula change at a relative rate.  As a result, when copied down one cell B3 becomes C3, then when copied down again it becomes D3.

Now it’s Power Query’s turn to create a relative reference.  After loading the data into Power Query, the Preview Window looks like this:

Add an Index column by clicking Add column -> Index Column (drop-down) -> From 0

PQ Add Index Column

Click Add Column -> Custom Column to add a new column.

Add column - Custom column

In the dialog box, enter this formula.

=[Profit] - #"Added Index"{[Index]-1}[Profit]

The Index column is used to determine which row is used in the formula.

Give the column a name such as Incremental (PQ), then click OK.

Custom Column containing the added Index column

The Preview Window looks like this:

Relative Reference - results in an error

As we are referring to the row above, the first row can never have a value; as a result, Power Query displays an error.

Select the new Incremental (PQ) column, then from the ribbon click Transform -> Replace Values (drop-down) -> Replace Errors

Transform - Replace Errors

In the Replace Errors window, enter null in the Value box, then click OK.

Replace Values null

And we’re done.  It wasn’t too hard, after all.

Preview Window - Final Position

In the screenshot above, the Incremental (PQ) column calculated the same value as we had in Excel (which is the Incremental column).  Therefore, we have created a relative reference 🙂

Conclusion

Power Query is not as flexible as standard Excel.  This is a strength because it means the data structure is more robust.  But, to replicate some simple Excel formulas, we need to work a bit harder.  As we’ve seen, while absolute and relative references are not standard Power Query transformations, we can write a simple formula to achieve the same outcome.



Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Email Address * First Name *

By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.


Don’t forget:

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

Do you need help adapting this to your needs?

I’m guessing the examples in this post didn’t exactly meet your situation.  We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs.  By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.

But, if you’re still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic.  You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office.  It’s amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community.  Remember, the people on these forums are generally giving their time for free.  So take care to craft your question, make sure it’s clear and concise.  List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner.   They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Leave a Reply

Your email address will not be published. Required fields are marked *