Power Query to Dynamically Change Column Name

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
Table1 has the Fiscal Year:
Fiscal Year
2021

Would like Table2 to dynamically change the column name from "Total" to "Total 2021 Amount" (as shown below) based on the fiscal year input in Table 1.

StationTotal 2021 Amount
2$5
3$10

Can it be done through the interface selections?
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I don't think you can change it dynamically using the interface but if copy the first line from the following code to the beginning of your code you can refer to it later:
Power Query:
let
    FiscalYear = "Total " & Text.From(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]{0}[#"Fiscal Year"]) & " amount",
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Renamed Columns" = Table.RenameColumns(Source,{{"Total", FiscalYear}})
in
    #"Renamed Columns"
 
Upvote 0
Yes it is possible, if you don't mind updating the M code of one of the steps.

Query 1: drill down on the value and name the Query fiscalyear or so.

Query two: do the rename manually. Now in the fx-bar update the M-code to something like:
Power Query:
"Total " & fiscalyear & " amount"

Edit: @Misca 's solution is better, since it does not reference another query. Might be a bit more challenging for a beginner to insert a block of code before a query.
 
Last edited by a moderator:
Upvote 0
Yes it is possible, if you don't mind updating the M code of one of the steps.

Query 1: drill down on the value and name the Query fiscalyear or so.

Query two: do the rename manually. Now in the fx-bar update the M-code to something like:
Excel Formula:
[CODE=pq]"Total " & fiscalyear & " amount"
[/CODE]

Edit: @Misca 's solution is better, since it does not reference another query. Might be a bit more challenging for a beginner to insert a block of code before a query.
I got the following error message when I concatenated the query two expression:

We cannot apply operator & to types Text and Number.

Not sure what to fix.
 
Upvote 0
I don't think you can change it dynamically using the interface but if copy the first line from the following code to the beginning of your code you can refer to it later:
Power Query:
let
    FiscalYear = "Total " & Text.From(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]{0}[#"Fiscal Year"]) & " amount",
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Renamed Columns" = Table.RenameColumns(Source,{{"Total", FiscalYear}})
in
    #"Renamed Columns"
This solution works. Thanks!
 
Upvote 0
I got the following error message when I concatenated the query two expression:

We cannot apply operator & to types Text and Number.

Not sure what to fix.
Yes, forgot to mention you need to define fiscalyear as text before drilldown.
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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