Power Query: Source Data with Variable Column Header

Graemea

Board Regular
Joined
Oct 30, 2015
Messages
119
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have created a connection using Power Query to a worksheet which is used to populate a pivot table.

Each month I will paste over the data in the worksheet and refresh connection to update the pivot table.

One problem is that the heading of one of columns in the worksheet changes each month to show the date of the latest month-end (formatted as text). For example, in this month's report the 4th column will have the header "31-08-2020" and next month it will be "30-09-2020".

This causes the query to fail when I try to refresh it.

I would want the resulting pivot table to include a field with the name of the 4th column (so "31-08-2020" this month and "30-09-2020" when I refresh it next month).

Is there a way around this other than manually to adjust the column header in the worksheet to be something that doesn't change each month?

Thanks!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
try
= Table.RenameColumns(previous_step,{{Table.ColumnNames(previous_step){3}, "Period"}})
 
Upvote 0
Thanks for the quick response!

Can I please ask how I would apply this to the M code in the attached image? I would like it to apply to both columns 3 and 4 (with headers "30 June 2020" and "31 July 2020".

Thanks! PQ_Col_Variable_Header.PNG
PQ_Col_Variable_Header.PNG
 
Upvote 0
Sorry, I've pasted the code in (I wasn't sure what you meant by
Power Query:
 so hopefully this is ok:

[CODE=pq] 
let

Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales Code", type text}, {"Product Name", type text}, {"30 Jun 2020", Int64.Type}, {"31 Jul 2020", type any}})

in

#"Changed Type"
 
Upvote 0
;)
should be
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table21"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales Code", type text}, {"Product Name", type text}, {"30 Jun 2020", Int64.Type}, {"31 Jul 2020", type any}}),
    Ren = Table.RenameColumns(#"Changed Type",{{Table.ColumnNames(#"Changed Type"){3}, "PizzaHut"}})
in
    Ren
change PizzaHut to proper name

btw. you should wrap code only by code tags not a whole text in the post
 
Upvote 0
or if you want end of month you can try this
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales Code", Int64.Type}, {"Product Name", Int64.Type}, {"30 Jun 2020", Int64.Type}, {"31 Jul 2020", Int64.Type}}),
    Ren = Table.RenameColumns(#"Changed Type",{{Table.ColumnNames(#"Changed Type"){3}, ""&Text.From(Date.EndOfMonth(Date.From(DateTime.LocalNow())))&""}})
in
    Ren
and it will give you
Sales CodeProduct Name30 Jun 202030/09/2020
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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