Column for subtraction of grand totals in pivot table - power query useful here?

heretolearnexcel

Board Regular
Joined
Jan 22, 2019
Messages
58
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a table of data that I turned into a pivot table. Here is an example of more or less the data I'm working with:
DateType of movementProduct A In
Product A OutUnit CostUnit CostClientSelling PriceProduct A in Stock
10/11/19A203100
10/3/19B102Client A$2585
9/23/19C5290
9/22/19B95Client B$2091
9/15/19A30470
9/13/19B124Client D$1835
9/10/19A75100
9/02/19C83Client A$2512
8/24/19A10913

<tbody>
</tbody>

I turned the data into a pivot table and ended up with this:

Type of movementSum of Product A inSum of product A out
B10
A20
B9
B12
A30
A7
A10
Grand Total6731

<tbody>
</tbody>

I would like for the pivot table to display in the Grand Total row the difference between Sum of Product A in and Sum of Product A out, such that the result in the example would be =67-31= 36. I tried adding a formula next to the 31 which, supposing Type of movement= A1, would be in cell C9 and the 31 would be in D9. But if I filter for Type of movement and more values are added, changing the size of the pivot table, then the formula in D9 will no longer subtract the values from the grand total since they will no longer be in the original cells.

I tried using Power Query to changing the Type of movement column so that each unique row value of the column becomes a new column, and then I would be able to work the data to get the results I want, but I can't seem to find a way to do that in Power Query and keep all the corresponding data. The result I would like to get using Power Query would be something like this.

DateType of Movement AType of Movement BType of Movement CUnit CostUnit CostClientSelling PriceProduct A in Stock
10/11/2019Product A In203100
10/3/2019Product A Out102Client A$25 85
9/23/2019Product A In5290
9/22/2019Product A Out95Client B$20 91
9/15/2019Product A In30470
9/13/2019Product A Out124Client D$18 35
9/10/2019Product A In75100
9/2/2019Product A Out83Client A$25 12
8/24/2019Product A In10913

<tbody>
</tbody>


Well... I just realized that wouldn't work for what I want. But still, is there any way to do that in Power Query? And regarding my original question, how could I achieve that with my pivot table?

Thank you in advance.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
with table or PowerQuery it is not possible : two the same name of columns, eg: Unit Cost
 
Upvote 0
is that what you want with PQ ?

DateAttributeABCUnit CostUnit Cost2ClientSelling PriceProduct A in Stock
11/10/2019​
Product A In
20​
3​
100​
03/10/2019​
Product A Out
10​
2​
Client A
25​
85​
23/09/2019​
Product A In
5​
2​
90​
22/09/2019​
Product A Out
9​
5​
Client B
20​
91​
15/09/2019​
Product A In
30​
4​
70​
13/09/2019​
Product A Out
12​
4​
Client D
18​
35​
10/09/2019​
Product A In
7​
5​
100​
02/09/2019​
Product A Out
8​
3​
Client A
25​
12​
24/08/2019​
Product A In
10​
9​
13​
 
Upvote 0
so try this

Code:
[SIZE=1]// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Type of movement", type text}, {"Product A In", Int64.Type}, {"Product A Out", Int64.Type}, {"Unit Cost", Int64.Type}, {"Unit Cost2", Int64.Type}, {"Client", type text}, {"Selling Price", Int64.Type}, {"Product A in Stock", Int64.Type}}),
    UnpivotOSC = Table.Unpivot(Type, {"Product A In", "Product A Out"}, "Attribute", "Value"),
    Pivot = Table.Pivot(UnpivotOSC, List.Distinct(UnpivotOSC[#"Type of movement"]), "Type of movement", "Value"),
    Reorder = Table.ReorderColumns(Pivot,{"Date", "Attribute", "A", "B", "C", "Unit Cost", "Unit Cost2", "Client", "Selling Price", "Product A in Stock"}),
    Sort = Table.Sort(Reorder,{{"Date", Order.Descending}})
in
    Sort[/SIZE]
 
Upvote 0
Woah, I'm sorry but I'm new to Power Query. How would I go about using those formulas or that information there? So far i've played with the options in the menu to split cells, re-arrange the order of cells, etc.
 
Upvote 0
be sure the name of source table is the same as in the M-code (here: Table1)
copy code from the post
open Advanced Editor
paste code

here is source table I used

DateType of movementProduct A InProduct A OutUnit CostUnit Cost2ClientSelling PriceProduct A in Stock
11/10/2019​
A
20​
3​
100​
03/10/2019​
B
10​
2​
Client A
25​
85​
23/09/2019​
C
5​
2​
90​
22/09/2019​
B
9​
5​
Client B
20​
91​
15/09/2019​
A
30​
4​
70​
13/09/2019​
B
12​
4​
Client D
18​
35​
10/09/2019​
A
7​
5​
100​
02/09/2019​
C
8​
3​
Client A
25​
12​
24/08/2019​
A
10​
9​
13​

there is NO $ sign in Selling Price column

dates looks different because you are in US style, me not


more about PowerQuery aka Get&Transform
 
Last edited:
Upvote 0
Does anyone have any suggestions regarding the information I want to do with my pivot table?
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,666
Members
448,977
Latest member
moonlight6

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