PQ: append tables and get unique values

illusionek

Board Regular
Joined
Jun 21, 2014
Messages
104
Hi Guys

Please could you help me to develop this solution in the Power Query. I believe I understand steps I need to go through or at least this is how I would do it in MS Access but not sure how to do it in PQ as I am really new to this. I especially struggle with Steps 4, 5, 6


1 Bring all tables into the data model
2 Group by Product/Item No & Colour to get total quantity for each as there may be duplicates in each column
3 Append both tables for Product/Item No and Colour
4 ‘Select Distinct’ from appended table to get unique list of Product/Item Nos and Colours
5 Create new table with the list from Step 4
6 Create a link between table from Step 5 and tables from Step 2. Link is based on the combination of Product/Item No & Colour (ie concatenate)

7 Bring qtys from both columns
8 Create function to deduct one from the other



Table A

Item NoColourQtySupplier
A123Blue1S123
B123Pink3S456
A123Blue4S123
C123White3S456
D123Green2S456
D123Pink1S123

<tbody>
</tbody>

Table B

Product NoColour NameQuantityCustomer
A123Blue3ABC
B123Black4CDE
A123Blue5FEG
C123White2ABC
D123Green3FEG
D123Grey1CDE

<tbody>
</tbody>

Results

Product NoColour NameTable ATable BVariance
A123Blue58-3
B123Black04-4
B123Pink303
C123White321
D123Green23-1
D123Grey01-1
D123Pink101

<tbody>
</tbody>
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Make sure your headers are somewhat consistent. Change them to the below and follow the steps. Reach out with questions.

Item NoColourQty - Table ASupplierItem NoColourQty - Table BCustomer
A123Blue1S123A123Blue3ABC
B123Pink3S456B123Black4CDE
A123Blue4S123A123Blue5FEG
C123White3S456C123White2ABC
D123Green2S456D123Green3FEG
D123Pink1S123D123Grey1CDE

<tbody>
</tbody>


Step 1) Make both data sets into tables (Ctrl+T), label one 'TableA' and one 'TableB'
Step 2) Click 'TableB' and then click 'Data', then 'From Table/Range', then in power query click 'Close & Load', then 'Close & Load To...', 'Only Create Connection'.
Step 3) Click TableA', and then click 'Data', then 'From Table/Range', then in power query click 'Advanced Editor' and paste the below code into it. Then click ok, then 'Close & Load'.


Code:
let    Source = Excel.CurrentWorkbook(){[Name="TableA"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item No", type text}, {"Colour", type text}, {"Qty - Table A", Int64.Type}, {"Supplier", type text}}),
    #"Appended Query" = Table.Combine({#"Changed Type", TableB}),
    #"Removed Other Columns" = Table.SelectColumns(#"Appended Query",{"Item No", "Colour", "Qty - Table A", "Qty - Table B"}),
    #"Grouped Rows" = Table.Group(#"Removed Other Columns", {"Item No", "Colour"}, {{"Table A", each List.Sum([#"Qty - Table A"]), type number}, {"Table B", each List.Sum([#"Qty - Table B"]), type number}}),
    #"Replaced Value" = Table.ReplaceValue(#"Grouped Rows",null,0,Replacer.ReplaceValue,{"Table A", "Table B"}),
    #"Added Custom" = Table.AddColumn(#"Replaced Value", "Variance", each [Table A]-[Table B]),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Item No", Order.Ascending}, {"Colour", Order.Ascending}})
in
    #"Sorted Rows"


The result should be the below. Alter the headers of the final table if you want.

Item NoColourTable ATable BVariance
A123Blue58-3
B123Black04-4
B123Pink303
C123White321
D123Green23-1
D123Grey01-1
D123Pink101

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Last edited:
Upvote 0
another approach

Code:
[SIZE=1]// TableA
let
    Source = Excel.CurrentWorkbook(){[Name="TableA"]}[Content]

in
    Source[/SIZE]

Code:
[SIZE=1]// TableB
let
    Source = Excel.CurrentWorkbook(){[Name="TableB"]}[Content]
in
    Source[/SIZE]

Append Queries as New

Code:
[SIZE=1]// Append1
let
    Source = Table.Combine({TableA, TableB}),
    Replace = Table.ReplaceValue(Source,null,0,Replacer.ReplaceValue,{"Qty", "Quantity"}),
    Merge = Table.CombineColumns(Replace,{"Colour", "Colour Name"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Colour.1"),
    Group = Table.Group(Merge, {"Item No", "Colour.1"}, {{"TableA", each List.Sum([Qty]), type number}, {"TableB", each List.Sum([Quantity]), type number}}),
    Subtraction = Table.AddColumn(Group, "Subtraction", each [TableA] - [TableB], type number),
    Rename = Table.RenameColumns(Subtraction,{{"Colour.1", "Colour"}, {"Subtraction", "Variance"}}),
    Sort = Table.Sort(Rename,{{"Item No", Order.Ascending}})
in
    Sort[/SIZE]

TableATableBResult
Item NoColourQtySupplierItem NoColour NameQuantityCustomerItem NoColourTableATableBVariance
A123Blue
1​
S123A123Blue
3​
ABCA123Blue
5​
8​
-3​
B123Pink
3​
S456B123Black
4​
CDEB123Black
0​
4​
-4​
A123Blue
4​
S123A123Blue
5​
FEGB123Pink
3​
0​
3​
C123White
3​
S456C123White
2​
ABCC123White
3​
2​
1​
D123Green
2​
S456D123Green
3​
FEGD123Grey
0​
1​
-1​
D123Pink
1​
S123D123Grey
1​
CDED123Green
2​
3​
-1​
D123Pink
1​
0​
1​
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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