PowerQuery - date as header

illusionek

Board Regular
Joined
Jun 21, 2014
Messages
104
Hello

PowerQuery does not accept fields formatted as date as a column name. When I used option 'Use First Row as Headers' instead of my dates I got Column3, Column4 etc

I am importing a table into PowerQuery so I can use Unpivot Columns option, so it is important to me that my dates are column headers.

As a workaround I change dates to text value before importing to PowerQuery, unpivot data, export back to Excel and then I change text value back to date value.

I was wondering if there is a smarter way of doing this?


Many thanks.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Why not convert the columns to text In power query (not excel), then promote to header and then convert the columns back to what ever you need. I assume that will work.
 
Upvote 0
Hi
It is a variant for example
Code:
let
    source = Table.FromRows({
    {"id",#datetime(2016,1,1,0,0,0),#datetime(2016,1,2,0,0,0),#datetime(2016,1,3,0,0,0)},
    {1,23,33,45},
    {2,31,52,26}
    }),
    headPart = Table.FirstN(source,1),
    dataPart = Table.LastN(source,Table.RowCount(source)-1),
    firstRow = Table.ToRows(headPart){0},
    dateToText = List.Transform(firstRow, each if _ is datetime then Text.From(Date.From(_)) else _),
    unpivotColNames = List.RemoveNulls(List.Transform(firstRow, each if _ is datetime then Text.From(Date.From(_)) else null)),
    newHeadPart = Table.FromRows({dateToText},Table.ColumnNames(headPart)),
    newSource = Table.Combine({newHeadPart,dataPart}),
    realHead = Table.PromoteHeaders(newSource),
    unpivoted = Table.Unpivot(realHead,unpivotColNames,"date","value"),
    return = Table.TransformColumns(unpivoted,{ {"date", each Date.From(_),type date} })
in
    return
Regards,
 
Upvote 0
I know this is an old post but in case anyone comes across it...

To promote all headers, regardless of their data type (e.g. date), simply add the following flag:

Table.PromoteHeaders(
, [PromoteAllScalars = true])

Simply changing the table to text will not work in certain situations, e.g. ETL'ing many templates that change slightly.


Simon
 
Last edited:
Upvote 0
Hi Simon,
I've spent hours trying to find a solution and came across your post. I've given it a go but it came back with an error. Maybe you can tell me if I did something wrong.

I'll run through what I did....

1. Promoted the Header
2. Selected fx.
3. What automatically appeared was = #"Promoted Headers". I replaced this with = Table.PromoteHeaders(
, [PromoteAllScalars = true])
4. This returned an error

It would be very kind of you if you assisted.

Thanks
Carmelle



I know this is an old post but in case anyone comes across it...

To promote all headers, regardless of their data type (e.g. date), simply add the following flag:

Table.PromoteHeaders(
, [PromoteAllScalars = true])

Simply changing the table to text will not work in certain situations, e.g. ETL'ing many templates that change slightly.


Simon
 
Upvote 0
For Future reference. I tried to convert my data into table first by using Insert Table before using Power query. Once your data in in excel dynamic table Power QUery will automatically take date as header.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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