Calculated field in Pivot Table

niam07

New Member
Joined
Mar 13, 2014
Messages
11
Hi ya,

I have been searching all through the forum but cannot get my head around Pivot Table calculated field, will be very appreciate of any help. I have raw data as following. I buy stock from different suppliers. Every item has a code. If I buy same stock from different suppliers, my code remains same. My category determines whether I am buying or selling stock.


Suppliers or clientCode
CategoryItemQuantity
Supplier 11001PurchaseSofa15
Supplier 11002PurchaseBed30
Client 11001SellSofa1
Supplier 21001PurchaseSofa5
Client 21001SellSofa3
Client 31002SellBed1

<tbody>
</tbody>

I need Pivot table summary as following where it gives me the balance of my remaining stock.

Code
Sum of Purchase QuantityStock Remaining
Sofa10012016
Bed10023029

<tbody>
</tbody>

Thanks a lot.

Naim
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You can try with PowerQuery aka Get&Transform:

Suppliers or clientCodeCategoryItemQuantityItemCodePurchaseOnStock
Supplier 1
1001​
PurchaseSofa
15​
Sofa
1001​
20​
16​
Supplier 1
1002​
PurchaseBed
30​
Bed
1002​
30​
29​
Client 1
1001​
SellSofa
1​
Supplier 2
1001​
PurchaseSofa
5​
Client 2
1001​
SellSofa
3​
Client 3
1002​
SellBed
1​

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Pivoted Column" = Table.Pivot(Source, List.Distinct(Source[Category]), "Category", "Quantity"),
    #"Grouped Rows" = Table.Group(#"Pivoted Column", {"Code"}, {{"Count", each _, type table}, {"Purchase", each List.Sum([Purchase]), type number}, {"Sell", each List.Sum([Sell]), type number}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "OnStock", each [Purchase]-[Sell]),
    #"Added Custom" = Table.AddColumn(#"Added Custom1", "Item", each List.Distinct(Table.Column([Count],"Item"))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Item", each Text.Combine(List.Transform(_, Text.From)), type text}),
    #"Reordered Columns" = Table.ReorderColumns(#"Extracted Values",{"Item", "Code", "Count", "Purchase", "Sell"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Count", "Sell"})
in
    #"Removed Columns"[/SIZE]
 
Upvote 0
Hi Sandy,

Thanks a lot. Sorry for being dummy but where do I enter the code that you have provided. I am using office 365 and Googled but cannot find where to input this code. if you could provide me bit detailed info on how to input this code please.

Thanks,
Niam
 
Upvote 0
Hi Sandy,

Thanks a lot. Sorry for being dummy but where do I enter the code that you have provided. I am using office 365 and Googled but cannot find where to input this code. if you could provide me bit detailed info on how to input this code please.

Thanks,
Niam
 
Upvote 0
- Create Table from your source table (Ctrl+T) Make sure the name of this table is Table1 (Formulas - Name Manager)
- Click inside the table
- Data - From table

you will see PowerQuery editor

go to Home - Advanced Editor

- delete all what you see there then paste code from the post
- Close & Load - you should see new sheet with result table
-
here is example file
 
Last edited:
Upvote 0
Another option, which AFAIK works for any Excel version with pivot tables. So since the mid 90s?

Give the source data a simple (non-dynamic) defined name. say YourTableName then save the file. From a new file (CTRL-N), start the pivot table wizard (ALT-D-P) and choose external data source, next, get data, Excel files, OK, browse for the file, OK, see the defined name, select a field or more, then follow wizard to the end choosing the option to edit in MS Query at the last step. Via the SQL button change the text to
Code:
SELECT Item, Code, IIF(Category='Purchase',1,0)*Quantity AS [Purchase Quantity], IIF(Category='Purchase',1,-1)*Quantity AS [Stock Remaining]
FROM YourTableName
OK to enter, OK to acknowledge message about not being able to graphically represent, then via the open door icon exit MS Query & make the pivot table. Once created, the worksheet containing the pivot table can be moved into the source workbook if you like.

If you want other fields in the pivot table, then add them in the above definition.

If you want the SUM directly in the dataset without the details, then use
Code:
SELECT Item, Code, SUM(IIF(Category='Purchase',1,0)*Quantity) AS [Purchase Quantity], SUM(IIF(Category='Purchase',1,-1)*Quantity) AS [Stock Remaining]
FROM YourTableName
GROUP BY Item, Code

And, instead of a pivot table this could be just a query.
 
Last edited:
Upvote 0
Hi Sandy,

Thanks for this. I found it easy to download your file and make changes there. It worked for some values but for some reason as my raw data increased, i.e. as my codes increased in my table after 1006, the quantity part is not showing.

I have posted my raw data and desired query as follows. Will really appreciate your input into this. For some reason, output does not does not add up all quantities. Thanks a lot.

Raw Data

CategoryItemCodeSuppliers or clientQuantity
PurchaseSofas1001Supplier 160
PurchaseTea Table1003Supplier 23
SalesSofas1001Supplier 12
PurchaseMattress1004Supplier 32
PurchaseMattress1004Supplier 42
PurchaseBed1002Supplier 21
PurchaseBed1002Supplier 21
SalesBed1002Supplier 21
PurchaseSofas1001Supplier 11
SalesSofas1001Supplier 11
PurchaseSofas1001Supplier 14
PurchaseDinning Table1005Supplier 55
SalesDinning Table1005Supplier 52
PurchaseDivan1006Supplier 42
PurchaseSofas1001Supplier 64
PurchaseOffice Chair1007Supplier 65
SalesOffice Chair1007Supplier 65

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>

Required Output


ItemCodePurchaseSellOnStock
Bed1002
Dinning Table10055
Divan10062
Mattress10044
Office Chair10075
Sofas1001
Tea Table10033

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>







<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
With your new example:

CategoryItemCodeSuppliers or clientQuantityItemCodePurchaseOnStock
PurchaseSofas
1001​
Supplier 1
60​
Bed
1002​
2​
1​
PurchaseTea Table
1003​
Supplier 2
3​
Dinning Table
1005​
5​
3​
SalesSofas
1001​
Supplier 1
2​
Divan
1006​
2​
PurchaseMattress
1004​
Supplier 3
2​
Mattress
1004​
4​
PurchaseMattress
1004​
Supplier 4
2​
Office Chair
1007​
5​
0​
PurchaseBed
1002​
Supplier 2
1​
Sofas
1001​
69​
66​
PurchaseBed
1002​
Supplier 2
1​
Tea Table
1003​
3​
SalesBed
1002​
Supplier 2
1​
PurchaseSofas
1001​
Supplier 1
1​
SalesSofas
1001​
Supplier 1
1​
PurchaseSofas
1001​
Supplier 1
4​
PurchaseDinning Table
1005​
Supplier 5
5​
SalesDinning Table
1005​
Supplier 5
2​
PurchaseDivan
1006​
Supplier 4
2​
PurchaseSofas
1001​
Supplier 6
4​
PurchaseOffice Chair
1007​
Supplier 6
5​
SalesOffice Chair
1007​
Supplier 6
5​

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Pivoted Column" = Table.Pivot(Source, List.Distinct(Source[Category]), "Category", "Quantity", List.Sum),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Suppliers or client"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Code"}, {{"Count", each _, type table}, {"Purchase", each List.Sum([Purchase]), type number}, {"Sales", each List.Sum([Sales]), type number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "OnStock", each [Purchase]-[Sales]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Sales"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Columns1", "Item", each List.Distinct(Table.Column([Count],"Item"))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom1", {"Item", each Text.Combine(List.Transform(_, Text.From)), type text}),
    #"Removed Columns2" = Table.RemoveColumns(#"Extracted Values",{"Count"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns2",{"Item", "Code", "Purchase", "OnStock"})
in
    #"Reordered Columns"[/SIZE]

I think you can try to learn step by step how it works
Microsoft Power Query for Excel Help

and here is example again: example file
 
Upvote 0
Brilliant, it works. Thanks a lot Sandy. Highly appreciated your help. Just on the output, I also wanted selling column. And the on stock, I wanted it to show "purchase - sell" , and if there is no sell, just the purchase amount. Thanks again.
 
Last edited:
Upvote 0
in PQ editor - Applied steps - delete step: Remove Column1 then reorder columns as you wish and Close&Load


ItemCodePurchaseSalesOnStock
Bed
1002​
2​
1​
1​
Dinning Table
1005​
5​
2​
3​
Divan
1006​
2​
Mattress
1004​
4​
Office Chair
1007​
5​
5​
0​
Sofas
1001​
69​
3​
66​
Tea Table
1003​
3​

check example file from post#8
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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