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

heretolearnexcel

Board Regular
Joined
Jan 22, 2019
Messages
61
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:
[TABLE="width: 1000"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Type of movement[/TD]
[TD]Product A In
[/TD]
[TD]Product A Out[/TD]
[TD]Unit Cost[/TD]
[TD]Unit Cost[/TD]
[TD]Client[/TD]
[TD]Selling Price[/TD]
[TD]Product A in Stock[/TD]
[/TR]
[TR]
[TD]10/11/19[/TD]
[TD]A[/TD]
[TD]20[/TD]
[TD][/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]10/3/19[/TD]
[TD]B[/TD]
[TD][/TD]
[TD]10[/TD]
[TD][/TD]
[TD]2[/TD]
[TD]Client A[/TD]
[TD]$25[/TD]
[TD]85[/TD]
[/TR]
[TR]
[TD]9/23/19[/TD]
[TD]C[/TD]
[TD]5[/TD]
[TD][/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]9/22/19[/TD]
[TD]B[/TD]
[TD][/TD]
[TD]9[/TD]
[TD][/TD]
[TD]5[/TD]
[TD]Client B[/TD]
[TD]$20[/TD]
[TD]91[/TD]
[/TR]
[TR]
[TD]9/15/19[/TD]
[TD]A[/TD]
[TD]30[/TD]
[TD][/TD]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]70[/TD]
[/TR]
[TR]
[TD]9/13/19[/TD]
[TD]B[/TD]
[TD][/TD]
[TD]12[/TD]
[TD][/TD]
[TD]4[/TD]
[TD]Client D[/TD]
[TD]$18[/TD]
[TD]35[/TD]
[/TR]
[TR]
[TD]9/10/19[/TD]
[TD]A[/TD]
[TD]7[/TD]
[TD][/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]9/02/19[/TD]
[TD]C[/TD]
[TD][/TD]
[TD]8[/TD]
[TD][/TD]
[TD]3[/TD]
[TD]Client A[/TD]
[TD]$25[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]8/24/19[/TD]
[TD]A[/TD]
[TD]10[/TD]
[TD][/TD]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]13[/TD]
[/TR]
</tbody>[/TABLE]

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

[TABLE="width: 500"]
<tbody>[TR]
[TD]Type of movement[/TD]
[TD]Sum of Product A in[/TD]
[TD]Sum of product A out[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD][/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]20[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD][/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD][/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]30[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Grand Total[/TD]
[TD]67[/TD]
[TD]31[/TD]
[/TR]
</tbody>[/TABLE]

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.

[TABLE="width: 823"]
<tbody>[TR]
[TD]Date[/TD]
[TD][/TD]
[TD]Type of Movement A[/TD]
[TD]Type of Movement B[/TD]
[TD]Type of Movement C[/TD]
[TD]Unit Cost[/TD]
[TD]Unit Cost[/TD]
[TD]Client[/TD]
[TD]Selling Price[/TD]
[TD]Product A in Stock[/TD]
[/TR]
[TR]
[TD]10/11/2019[/TD]
[TD]Product A In[/TD]
[TD]20[/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]10/3/2019[/TD]
[TD]Product A Out[/TD]
[TD][/TD]
[TD]10[/TD]
[TD][/TD]
[TD][/TD]
[TD]2[/TD]
[TD]Client A[/TD]
[TD]$25 [/TD]
[TD]85[/TD]
[/TR]
[TR]
[TD]9/23/2019[/TD]
[TD]Product A In[/TD]
[TD][/TD]
[TD][/TD]
[TD]5[/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]9/22/2019[/TD]
[TD]Product A Out[/TD]
[TD][/TD]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[TD]5[/TD]
[TD]Client B[/TD]
[TD]$20 [/TD]
[TD]91[/TD]
[/TR]
[TR]
[TD]9/15/2019[/TD]
[TD]Product A In[/TD]
[TD]30[/TD]
[TD][/TD]
[TD][/TD]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]70[/TD]
[/TR]
[TR]
[TD]9/13/2019[/TD]
[TD]Product A Out[/TD]
[TD][/TD]
[TD]12[/TD]
[TD][/TD]
[TD][/TD]
[TD]4[/TD]
[TD]Client D[/TD]
[TD]$18 [/TD]
[TD]35[/TD]
[/TR]
[TR]
[TD]9/10/2019[/TD]
[TD]Product A In[/TD]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]9/2/2019[/TD]
[TD]Product A Out[/TD]
[TD][/TD]
[TD][/TD]
[TD]8[/TD]
[TD][/TD]
[TD]3[/TD]
[TD]Client A[/TD]
[TD]$25 [/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]8/24/2019[/TD]
[TD]Product A In[/TD]
[TD]10[/TD]
[TD][/TD]
[TD][/TD]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]13[/TD]
[/TR]
</tbody>[/TABLE]


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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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 ?

[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#70AD47]Date[/td][td=bgcolor:#70AD47]Attribute[/td][td=bgcolor:#70AD47]A[/td][td=bgcolor:#70AD47]B[/td][td=bgcolor:#70AD47]C[/td][td=bgcolor:#70AD47]Unit Cost[/td][td=bgcolor:#70AD47]Unit Cost2[/td][td=bgcolor:#70AD47]Client[/td][td=bgcolor:#70AD47]Selling Price[/td][td=bgcolor:#70AD47]Product A in Stock[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E2EFDA]
11/10/2019​
[/td][td=bgcolor:#E2EFDA]Product A In[/td][td=bgcolor:#E2EFDA]
20​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
3​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
100​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
03/10/2019​
[/td][td]Product A Out[/td][td][/td][td]
10​
[/td][td][/td][td][/td][td]
2​
[/td][td]Client A[/td][td]
25​
[/td][td]
85​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E2EFDA]
23/09/2019​
[/td][td=bgcolor:#E2EFDA]Product A In[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
5​
[/td][td=bgcolor:#E2EFDA]
2​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
90​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
22/09/2019​
[/td][td]Product A Out[/td][td][/td][td]
9​
[/td][td][/td][td][/td][td]
5​
[/td][td]Client B[/td][td]
20​
[/td][td]
91​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E2EFDA]
15/09/2019​
[/td][td=bgcolor:#E2EFDA]Product A In[/td][td=bgcolor:#E2EFDA]
30​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
4​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
70​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
13/09/2019​
[/td][td]Product A Out[/td][td][/td][td]
12​
[/td][td][/td][td][/td][td]
4​
[/td][td]Client D[/td][td]
18​
[/td][td]
35​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E2EFDA]
10/09/2019​
[/td][td=bgcolor:#E2EFDA]Product A In[/td][td=bgcolor:#E2EFDA]
7​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
5​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
100​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
02/09/2019​
[/td][td]Product A Out[/td][td][/td][td][/td][td]
8​
[/td][td][/td][td]
3​
[/td][td]Client A[/td][td]
25​
[/td][td]
12​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E2EFDA]
24/08/2019​
[/td][td=bgcolor:#E2EFDA]Product A In[/td][td=bgcolor:#E2EFDA]
10​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
9​
[/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA][/td][td=bgcolor:#E2EFDA]
13​
[/td][/tr]
[/table]
 
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

[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#5B9BD5]Date[/td][td=bgcolor:#5B9BD5]Type of movement[/td][td=bgcolor:#5B9BD5]Product A In[/td][td=bgcolor:#5B9BD5]Product A Out[/td][td=bgcolor:#5B9BD5]Unit Cost[/td][td=bgcolor:#5B9BD5]Unit Cost2[/td][td=bgcolor:#5B9BD5]Client[/td][td=bgcolor:#5B9BD5]Selling Price[/td][td=bgcolor:#5B9BD5]Product A in Stock[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]
11/10/2019​
[/td][td=bgcolor:#DDEBF7]A[/td][td=bgcolor:#DDEBF7]
20​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
3​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
100​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
03/10/2019​
[/td][td]B[/td][td][/td][td]
10​
[/td][td][/td][td]
2​
[/td][td]Client A[/td][td]
25​
[/td][td]
85​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]
23/09/2019​
[/td][td=bgcolor:#DDEBF7]C[/td][td=bgcolor:#DDEBF7]
5​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
2​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
90​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
22/09/2019​
[/td][td]B[/td][td][/td][td]
9​
[/td][td][/td][td]
5​
[/td][td]Client B[/td][td]
20​
[/td][td]
91​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]
15/09/2019​
[/td][td=bgcolor:#DDEBF7]A[/td][td=bgcolor:#DDEBF7]
30​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
4​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
70​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
13/09/2019​
[/td][td]B[/td][td][/td][td]
12​
[/td][td][/td][td]
4​
[/td][td]Client D[/td][td]
18​
[/td][td]
35​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]
10/09/2019​
[/td][td=bgcolor:#DDEBF7]A[/td][td=bgcolor:#DDEBF7]
7​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
5​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
100​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
02/09/2019​
[/td][td]C[/td][td][/td][td]
8​
[/td][td][/td][td]
3​
[/td][td]Client A[/td][td]
25​
[/td][td]
12​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]
24/08/2019​
[/td][td=bgcolor:#DDEBF7]A[/td][td=bgcolor:#DDEBF7]
10​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
9​
[/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7][/td][td=bgcolor:#DDEBF7]
13​
[/td][/tr]
[/table]


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

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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