Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

google sheets - How to add a calculated column to pivot table

I try to include a column that is calculated on a basis of a % difference between values of 2 columns in a pivot table. I tried calculated field but could not figure out how to describe these columns. These are year values based on one column in dataset so I could not use just column name.

I've prepared a playground with limited data and desired result that I can make using standard formulas. But I need it to be a part of Pivot Table as this is much bigger project and includes expanding rows and changing dimensions.

Here is how it looks like (K is a column that calculates % difference between G and H columns) and I need to include this one into Pivot.

enter image description here

My file is here: https://docs.google.com/spreadsheets/d/1yTDExBSvRXXO3P71CH09oerFp2Is7UXqzeuwNlE5XbM/edit#gid=0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try:

={{QUERY({A3:C}, "select Col1,sum(Col2) where Col1 is not null group by Col1 pivot Col3", 1), 
 QUERY(QUERY(QUERY({A3:C}, "select sum(Col2) where Col1 is not null group by Col1 pivot Col3", 1), 
 "select (Col2-Col1)/Col1 label (Col2-Col1)/Col1'Calculated column' format (Col2-Col1)/Col1 '0.00%'"))};
 {"Grand Total", INDEX(QUERY({A3:C}, "select sum(Col2) where Col1 is not null pivot Col3", 1), 2), ""}}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...