Variable column formula

greyangel23

New Member
Joined
Apr 23, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have to cells labeled Month1 and month 2. Month 1 and month 2 are columns in my power query. What I need to do is create a custom column that subtracts month 1 from month 2, however I do not know how to make the column names variable in M code. Below is what I want to happen but obviously this isn't working.
= Table.AddColumn(#"Changed Type", "Custom", each [Month2]-[Month1])
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Use the menus. That’s what they’re for. Go to Add/Add column/custom column. Write
[Month2]-[Month1]
In the dialogue box
 
Upvote 0
Does not work because Month2 and Month1 are variable names. Example I have a table that is called Month2 which equals August and a table called Month1 which equals July. I can't use July and August because this custom column can change every month depending on what the user types into these tables
 
Upvote 0
You said Month 1 and Month 2 are columns. My solution works in that instance. How about you post some realistic sample data and illustrate what you are trying to do with it and then someone can no doubt help you.
 
Upvote 0
First, rename your variable column names by referring to their position - to be able to 'consistently' name them;

Power Query:
= Table.RenameColumns(#"Changed Type",{{Table.ColumnNames(#"Changed Type"){0}, "[Month 2]"}, {Table.ColumnNames(#"Changed Type"){1}, "[Month 1]"}})

Replace 0 and 1 with where your Month columns appear in your query (with your first column being 0, the second 1, and so on).

After than, you can do your [Month 2] - [Month 1] step, as they'll then always be called that.
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top