Power Query: Selection of columns with date headers

jajatidev

Board Regular
Joined
Jul 29, 2016
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Hello All,
I receive an Excel file weekly where the table is already in pivoted format with the below-mentioned headers.
Please advise what do I need to do as a power query step to select the column headers marked in green because the columns with date header is subject to change where past month columns will be retained and new months getting added.

Adoption Manager or CSM
Opportunity Owner (Sales)
Opportunity ID
Opportunity Name
Customer Name
Mfg Part/SKU #
Option Code
Item Description
11/1/2023
12/1/2023
1/1/2024
2/1/2024
3/1/2024
4/1/2024
5/1/2024
6/1/2024
7/1/2024
8/1/2024
9/1/2024
10/1/2024
11/1/2024
12/1/2024
1/1/2025
2/1/2025
3/1/2025
4/1/2025
File Name
Product Type
Sales Stage
PL
NPI EOL Notes
SKU Status
Row Validation
Flexworker SKU
Sales Territory ID
Platform ID
Distributor
Country
Billing Type
SKU + Option Code
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Assuming your table is named Table1, you could do this:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Custom1 = Table.ColumnNames(Source),
    #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    ColumnsToKeep = Table.ToList(Table.SelectRows(#"Converted to Table", each Text.Contains([Column1], "#") or Text.Contains([Column1], "/"))),
    Custom2 = Source,
    #"Removed Other Columns" = Table.SelectColumns(Custom2,ColumnsToKeep)
in
    #"Removed Other Columns"

You get the column names by using Table.ColumnNames, then you convert it to a table. Filter out the columns where they contain "#" and "/", and then convert it back to a list using Table.ToList

Then you create another step referencing the main Source and then use the Remove Other Columns feature and reference the list (which is called ColumnsToKeep in above code)
 
Upvote 0
Solution
Assuming your table is named Table1, you could do this:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Custom1 = Table.ColumnNames(Source),
    #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    ColumnsToKeep = Table.ToList(Table.SelectRows(#"Converted to Table", each Text.Contains([Column1], "#") or Text.Contains([Column1], "/"))),
    Custom2 = Source,
    #"Removed Other Columns" = Table.SelectColumns(Custom2,ColumnsToKeep)
in
    #"Removed Other Columns"

You get the column names by using Table.ColumnNames, then you convert it to a table. Filter out the columns where they contain "#" and "/", and then convert it back to a list using Table.ToList

Then you create another step referencing the main Source and then use the Remove Other Columns feature and reference the list (which is called ColumnsToKeep in above code)
Thank you. Please allow me time to apply the solution and share my findings.
 
Upvote 0
Alternative build with UI
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    First_record = Table.First(Source),
    Convert_to_table = Record.ToTable(First_record),
    Set_as_date = Table.TransformColumnTypes(Convert_to_table,{{"Value", type datetime}}),
    Remove_errors = Table.RemoveRowsWithErrors(Set_as_date, {"Value"}),
    Set_as_txt = Table.TransformColumnTypes(Remove_errors,{{"Value", type text}})[Value],
    Promote_headers = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    Unpivot_date_cols = Table.Unpivot(Promote_headers, Set_as_txt, "Date", "Value")
in
    Unpivot_date_cols
 
Upvote 0
Alternative build with UI
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    First_record = Table.First(Source),
    Convert_to_table = Record.ToTable(First_record),
    Set_as_date = Table.TransformColumnTypes(Convert_to_table,{{"Value", type datetime}}),
    Remove_errors = Table.RemoveRowsWithErrors(Set_as_date, {"Value"}),
    Set_as_txt = Table.TransformColumnTypes(Remove_errors,{{"Value", type text}})[Value],
    Promote_headers = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    Unpivot_date_cols = Table.Unpivot(Promote_headers, Set_as_txt, "Date", "Value")
in
    Unpivot_date_cols
Hi,
I wasn't able to replicate the solution which could be due to the format of the data I shared. Therefore, to make things easier I'm sharing a sample of the data in the format I receive.
When I get to transform data by promoting the header, the change type step gets automatically added which is the header I shared earlier.

Adoption Manager or CSMOpportunity Owner (Sales)Opportunity IDOpportunity NameCustomer NameMfg Part/SKU #Option CodeItem DescriptionNov-23Dec-23Jan-24Feb-24Mar-24Apr-24May-24Jun-24Jul-24Aug-24Sep-24Oct-24Nov-24Dec-24Jan-25Feb-25Mar-25Apr-25File NameProduct TypeSales StagePLNPI EOL NotesSKU StatusRow ValidationFlexworker SKUSales Territory IDPlatform IDDistributorCountryBilling TypeSKU + Option Code
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJ55252520202020MCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ
HardyShawnMCD 2022 PMSMCD CompanyS7C05D55252520202020MCD Company Forecast06 - WonSJGreenGreen354S7C05D
HardyShawnMCD 2022 PMSMCD Company3MS25DBZJ10105MCD Company Forecast06 - WonMPYellowGreen3543MS25D#BZJ
HardyShawnMCD 2022 PMSMCD CompanyB5E34D10105MCD Company Forecast06 - WonSQGreenGreen354B5E34D
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJ192020208MCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJ13131314MCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ
 
Upvote 0
Hi,
I wasn't able to replicate the solution which could be due to the format of the data I shared. Therefore, to make things easier I'm sharing a sample of the data in the format I receive.
When I get to transform data by promoting the header, the change type step gets automatically added which is the header I shared earlier.
Same query on your shared data does work on my end.
Try with changing the source step to your source. Or share the code you try.
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/11/2023 0:00:005
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/12/2023 0:00:005
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/01/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/02/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/03/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/04/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/05/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/06/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/11/2023 0:00:005
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/12/2023 0:00:005
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/01/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/02/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/03/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/04/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/05/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/06/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3MS25DBZJMCD Company Forecast06 - WonMPYellowGreen3543MS25D#BZJ1/11/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD Company3MS25DBZJMCD Company Forecast06 - WonMPYellowGreen3543MS25D#BZJ1/12/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD Company3MS25DBZJMCD Company Forecast06 - WonMPYellowGreen3543MS25D#BZJ1/01/2024 0:00:005
HardyShawnMCD 2022 PMSMCD CompanyB5E34DMCD Company Forecast06 - WonSQGreenGreen354B5E34D1/11/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD CompanyB5E34DMCD Company Forecast06 - WonSQGreenGreen354B5E34D1/12/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD CompanyB5E34DMCD Company Forecast06 - WonSQGreenGreen354B5E34D1/01/2024 0:00:005
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/11/2023 0:00:0019
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/12/2023 0:00:0020
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/01/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/02/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/03/2024 0:00:008
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/11/2023 0:00:0013
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/12/2023 0:00:0013
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/01/2024 0:00:0013
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/02/2024 0:00:0014
 
Upvote 0
Same query on your shared data does work on my end.
Try with changing the source step to your source. Or share the code you try.
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/11/2023 0:00:005
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/12/2023 0:00:005
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/01/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/02/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/03/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/04/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/05/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3ZD75DBZJMCD Company Forecast06 - WonGBGreenGreen3543ZD75D#BZJ1/06/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/11/2023 0:00:005
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/12/2023 0:00:005
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/01/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/02/2024 0:00:0025
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/03/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/04/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/05/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD CompanyS7C05DMCD Company Forecast06 - WonSJGreenGreen354S7C05D1/06/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company3MS25DBZJMCD Company Forecast06 - WonMPYellowGreen3543MS25D#BZJ1/11/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD Company3MS25DBZJMCD Company Forecast06 - WonMPYellowGreen3543MS25D#BZJ1/12/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD Company3MS25DBZJMCD Company Forecast06 - WonMPYellowGreen3543MS25D#BZJ1/01/2024 0:00:005
HardyShawnMCD 2022 PMSMCD CompanyB5E34DMCD Company Forecast06 - WonSQGreenGreen354B5E34D1/11/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD CompanyB5E34DMCD Company Forecast06 - WonSQGreenGreen354B5E34D1/12/2023 0:00:0010
HardyShawnMCD 2022 PMSMCD CompanyB5E34DMCD Company Forecast06 - WonSQGreenGreen354B5E34D1/01/2024 0:00:005
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/11/2023 0:00:0019
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/12/2023 0:00:0020
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/01/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/02/2024 0:00:0020
HardyShawnMCD 2022 PMSMCD Company5ZJ90DBZJMCD Company Forecast06 - WonBGGreenGreen3545ZJ90D#BZJ1/03/2024 0:00:008
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/11/2023 0:00:0013
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/12/2023 0:00:0013
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/01/2024 0:00:0013
HardyShawnMCD 2022 PMSMCD Company3ZD35DBZJMCD Company Forecast06 - WonMPGreenGreen3543ZD35D#BZJ1/02/2024 0:00:0014
Here are the steps

Power Query:
let
    Source = Excel.Workbook(), null, true),
    Data_Sheet = Source{[Item="Data",Kind="Sheet"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Data_Sheet,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type any}, {"Column10", type any}, {"Column11", type any}, {"Column12", type any}, {"Column13", type any}, {"Column14", type any}, {"Column15", type any}, {"Column16", type any}, {"Column17", type any}, {"Column18", type any}, {"Column19", type any}, {"Column20", type any}, {"Column21", type any}, {"Column22", type any}, {"Column23", type any}, {"Column24", type any}, {"Column25", type any}, {"Column26", type any}, {"Column27", type text}, {"Column28", type text}, {"Column29", type text}, {"Column30", type text}, {"Column31", type text}, {"Column32", type text}, {"Column33", type text}, {"Column34", type text}, {"Column35", type text}, {"Column36", type any}, {"Column37", type text}, {"Column38", type text}, {"Column39", type text}, {"Column40", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Adoption Manager or CSM", type text}, {"Opportunity Owner (Sales)", type text}, {"Opportunity ID", type text}, {"Opportunity Name", type text}, {"Customer Name", type text}, {"Mfg Part/SKU #", type text}, {"Option Code", type text}, {"Item Description", type text}, {"11/1/2023", Int64.Type}, {"12/1/2023", Int64.Type}, {"1/1/2024", Int64.Type}, {"2/1/2024", Int64.Type}, {"3/1/2024", Int64.Type}, {"4/1/2024", Int64.Type}, {"5/1/2024", Int64.Type}, {"6/1/2024", Int64.Type}, {"7/1/2024", Int64.Type}, {"8/1/2024", Int64.Type}, {"9/1/2024", Int64.Type}, {"10/1/2024", Int64.Type}, {"11/1/2024", Int64.Type}, {"12/1/2024", Int64.Type}, {"1/1/2025", Int64.Type}, {"2/1/2025", Int64.Type}, {"3/1/2025", Int64.Type}, {"4/1/2025", Int64.Type}, {"File Name", type text}, {"Product Type", type text}, {"Sales Stage", type text}, {"PL", type text}, {"NPI EOL Notes", type text}, {"SKU Status", type text}, {"Row Validation", type text}, {"Flexworker SKU", type text}, {"Sales Territory ID", Int64.Type}, {"Platform ID", type any}, {"Distributor", type text}, {"Country", type text}, {"Billing Type", type text}, {"SKU + Option Code", type text}})

in
    #"Changed Type1"
 
Upvote 0
You can try with this.
Power Query:
let
    Source = Excel.Workbook(), null, true),
    Data_Sheet = Source{[Item="Data",Kind="Sheet"]}[Data],
    First_record = Table.First(Data_Sheet),
    Convert_to_table = Record.ToTable(First_record),
    Set_as_date = Table.TransformColumnTypes(Convert_to_table,{{"Value", type datetime}}),
    Remove_errors = Table.RemoveRowsWithErrors(Set_as_date, {"Value"}),
    Set_as_txt = Table.TransformColumnTypes(Remove_errors,{{"Value", type text}})[Value],
    Promote_headers = Table.PromoteHeaders(Data_Sheet, [PromoteAllScalars=true]),
    Unpivot_date_cols = Table.Unpivot(Promote_headers, Set_as_txt, "Date", "Value")
in
    Unpivot_date_cols
 
Upvote 1
You can try with this.
Power Query:
let
    Source = Excel.Workbook(), null, true),
    Data_Sheet = Source{[Item="Data",Kind="Sheet"]}[Data],
    First_record = Table.First(Data_Sheet),
    Convert_to_table = Record.ToTable(First_record),
    Set_as_date = Table.TransformColumnTypes(Convert_to_table,{{"Value", type datetime}}),
    Remove_errors = Table.RemoveRowsWithErrors(Set_as_date, {"Value"}),
    Set_as_txt = Table.TransformColumnTypes(Remove_errors,{{"Value", type text}})[Value],
    Promote_headers = Table.PromoteHeaders(Data_Sheet, [PromoteAllScalars=true]),
    Unpivot_date_cols = Table.Unpivot(Promote_headers, Set_as_txt, "Date", "Value")
in
    Unpivot_date_cols
Thank you, GraH.
The solution works as expected. However, after comparison, I'm more comfortable using the one proposed by ExceltoDAX.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

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