Custom Formula for Grand Total column - excel

I have a frequent problem where the formula I want to use in the Values area in my Pivot-Table is different than the formula I want to use for the Grand Total column of that row. I typically want to Sum the Values but I want to average the Sums. Here is what I normally would get if I pivoted the dates on the Column Labels, Meat Type on the Row Labels, and Sum Orders in the Values.
Row Lables | Day 1 | Day 2 | Day 3 | Grand Total
________________________________________________
Beef | 100 | 105 | 102 | 307
Chicken | 200 | 201 | 202 | 603
I get sums by day and a sum of all of the days in the Grand Total column. Here is what I want to have:
Row Lables | Day 1 | Day 2 | Day 3 | Grand Total (Avg of Day Totals)
________________________________________________
Beef | 100 | 105 | 102 | 102.3
Chicken | 200 | 201 | 202 | 201.0
In this case the Orders are still summed by day but the Grand Total is now an average of the sums. What I do now is copy and paste the Pivot data onto a seperate sheet then calculate the averages. If there was a way to do this with a custom Grand Total column it would be incredible. This is one of the biggest shortcomings of Pivot Tables for me but I'm hoping it is due to my ignorance, which it often is. Thanks for the help!

You can write a measure that checks the number of 'rows' in a particular filter context and nest that in an IF() to determine which to use.
If using PowerPivot V2 then it's:
=IF(HASONEVALUE(Calendar[Day]), SUM(AMOUNT), AVERAGE(AMOUNT))
If using PowerPivot V1 it's:
=IF(COUNTROWS(Calendar[Day])=1, SUM(AMOUNT), AVERAGE(AMOUNT))
Both do the same thing in that they assess the number of rows in the table in the given context and when the Meat Type is 'Beef' then the temporarily filtered table has one row. If it doesn't have one row then its going down the path of the AVERAGE()
This assumes your column headers 'Days' are in a table called Calendar (if you aren't using a separate Calendar table then you are missing the most powerful functionality of PowerPivot IMO).
Jacob

I can't think of a "good" way, but here's one option. Add the Amount field to the data area a second time and change the operation to Average. Then use conditional formatting to hide the averages in the data area and hide the sums in the total area.
You might be better off just using some array formulas in a do-it-yourelf pivot table. You lose the pivot table benefits, but get more flexibility with the data.

Related

Calculate weighted mean with different types of rows

I want to track the weighted mean price per coin of my crypto currencies in excel.
But I don't know what would be the best approach for my table which looks like this:
| Type | Total Price | Price Per Coin | Coins |
|------|-------------|----------------|-----------|
| Buy | 250 | 7.80 | 32,051282 |
| Buy | 300 | 7.42 | 40,431266 |
| Sell | 270 | 7.61 | 35,479632 |
To calculate the weighted mean one would use the formula:
SUMPRODUCT([Price Per Coin], [Coins])/SUM([Coins])
But in my case the table also holds Sell rows which that I don't want in the calculation of the weighted mean for the Buy price of my coins. How can I exclude them from the formula?
I tried something like:
SUMPRODUCT([Price Per Coin], [Coins])/SUMIF([Type], "==Buy", [Coins])
But this doesn't some to be right (I got a result way over 15 bucks per coin which doesn't make sense...).
How could I furthermore calculate how much I've made/lost with my transactions?
If you have O365:
=LET(x,FILTER(coinTbl,coinTbl[Type]="Buy"),SUMPRODUCT(INDEX(x,0,3),INDEX(x,0,4))/SUM(INDEX(x,0,4)))
=> 7.588
If you don't have O365, this should work:
=SUMPRODUCT(coinTbl[Price Per Coin]*(coinTbl[Type]="Buy"),coinTbl[Coins]*(coinTbl[Type]="Buy"))/SUMIF(coinTbl[Type],"Buy",coinTbl[Coins])
Your second question probably deserves to be separate and include a discussion of what you mean and how you want to calculate your profit/loss Recognized, unrecognized, FIFO, average cost, or some other method.
And also why you are, apparently, using different decimal indicators in your price and coins columns.
A pivot table would let you filter away SELL rows and instantly calculate the sum of total_price, sum of coins
To get proper weighted average you can either refer to the pivot table totals or follow this guide (i just googled for excel weighted average pivot table)
https://www.extendoffice.com/documents/excel/4816-excel-pivot-table-weighted-average.html
Although I am curious because using the data you provided it looks like remaining balance of coins is only 37 not 72
Seems to me you need to at least add more columns to store Coin_balance to track remaining unsold coins for each Buy row

Dynamic sum column in SUMIFS

I have month columns and criteria for rows (Division, measure) and need a way to sum all divisions for a given measure and only return for the reporting month.
INDEX(MATCH) does not work because I need it to sum all Dec-18 absence values, but there are other measures in the columns as well.
My current iteration (array):
=SUM(OFFSET(D1:D53,,MATCH(Y3,$D$2:$P$2,0)))
But I can't get this to change the summing column based on the month selected.
My last guess is that I need to swap division and month (so division column headers, month rows), but I'd rather not if I'm missing something obvious.
Example:
Department | Measure | Nov-18 | Dec-18
Sales | Absence Hours | 3.5 | 4.6
Manu | Absence Hours | 6.2 | 1.7
Sales | Hours worked | 1000 | 976
An alternative solution that does not use array like calculation nor volatile functions.
=SUMIF($B$2:$B$4,$F2,INDEX($C$2:$D$4,0,MATCH(G$1,$C$1:$D$1,0)))
In the event that either your criteria or date is not found an error will be displayed. You can deal with this by wrapping the whole thing in an IFERROR function and then choose your own error message, display blank or return 0.
=IFERROR(SUMIF($B$2:$B$4,$F2,INDEX($C$2:$D$4,0,MATCH(G$1,$C$1:$D$1,0))),"NOT FOUND")
Also, if you wish to further breakdown your results but "department" and by "measure", then you could us SUMIFS which allows you to set multiple criteria instead of 1
Maybe something like this would work?
=SUMPRODUCT((B3:B5=G3)*OFFSET(B3:B5;0;MATCH(G2;C2:D2;0)))

Row number and partition in Excel

I have data in excel such as:
ID | Fee
123456789 | 100
987654321 | 100
987654321 | 75
987654321 | 50
I need to calculate a fee reduction for the items that are not the max price. The spreadsheet is sorted by ID, then Fee in the fashion needed. What I do not know how to do is use a similar row_number() over(partition by) in excel that I would normally do in SQL
Desired output would be
ID | Fee | rn
123456789 | 100 | 1
987654321 | 100 | 1
987654321 | 75 | 2
987654321 | 50 | 3
This formula will do the job:
=COUNTIF($A$2:INDIRECT("A"&ROW(A2)),A2)
There is no need for sorting the data and you won't fall out of the range.
ROW() is used to make the range dynamic, so if we drag the formula down, ROW() will always give us ending point:
There's probably a more complex formula one could just throw at the data without having to monkey with the data, but I think this may be an easier solution:
Sort the data by ID (smallest to largest) and Fee (Smallest to largest)
Use formula =Countif(A2:A5, A2) to count how many times the same id appears in the data for the current cell and every cell below it. Copying this down to fill out the missing column.
you can use =COUNTIF($A$2:A2,A2); note that only the first $A$2 will not move.
Arrange everything in column A (in any order).
In B1 type this : =IF(A1=A2, (B2+1),1), extent this over the entire column B.

Add entire calender year to pivotTable in Excel from sample with only few dated entries

Hopefully somebody can help me figure out a better way to create what I need in a pivotTable. I have a table structured as follows:
Date | ID# | Revenue
---------------------------------------
1/1/14 | 123 | $200.00
1/1/14 | 234 | $99.00
1/5/14 | 455 | $100.00
1/31/14 | 5666 | $50.00
2/4/14 | 2454 | $500.00
...
...
...
12/4/14 | 88484 | $3000.00
Unfortunately, when I create a pivotTable off of this information, and then try to process the data all the way into chart data, it gives me only dated entries when there is an actual entry.
What I need is to not only create a table that shows dates that had $0.00 revenue, but also a chart that displays all 365 days, without manually going back day by day (as there are 1000+ rows) to enter a day that has a $0.00 revenue.
Any ideas?
One approach would be to put the days of the year into a spare column, say D, by starting with 1/1/14 in D2, pulling down with the right mouse button and choosing 'Fill Days'.
Then put a formula into column E starting in E2 like
=SUMIF(A:A,D2,C:C)
to put in the total revenue for each day in column C based on all matching days in column A.
that's assuming that what you want is the total revenue for each day of the year whether or not it's zero.

Executing a function over a grouped set

I have two columns of data in Excel, one containing dates and the other values 1 or 2 which represent a specific status (like an enum).
Here is an example data set:
2014/07/04 | 1
2014/07/04 | 1
2014/07/04 | 2
2014/07/04 | 1
2014/07/05 | 2
2014/07/06 | 1
2014/07/06 | 1
2014/07/06 | 2
I need to get a graph of the percentages of the number 1 over days; in the above example
July 4th: 75%
July 5th: 100%
July 6th: 66%.
I've tried pivot tables and charts with no luck because I can't write my own function for values (COUNTIF for ones divided by COUNT), only use the predefined ones which aren't of any use.
Does anyone know how I would go about doing this?
You could do this with a pivot chart.
I made up my own data so it doesn't quite match but it is the same idea.
For the pivot table fields I used
Legend Fields:Compare
Axis Fields:Date
Values: Count of compare
For values under Value field settings goto show value as and change this to % of column total and summarize value by Count.
If you only want to see number one just put a filter on the column labels.

Resources