PowerQuery - cleansing and transposing multiple headers

batteredveg

New Member
Joined
Aug 27, 2014
Messages
7
Hi folks,

I'd be grateful for any help with what should be a really simple set of PowerQuery steps - it's driving me mad as I just cannot get it right.

Rather than (embarrasing myself by) pasting my current set of steps, let me show you what I have to work with (obviously the input document is significantly larger) and what I'm trying to achieve. Ignore the fact the figures are the same for each 'department' - it was just a quick copy/paste job to set the scene.

Essentially I need to cleanse the input, pivot the 'Department' column, and summarise the Figures 1 to 5 for each 'Department':

Power Query Help.xlsx
ABCDEFGHIJKLMNOPQ
1Current CSV import:
2
3Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column12Column13Column14Column15Column16Column17
4DepartmentSalesSalesSalesSalesSalesITITITITITPurchasingPurchasingPurchasingPurchasingPurchasing
5Company NameCompany NumberFigure 1Figure 2Figure 3Figure 4Figure 5Figure 1Figure 2Figure 3Figure 4Figure 5Figure 1Figure 2Figure 3Figure 4Figure 5
6Quimba2963520875$199087.05$570792.24$479678.24$215338.21$630880.28$199087.05$570792.24$479678.24$215338.21$630880.28$199087.05$570792.24$479678.24$215338.21$630880.28
7Centimia3423440279$215096.01$633523.02$861394.69$9620.50$86986.09$215096.01$633523.02$861394.69$9620.50$86986.09$215096.01$633523.02$861394.69$9620.50$86986.09
8Tanoodle8969707689$828544.93$225343.97$47155.50$955012.74$822255.72$828544.93$225343.97$47155.50$955012.74$822255.72$828544.93$225343.97$47155.50$955012.74$822255.72
9Skippad8463523196$947240.25$475782.54$231024.55$923782.09$638132.10$947240.25$475782.54$231024.55$923782.09$638132.10$947240.25$475782.54$231024.55$923782.09$638132.10
10Aivee4637729386$921910.87$813896.35$262644.73$271890.69$258548.49$921910.87$813896.35$262644.73$271890.69$258548.49$921910.87$813896.35$262644.73$271890.69$258548.49
11
12What I want to convert this to in PowerQuery:
13
14Company NameCompany NumberDepartmentFigure 1Figure 2Figure 3Figure 4Figure 5
15Quimba2963520875Sales$199087.05$570792.24$479678.24$215338.21$630880.28
16Quimba2963520875IT$199087.05$570792.24$479678.24$215338.21$630880.28
17Quimba2963520875Purchasing$199087.05$570792.24$479678.24$215338.21$630880.28
18Centimia3423440279Sales$215096.01$633523.02$861394.69$9620.50$86986.09
19Centimia3423440279IT$215096.01$633523.02$861394.69$9620.50$86986.09
20Centimia3423440279Purchasing$215096.01$633523.02$861394.69$9620.50$86986.09
21Tanoodle8969707689Sales$828544.93$225343.97$47155.50$955012.74$822255.72
22Tanoodle8969707689IT$828544.93$225343.97$47155.50$955012.74$822255.72
23Tanoodle8969707689Purchasing$828544.93$225343.97$47155.50$955012.74$822255.72
24Skippad8463523196Sales$947240.25$475782.54$231024.55$923782.09$638132.10
25Skippad8463523196IT$947240.25$475782.54$231024.55$923782.09$638132.10
26Skippad8463523196Purchasing$947240.25$475782.54$231024.55$923782.09$638132.10
27Aivee4637729386Sales$921910.87$813896.35$262644.73$271890.69$258548.49
28Aivee4637729386IT$921910.87$813896.35$262644.73$271890.69$258548.49
29Aivee4637729386Purchasing$921910.87$813896.35$262644.73$271890.69$258548.49
Sheet1


Any advice gratefully received!

Best wishes,

P
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
maybe

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Depts = List.Distinct(List.RemoveItems(Record.ToList(Source{0}), {"Department", null})),
    cols = List.Buffer(Table.ToColumns(Source)),
    firstcols = List.Buffer(List.FirstN(cols,2)),
    deptcols = List.Buffer(List.Skip(cols,2)),
    lst = List.Buffer(List.Accumulate(Depts, {}, (s,c)=> s & { firstcols & List.Select(deptcols, each _{0} = c) & {{c, "Department", c}}} )),
    lst1 = List.Transform(lst, each Table.FillDown(Table.PromoteHeaders(Table.Skip(Table.FromColumns(_),1)), {"Department"})),
    tbl = Table.FromList(lst1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    tcn = List.Buffer(Table.ColumnNames(tbl[Column1]{0})),
    tbl1 = Table.ExpandTableColumn(tbl, "Column1",tcn),
    reorderlist = List.FirstN(tcn,2) & {"Department"} & List.Range(tcn, 2, List.Count(tcn) -3),
    tbl2 = Table.ReorderColumns(tbl1,reorderlist),
    Result = Table.Sort(tbl2,{{"Company Name", Order.Ascending}, {"Department", Order.Ascending}})
in  
    Result
 
Upvote 0
Or something like:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(Source, {{"Column2", type text}}, "en-GB"),{"Column1", "Column2"},Combiner.CombineTextByDelimiter("~", QuoteStyle.None),"Merged"),
    #"Transposed Table" = Table.Transpose(#"Merged Columns"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"~Department", "Company Name~Company Number"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("~", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[#"Company Name~Company Number"]), "Company Name~Company Number", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"Attribute.1", "Company Name"}, {"Attribute.2", "Company number"}, {"~Department", "Department"}})
in
    #"Renamed Columns"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,366
Messages
6,171,665
Members
452,416
Latest member
johnog

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