PowerQuery Function: Unpivot Data

jajatidev

Board Regular
Joined
Jul 29, 2016
Messages
82
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have pivoted monthly forecast files stored in a SharePoint folder.
To unpivot the data I created the following PQ Function. However, I'm getting the following error message while trying to pass the files.

Can someone please guide me on how to overcome the issue?

1735492595118.png


Power Query:
(MyTable as table) =>
let
    // Filter out hidden files
    #"Filtered Hidden Files1" = Table.SelectRows(MyTable, each [Attributes]?[Hidden]? <> true),
    
    // Add a custom column to transform the binary content to a table
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File (2)", each Csv.Document(Binary.Decompress([Content], Compression.Deflate))),
    
    // Rename columns for clarity
    #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
    
    // Remove unnecessary columns
    #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File (2)"}),
    
    // Expand the table column to get the data
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File (2)", Table.ColumnNames(#"Transform File (2)"(#"Sample File (2)"))),
    
    // Promote headers
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Table Column1", [PromoteAllScalars=true]),
    
    // Get the first column name for renaming
    CycleForecast = Table.ColumnNames(#"Promoted Headers"){0},
    
    // Rename the first column to "Forecast_Month"
    ForecastMonth = Table.RenameColumns(#"Promoted Headers", {{CycleForecast, "Forecast_Month"}}),
    
    // Replace specific values in the "Forecast_Month" column
    #"Replaced Value" = Table.ReplaceValue(ForecastMonth, "Monthly_Direct_Forecast_Hardware_", "", Replacer.ReplaceText, {"Forecast_Month"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value", ".csv", "", Replacer.ReplaceText, {"Forecast_Month"}),
    
    // Unpivot the other columns
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Replaced Value1", {"Forecast_Month", "Business Area", "SKU No.", "PL"}, "Attribute", "Value"),
    
    // Change the data types of specific columns
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns", {{"Value", Int64.Type}, {"PL", type text}, {"SKU No.", type text}, {"Business Area", type text}})
in
    #"Changed Type"
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Converting binary to excel would be something like (instead of your second step).

Change the table name too in the code below. I used myTable but that should be the previous step

Power Query:
trform = List.Transform(myTable[Content], (x)=> Excel.Workbook(x))
 
Upvote 0
Converting binary to excel would be something like (instead of your second step).

Change the table name too in the code below. I used myTable but that should be the previous step

Power Query:
trform = List.Transform(myTable[Content], (x)=> Excel.Workbook(x))

Hello JEC,
New Year Greeting! I hope you had a great time with your family and friends.

Thank you for getting back to me. I tried as advised however, I am still faced with the same error.

As mentioned, I'm trying to create a function that can be used for parsing multiple workbooks and then expand for the final output. To achieve this I'm adding a custom column with the following formula

Power Query:
Unpivot([Content]

1735921520579.png


Power Query:
(myTable as table) =>
let
    trform = List.Transform(myTable[Content], (x) => Excel.Workbook(x)),
    #"Invoke Custom Function1" = Table.AddColumn(trform, "Transform File (2)", each #"Transform File (2)"([Content])),
    #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File (2)"}),
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File (2)", Table.ColumnNames(#"Transform File (2)"(#"Sample File (2)"))),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Table Column1", [PromoteAllScalars=true]),
    CycleForecast = Table.ColumnNames (#"Promoted Headers"){0},
    ForecastMonth = Table.RenameColumns (#"Promoted Headers", {{CycleForecast, "Forecast_Month"}}),
    #"Replaced Value" = Table.ReplaceValue(ForecastMonth,"Monthly_Direct_Forecast_Hardware_","",Replacer.ReplaceText,{"Forecast_Month"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",".csv","",Replacer.ReplaceText,{"Forecast_Month"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Replaced Value1", {"Forecast_Month", "Business Area", "SKU No.", "PL"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", Int64.Type}, {"PL", type text}, {"SKU No.", type text}, {"Business Area", type text}})
in
    #"Changed Type"
 
Upvote 0
@jajatidev , your original function works with table as parameter. Such table must have a column Content with binary values (#"Removed Columns" table in your image).
You are trying to invoke your function via user interface as Add Column >> Invoke Custom Function. This not going to work because Table.AddColumn passes each instance of Content column into your function which is binary. That's why you get "can't convert value of type Binary to type Table" error.
I don't know how to help you using UI only, sorry.
p.s. Excel.Workbook has nothing to do with your data (csv files).
 
Upvote 0
@jajatidev , your original function works with table as parameter. Such table must have a column Content with binary values (#"Removed Columns" table in your image).
You are trying to invoke your function via user interface as Add Column >> Invoke Custom Function. This not going to work because Table.AddColumn passes each instance of Content column into your function which is binary. That's why you get "can't convert value of type Binary to type Table" error.
I don't know how to help you using UI only, sorry.
p.s. Excel.Workbook has nothing to do with your data (csv files).

Hello AlienSx,
New Year Greetings! Thanks for the feedback.

The CSV files I'm dealing with are published and saved in a SharePoint Folder. The list of reports is expected to grow over time. Hence, I'm trying to create this function to unpivot and append all files available in the SharePoint folder before I can begin massaging the data. Do you have any suggestions on how to approach this?

Here is the initial query I'm trying to convert into a dynamic function that can be applied to all the files in the specific folder so that I do not have to repeat the process.

Power Query:
let
    Source = SharePoint.Files("SharePointLink", [ApiVersion = 15]),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Folder Path] = "SharePointLink") and ([Name] = "Monthly_Forecast_May-24.csv")),
    #"Filtered Hidden Files1" = Table.SelectRows(#"Filtered Rows", each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
    #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File"}),
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Table Column1", [PromoteAllScalars=true]),
    SourceName = Table.ColumnNames (#"Promoted Headers"){0},
    RenameHeader = Table.RenameColumns (#"Promoted Headers",{{SourceName, "Forecast_Cycle"}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(RenameHeader, {"Forecast_Cycle", "SKU No.", "PL"}, "Attribute", "Value")
in
    #"Unpivoted Other Columns"


Here is the sample data from a couple of files for your reference. I hope this will facilitate a solution.

Monthly_Forecast_May-24.csv
ABCDEFGHIJK
1SKU No.PLJun-25Jul-25Aug-25Sep-25Oct-25Nov-25Dec-25Jan-25Feb-25
21NR73A-K1HAP31850505050505000
3A1A30A-KGJAL31850505050505000
45RA32F-KGJAL312318311321343320233207207
5A1A77A-KGJAL298414081648197314541267115700
6N7L37AST35017213814413179625332
72Z600F-KGJAL597360318356263196167171165
83JA05AN-150AN3885931252614162630
9A1A56A-K1HAP3791839611410189737079
102Z613F-K1HAP69717512111510799936868
113GZ25A-K1HAP333333333
12K5L35AST193013901053838797602578295291
13Y3D03AST4428953311210
14Z7C07AST18071152113012951099228226182144
153PZ35A-150AN2942303342281861031712628
16D7P27AST597360318356263196167171165
Monthly_Forecast_May-24


Monthly_Forecast_Jun-24.csv
ABCDEFGHIJKL
1SKU No.PLJun-25Jul-25Aug-25Sep-25Oct-25Nov-25Dec-25Jan-25Feb-25Mar-25
21NR73A-K1HAP503535050505050000
3A1A30A-KGJAL503535050505050000
45RA32F-KGJAL5360284244310238282363487249
5A1A77A-KGJAL1075285216481973145412671157000
6N7L37AST1681431291171435948171313
72Z600F-KGJAL26831811212811610488849393
83JA05AN-150AN3793943160271416263028
9A1A56A-K1HAP22744103256118301347015091
102Z613F-K1HAP884329599102100101919191
113GZ25A-K1HAP3000000000
12K5L35AST199421991074875763670722328318310
13Y3D03AST1107716102141413131313
14Z7C07AST14411756131114621267233227344309309
153PZ35A-150AN228314342202841772793632
16D7P27AST13742297109010121002645700724656707
Monthly_Forecast_Jun-24
 
Upvote 0
Use this data for June instead of the one provided earlier

Monthly_Forecast_Jun-24.csv
ABCDEFGHIJK
1SKU No.PLJul-25Aug-25Sep-25Oct-25Nov-25Dec-25Jan-25Feb-25Mar-25
21NR73A-K1HAP3535050505050000
3A1A30A-KGJAL3535050505050000
45RA32F-KGJAL60284244310238282363487249
5A1A77A-KGJAL285216481973145412671157000
6N7L37AST1431291171435948171313
72Z600F-KGJAL31811212811610488849393
83JA05AN-150AN3943160271416263028
9A1A56A-K1HAP744103256118301347015091
102Z613F-K1HAP4329599102100101919191
113GZ25A-K1HAP000000000
12K5L35AST21991074875763670722328318310
13Y3D03AST7716102141413131313
14Z7C07AST1756131114621267233227344309309
153PZ35A-150AN314342202841772793632
16D7P27AST2297109010121002645700724656707
Monthly_Forecast_Jun-24
 
Upvote 0
@jajatidev , completely blind solution (I don't use SP). Try this. If it works then you can create a function out of it with a path to SP folder as a parameter
Power Query:
let
    unpivot_csv = (bin as binary) => Table.UnpivotOtherColumns(
        Table.PromoteHeaders(Csv.Document(bin)),
        {"SKU No.", "PL"},
        "Attribute",
        "Value"
    ),
    row_files = SharePoint.Files("SharePointLink", [ApiVersion = 15]),
    remove_hidden = Table.SelectRows(row_files, each [Attributes]?[Hidden]? <> true)[[Content], [Name]],
    transormations = Table.TransformColumns(
        remove_hidden,
        {
            {"Content", unpivot_csv},
            {"Name", (x) => Text.BetweenDelimiters(x, "_", ".", {0, RelativePosition.FromEnd})}
        }
    ),
    renames = Table.RenameColumns(transformations, {{"Name", "Forecast_Month"}}),
    expand = Table.ExpandTableColumn(renames, "Content", {"SKU No.", "PL", "Attribute", "Value"})
in
    expand
You should use Advanced Editor to create a blank query and paste this code. I don't know what "SharePointLink" is . Must be some URL...
 
Upvote 0

Forum statistics

Threads
1,225,135
Messages
6,183,066
Members
453,147
Latest member
Lacey D

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