Power Query - dynamic headers

KimC2504

Board Regular
Joined
Jul 17, 2012
Messages
141
Hello,
I am trying to deal with Dynamic Headers in Power Query and I have no idea how to go about it and if in fact it can be done.

I have two examples below, the Product Headers can change each time I run my report. Any suggestions how I can deal with this?

Oppty IDNameNumberDateRepProduct AProduct BProduct C
OPE-123FostersA115531/12/2019James10050
OPE-456CUBB115515/12/2019Pat50100
OPE-111SquireA159815/1/2020Tom99
OPE-145BoagA147815/2/2020Felix60




Oppty IDNameNumberDateRepProduct BProduct DProduct CProduct EProduct G
OPE-999CUBB11551/6/2020Tom1001005
OPE-874SquireA159815/1/2020Tina5011
OPE-144FostersA115511/1/2020Tiny505050
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What are you trying to do with the variable columns? Do you simply want to delete them? Are you unpivoting into a generic "Product" column? Do the product names all come from a "master" list where there can't be any more than what's in the list, or can they be anything?
 
Upvote 0
Yes I want to unpivot them into a generic product column.

The products all come from a master list where they are limited.
 
Upvote 0
If this is what you are looking for below then use the Mcode shown below it.

Book1
ABCDEFG
1Oppty IDNameNumberDateRepAttributeValue
2OPE-123FostersA115531/12/2019JamesProduct A100
3OPE-123FostersA115531/12/2019JamesProduct B50
4OPE-456CUBB115515/12/2019PatProduct B50
5OPE-456CUBB115515/12/2019PatProduct C100
6OPE-111SquireA159815/1/2020TomProduct C99
7OPE-145BoagA147815/2/2020FelixProduct A60
Sheet2


VBA Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Oppty ID", type text}, {"Name", type text}, {"Number", type text}, {"Date", type text}, {"Rep", type text}, {"Product A", Int64.Type}, {"Product B", Int64.Type}, {"Product C", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Oppty ID", "Name", "Number", "Date", "Rep"}, "Attribute", "Value")
in
    #"Unpivoted Columns"
 
Upvote 0
Hi Alan,
that works fine for the first table but my problem is next time I run the report, it will be the second example and the power query wants to see Product A and won't pick up the new columns.
Sometimes all product columns will be in the table and other times only a subset of Product columns will be in the table
 
Upvote 0
remove line #"Changed Type"...
and change in this line #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, ...

I assume the first five columns are always the same
 
Upvote 0
Hi Sandy666,
Is this what you mean?


VBA Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
  #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source,{{"Oppty ID", "Name", "Number", "Date", "Rep"}, "Attribute", "Value"})
in
    #"Unpivoted Columns"

I get an expression error

First five columns are always the same
 

Attachments

  • snip.PNG
    snip.PNG
    70.3 KB · Views: 8
Upvote 0
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Oppty ID", "Name", "Number", "Date", "Rep"}, "Attribute", "Value")
in
    #"Unpivoted Other Columns"
you did something what I didn't say
#"Unpivoted Columns" = Table.UnpivotOtherColumns(Source,{{"Oppty ID", "Name", "Number", "Date", "Rep"}, "Attribute", "Value"})
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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