Power Query Importing all files in folder and unpivot

joe321

New Member
Joined
Jan 28, 2015
Messages
17
I would like to replace existing functioning VBA code with Power Query to do the following:

Process all Excel files in a folder and import into pivot table format.

The Excel data has row labels in column A and values in columns B to M. The row headers in B to M are years 2000,2001,2002 etc. I currently unpivot this data via VBA and import it into pivot table format. In a hidden column to the right I have a unique identifier for the row so if future versions of the excel model have lines added or deleted, I can match them up in PowerPivot.

In PowerPivot, I compare the results of many different scenarios creaed in Excel.

I need to import the following:
1) Scenario Name (it is listed in a cell in the excel file)
2) Year,RowId and value for each row columns B to M

Also, the years need to be "unpivoted"

I am a relative novice in Power Query, but am familar with the unpivot function the import all files in a folder menu item.

Any ideas to get me started are appreciated.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi
Maybe a part of my work code helps you to solve your task
Code:
let
    //get a work folder from a named cell in a report workbook
    fileFolder = Excel.CurrentWorkbook(){[Name="Путь"]}[Content]{0}[Column1],
    //get a table of information about files in the folder
    source = Folder.Files(fileFolder),
    //filter the table by .xlsx extension and get a workbook content list
    folderData = Table.SelectRows(source,each Text.Lower([Extension])=".xlsx")[Content],
    //get information about workbooks structure
    baseTables = Table.Combine(List.Transform(List.Positions(folderData),each Excel.Workbook(folderData{_},true))),
    //get data list of worksheets where each item is a table (each workbook has one sheet only)
    neededTables = Table.SelectRows(baseTables, each [Kind]="Sheet")[Data],
    //unpivot each table in the data list
    unpivoted = List.Transform(List.Positions(neededTables), each GetUnpivotedTable(neededTables{_},{"Дата посещения"},type number,"DateVal","Value")),
    //create a combined table from one
    combined = Table.Combine(unpivoted)
in
    combined
GetUnpivotedTable code is
Code:
let
    Creator = (this as table, FixedColNames as list, OtherType as type, AttributeColName as text, ValueColName as text) as table =>

let
    unpivotNames = List.Difference(Table.ColumnNames(this), FixedColNames),
    hasUnpivot = if List.Count(unpivotNames) > 0 then true else false,
    unpivotTypes = if hasUnpivot then List.Transform(List.Positions(unpivotNames), each {unpivotNames{_}, OtherType}) else null,
    typedTable = if hasUnpivot then Table.TransformColumnTypes(this, unpivotTypes) else this,
    unpivotedTable = if hasUnpivot then Table.UnpivotOtherColumns(typedTable, FixedColNames, AttributeColName, ValueColName) else this
in
    unpivotedTable
in
    Creator
Regards,
 
Upvote 0
Thanks anvg!
I'm a novice to M language too but your solution works very well for me! Can you also tell me how you'd keep the file names? thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,215,408
Messages
6,124,727
Members
449,185
Latest member
ekrause77

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