r/ExcelPowerQuery • • Jun 02 '24
Pivoting budget file with multiple column headers
I'm trying to unpivot a massive file that is in the format below

And trying to get it into a format like this

I've tried leveraging PowerQuery and unpivoting the data set in but I am a novice and feel like I'm missing something. I need to get the departments and months moved. Any help would be appreciated!
2
Upvotes
2
u/Dwa_Niedzwiedzie Jun 02 '24
As I assume you have three rows on the top of your table, which aren't proper headers - and this is what you need to take care of first. You must mix them a little to get a list of unique column names, and only after that you can do unpivoting. It may be something like this:
letSource = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],#"Kept First Rows" = Table.FirstN(Source,2),#"Transposed First Rows" = Table.Transpose(#"Kept First Rows"),#"Removed Blank Rows" = Table.SelectRows(#"Transposed First Rows", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),#"Merged Columns" = Table.CombineColumns(#"Removed Blank Rows",{"Column1", "Column2"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged"),#"Column names" = List.FirstN(Record.FieldValues(Source{2}), 3) & #"Merged Columns"[Merged],#"Converted to Table" = Table.FromList(#"Column names", Splitter.SplitByNothing(), null, null, ExtraValues.Error),#"Transposed Headers" = Table.Transpose(#"Converted to Table"),#"Appended Query" = Table.Combine({#"Transposed Headers", Source}),#"Promoted Headers" = Table.PromoteHeaders(#"Appended Query", [PromoteAllScalars=true]),#"Removed Top Rows" = Table.Skip(#"Promoted Headers",3),#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Top Rows", {"Version", "Account Rollup", "GL Account"}, "Attribute", "Amount"),#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Dpmt", "Month"})in#"Split Column by Delimiter"1
u/frankzygv Jun 03 '24
Hi! I have a solution:
|| || ||Agrupación de Cuentas|Cuenta GL|DMPT A|01-01-24|DMPT B|01-01-24|DMPT A|01-02-24|DMPT B|01-02-24|DMPT A|01-03-24|DMPT B|01-03-24| |Presupuesto|Gastos de Oficina|10000|5,000|3,125|7,813|19,531|97,656|61,035| |Presupuesto|Gastos de Personal|10001|7512|72687|54264|64725|34783|98701|
After that, you can Unpivot all the value columns without a problem and then split the column on whatever delimiter you chose (in this case "|")
Note: I think this is u/Dwa_Niedzwiedzie 's workaround, but his is implemented directly into Mcode
2
u/frankzygv Jun 03 '24
Hi! I have a solution:
|| || ||Agrupación de Cuentas|Cuenta GL|DMPT A|01-01-24|DMPT B|01-01-24|DMPT A|01-02-24|DMPT B|01-02-24|DMPT A|01-03-24|DMPT B|01-03-24| |Presupuesto|Gastos de Oficina|10000|5,000|3,125|7,813|19,531|97,656|61,035| |Presupuesto|Gastos de Personal|10001|7512|72687|54264|64725|34783|98701|
After that, you can Unpivot all the value columns without a problem and then split the column on whatever delimiter you chose (in this case "|")
Note: I think this is u/Dwa_Niedzwiedzie 's workaround, but his is implemented directly into Mcode