Finding Dates in Column Headers and Unpivoting them in Power Query

programsam

Board Regular
Joined
Feb 10, 2016
Messages
123
Not even sure where to start with this one but have a file that has several column headers, some with MMMM/YYYY in them. Because the file refreshes each week with new information from a folder where new data is appended to existing, I'd like to be able to unpivot the columns where dates are detected.

I would assume using something such as Date.From with some type of IF/THEN statement to look for "MMMM/YYYY" and then unpivoting but I'm unsure of the correct syntax. Is this even possible with Power Query?

1690284464472.png
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If there is a fixed set of columns not to unpivot, you could simply use that with Unpivot other columns. If not, you can do something like this:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    // get a list of the columns that look like dates
    DateColumns = List.Select(Table.ColumnNames(Source), each try Value.Is(Date.From(_), type date) otherwise false),
    // use that list to unpivot
    #"Unpivoted Columns" = Table.Unpivot(Source, DateColumns, "Attribute", "Value")
in
    #"Unpivoted Columns"
 
Upvote 1
Solution
If there is a fixed set of columns not to unpivot, you could simply use that with Unpivot other columns. If not, you can do something like this:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    // get a list of the columns that look like dates
    DateColumns = List.Select(Table.ColumnNames(Source), each try Value.Is(Date.From(_), type date) otherwise false),
    // use that list to unpivot
    #"Unpivoted Columns" = Table.Unpivot(Source, DateColumns, "Attribute", "Value")
in
    #"Unpivoted Columns"
Hi Rory, there won't be a fixed set of columns because each month, as a new month is added it will add an extra column. Is there a way to count the # of columns, then look for dates and then unpivot?

Appreciate the prompt reply.
 
Upvote 0
I said if there is a fixed list of columns not to unpivot. So you might have 5 fixed columns followed by a varying number of date columns. You can then select the fixed ones and use Unpivot Other columns, so it will unpivot anything that isn't the columns you selected.

The code I posted will check all the column names and unpivot any that look like dates.
 
Upvote 0
I said if there is a fixed list of columns not to unpivot. So you might have 5 fixed columns followed by a varying number of date columns. You can then select the fixed ones and use Unpivot Other columns, so it will unpivot anything that isn't the columns you selected.

The code I posted will check all the column names and unpivot any that look like dates.
Thank you, this worked masterfully. If there were an existing column that I also wanted to unpivot along with these called "MTD", how would this be accomplished?
 
Upvote 0
You can amend the select part to:

Power Query:
List.Select(Table.ColumnNames(Source), each _ = "MTD" or (try Value.Is(Date.From(_), type date) otherwise false))
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,157
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