Need help with power query unpivoting.

proficient

Well-known Member
Joined
Apr 10, 2012
Messages
745
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
Hello all,

Please check out the below screenshot of the normal table (there is an unnecessary break in each machine table) that I am getting from my colleague "DATA" along with the "DESIRED RESULT" that I want as the final result.

The data is in a regular excel table form and it would be time-taking to make official tables of each machine (a total of 16 machines) every time. I am looking forward to a code that will work if I convert the complete data into one sheet to save time by not making each machine an official excel table every time, Could anyone provide a code?

1666546388277.png
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Cannot manipulate data in a picture. Please upload your source data employing XL2BB so we don't have to guess and recreate the structure of your data.
 
Upvote 0
Cannot manipulate data in a picture. Please upload your source data employing XL2BB so we don't have to guess and recreate the structure of your data.
There you go!

Book4
ABCDEFGHIJKLMNOPQRS
1DATA
2DESIRED RESULT
3MACHINE 1MACHINE 2MACHINE 3MACHINE #DATEARTICLEOUTPUT
4DATEARTICLEOUTPUTDATEARTICLEOUTPUTDATEARTICLEOUTPUT11-OctABC300
51-OctABC3001-OctBCD3001-OctEFG30012-OctABC500
62-OctABC5002-OctBCD5002-OctEFG50013-OctABC500
73-OctABC5003-OctBCD5003-OctEFG50014-OctHIG500
815-OctPLK250
9DATEARTICLEOUTPUTDATEARTICLEOUTPUTDATEARTICLEOUTPUT16-OctPLK350
104-OctHIG5004-OctKLM5004-OctPQR15021-OctBCD300
115-OctPLK2505-OctKLM5005-OctPQR25022-OctBCD500
126-OctPLK3506-OctKLM5006-OctPQR50023-OctBCD500
1324-OctKLM500
1425-OctKLM500
1526-OctKLM500
1631-OctEFG300
1732-OctEFG500
1833-OctEFG500
1934-OctPQR150
2035-OctPQR250
2136-OctPQR500
22
Sheet2
Cell Formulas
RangeFormula
B6:B7,J11:J12,F11:F12,B11:B12,J6:J7,F6:F7B6=B5+1
 
Upvote 0
I used power query to achieve your expected result.
1. I loaded each machine table to Power Query and for each ran this query changing the table name for each.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([DATE] <> null and [DATE] <> "DATE")),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"DATE", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Machine", each 1),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Machine", "DATE", "ARTICLE", "OUTPUT"})
in
    #"Reordered Columns"

2. I then appended each to the other

Power Query:
let
    Source = Table.Combine({Table5, Table6, Table7})
in
    Source

My end result replicated your expected results
 
Upvote 0
I used power query to achieve your expected result.
1. I loaded each machine table to Power Query and for each ran this query changing the table name for each.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([DATE] <> null and [DATE] <> "DATE")),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"DATE", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Machine", each 1),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Machine", "DATE", "ARTICLE", "OUTPUT"})
in
    #"Reordered Columns"

2. I then appended each to the other

Power Query:
let
    Source = Table.Combine({Table5, Table6, Table7})
in
    Source

My end result replicated your expected results

Hello Alansidman,

Thank you for your efforts but the provided data is sample data, there are more than 15 machines, and I cannot make them table each time, which is why I asked if is there a way to make the complete data a table and then convert it into one data?


I am new to power query but can make one data from multiple sheet, but this is something challenging to me.
 
Upvote 0
Name machine data tables in some systematic way that you can loop over (e.g. Machine-1, Machine-2, ...) Have a cell in your spreadsheet for the number of data tables.
You should then be able to write M language code to loop over all the tables and grab their data into a single query.
 
Upvote 0
Here is a way that you can combine all of the data without needing to define each machine as its own table.

I selected all of the sample data, loaded into PQ, then applied the following steps which you should be able to just paste into the PQ advanced editor.

PROFICIENT
BCDEFGHIJKL
1DATA
2
3
4MACHINE 1MACHINE 2MACHINE 3
5DATEARTICLEOUTPUTDATEARTICLEOUTPUTDATEARTICLEOUTPUT
644835ABC30044835BCD30044835EFG300
744836ABC50044836BCD50044836EFG500
844837ABC50044837BCD50044837EFG500
9
10DATEARTICLEOUTPUTDATEARTICLEOUTPUTDATEARTICLEOUTPUT
1144838HIG50044838KLM50044838PQR150
1244839PLK25044839KLM50044839PQR250
1344840PLK35044840KLM50044840PQR500
14
15PQ Output
16MachineDATEARTICLEOUTPUT
17MACHINE 110/1/2022ABC300
18MACHINE 110/2/2022ABC500
19MACHINE 110/3/2022ABC500
20MACHINE 110/4/2022HIG500
21MACHINE 110/5/2022PLK250
22MACHINE 110/6/2022PLK350
23MACHINE 210/1/2022BCD300
24MACHINE 210/2/2022BCD500
25MACHINE 210/3/2022BCD500
26MACHINE 210/4/2022KLM500
27MACHINE 210/5/2022KLM500
28MACHINE 210/6/2022KLM500
29MACHINE 310/1/2022EFG300
30MACHINE 310/2/2022EFG500
31MACHINE 310/3/2022EFG500
32MACHINE 310/4/2022PQR150
33MACHINE 310/5/2022PQR250
34MACHINE 310/6/2022PQR500
Sheet4


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Flip0 = Table.Transpose(Source),
    RemoveBlanks0 = Table.SelectRows(Flip0, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
    Flip1 = Table.Transpose(RemoveBlanks0),
    RemoveBlanks1 = Table.Skip(Table.Distinct(Table.Skip(Table.SelectRows(Flip1, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),1),"Column1"),1),
    Machines = List.RemoveNulls(Record.ToList(Source{0})),
    Cols = List.Distinct(List.RemoveNulls(Record.ToList(Source{1}))),
    ToTables = List.Transform(List.Split(Table.ToColumns(RemoveBlanks1),List.Count(Cols)), each Table.FromColumns(_,Cols)),
    Combine = Table.FromColumns({Machines} & {ToTables},{"Machine","C2"}),
    Expand = Table.TransformColumnTypes(Table.ExpandTableColumn(Combine, "C2", Cols, Cols),{{"Machine", type text}, {"ARTICLE", type text}, {"DATE", type date}, {"OUTPUT", Int64.Type}})
in
    Expand
 
Upvote 0
Here's an updated and a little more streamlined version that avoids transposing the table multiple times.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    RemoveEmptyCols = Table.SelectColumns(Source,List.Select(Table.ColumnNames(Source), each List.MatchesAny(Table.Column(Source,_), each _ <> null))),
    RemoveEmptyRows = Table.SelectRows(RemoveEmptyCols, each _[Column1] <> null),
    Machines = List.RemoveNulls(Record.ToList(RemoveEmptyRows{0})),
    Cols = List.Distinct(Record.ToList(RemoveEmptyRows{1})),
    ToTables = List.Transform(List.Split(Table.ToColumns(Table.Skip(Table.SelectRows(RemoveEmptyRows, each _[Column1] <> "DATE"),2)),List.Count(Cols)), each Table.FromColumns(_,Cols)),
    Combine = Table.FromColumns({Machines} & {ToTables},{"Machine","C2"}),
    Expand = Table.ExpandTableColumn(Combine, "C2", Cols, Cols)
in
    Expand
 
Upvote 0
Here's an updated and a little more streamlined version that avoids transposing the table multiple times.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    RemoveEmptyCols = Table.SelectColumns(Source,List.Select(Table.ColumnNames(Source), each List.MatchesAny(Table.Column(Source,_), each _ <> null))),
    RemoveEmptyRows = Table.SelectRows(RemoveEmptyCols, each _[Column1] <> null),
    Machines = List.RemoveNulls(Record.ToList(RemoveEmptyRows{0})),
    Cols = List.Distinct(Record.ToList(RemoveEmptyRows{1})),
    ToTables = List.Transform(List.Split(Table.ToColumns(Table.Skip(Table.SelectRows(RemoveEmptyRows, each _[Column1] <> "DATE"),2)),List.Count(Cols)), each Table.FromColumns(_,Cols)),
    Combine = Table.FromColumns({Machines} & {ToTables},{"Machine","C2"}),
    Expand = Table.ExpandTableColumn(Combine, "C2", Cols, Cols)
in
    Expand
Hello Irobbo314: Could you please share the file I am getting errors.
 
Upvote 0
How do the data make it into the table? Does your colleague by any chance collect it from different files? That would be a good time to import from folder.
 
Upvote 0

Forum statistics

Threads
1,223,573
Messages
6,173,138
Members
452,501
Latest member
musallam

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