Calculated Field in PowerPivot - excel

I have tried researching this question, but have had issues understanding DAX.
I am using PowerPivot instead of a normal pivot table so that I can use the distinct count feature. I would like to create what in a classic pivot table would equate to a calculated field. I put together an example in the picture below.
In the value section of the pivot table, I have a distinct count of the sales of red hats (column B). To the right also in the value section, I have the count of total hat sales (all colors) (column C). I would like to add another column that displays the red hat sales as a percent of the total hat sales - The solution that I am looking for would divide the 'Distinct Count of Red Hat Sales' over the 'Count of Total Sales' using a field or measure in PowerPivot (Essentially what I have manually entered in column D).
I believe that I am doing to have to use DAX to create a measure?
I am sure that this has probably been answered before, but I have had trouble finding exactly what I am asking. Any help would be greatly appreciated.

You need to create a DAX measure with the following formula:
= DIVIDE( [Count of Red Hats], [Count of Total Hats])
This function ignores division by zero errors. If such errors happen, the function by default displays blanks, but you can choose to display something else (DIVIDE function has third optional parameter).

Related

Why is the calculated field I created not using the the formula provided to return result

These are the screenshots of me creating a calculated field, then the result I expected, and the one I got, which is not the result its supposed to give based on the formula I gave.
Screenshot of me creating the calculated field to subtract the two columns on the pivot table,
Screenshot of the result I was expecting,
Screenshot of the result I got
Like I said in my comment to your previous post on this issue, by default, Calculated Fields always apply a SUM aggregation to the values in the columns referenced prior to applying the aggregation you specify.
Effectively, this means that the AVERAGE functions you are using within the Calculated Fields are being applied to a single value only. So your Sum of Average profit column will be the result of
AVERAGE(Sum of Box Office Revenue) - AVERAGE(Sum of Budget)
where Sum of Box Office Revenue and Sum of Budget will be single values.
You can easily verify this by adding Sum of Box Office Revenue and Sum of Budget to your current Pivot Table and noting the difference between the two.
As mentioned, this is default behaviour for Calculated Fields, as non-intuitive as it may seem. To obtain what you require, you can either add your table to the Data Model and use a measure within Power Pivot, or else add an extra, 'dummy' column to your table which comprises the number 1 in every row, after which your Calculated Field formula would be:
='Box Office Revenue'/Dummy - Budget/Dummy

How to calculate average using calculated field in Excel Pivot Table?

I have a question concerning calculated fields in Excel Pivot Table.
I have attached an image for perusal.
Observed that in the attached image below, what happened was that I tried to find out using the calculated field in pivot table, the Average Profit Per Genre.
I have summarized both the Sum Of Budget and Sum Of Box Office Revenue to Average Of Budget and Average Of Box Office Revenue respectively.
However, it seems that calculated field won't allow me to minus the Average Of Budget and Average Of Box Office Revenue, so that I can get the Average Profit Per Genre.
It seems that calculated field would only calculate the Sum Of Budget and Sum Of Box Office Revenue.
Would like to ask if there are any other way around this? So that i can find out the Average Profit Per Genre?
Thanks!
Calculated Fields always sum fields, no matter what aggregation you set via the Value Field Settings dialog box.
You can either add your table to the Data Model, i.e. use Power Pivot, or else add a dummy column to your table - named 'Dummy', say - which contains the value 1 in every row. You can then use the following Calculated Field:
= ('Box Office Revenue' - Budget) / Dummy
I see the Excel Pivot Table you are trying to create is from the Hands-On Activity in the Google Professional Certificate in Data Analytics - Course 5.
I have a workaround to solve the problem, though it does not directly address the Calculated Fields issue.
STEP ONE:
As Pivot Table will not allow you to add a column, start typing in Cell D3, the heading of the column as "Average Profit".
In Cell D4, type the formula =C4-D4 (which calculates Average Box Office minus Average Budget = Average Profit). Then fill down from Cell D4 to get Average Profit for each genre.
STEP TWO:
Select the Pivot Table along with column D. Copy and go to cell F3 and click paste. Now, select the range of cells where your data is, from F3 to I20. Go to Sort & Filter and choose "Custom Sort". Make sure "My data has headers" is checked.
SORT BY - Average Profit
SORT ON - Values (leave as default, no need to change)
ORDER - Largest to smallest
You will then find all the genres arranged by Average Profit, which will help you answer the activity's requirement - "Which movie genre generates the most profit on average?"
Hope this helps. Feel free to ask for any clarifications. Thank you and keep going, guys!
P.S. I thought of adding a screenshot, but that might be considered a breach of Coursera Honor Code, as it would have the answer to the activity's requirement!

How to get percentage answer from pivot data?

Suppose I have the following fields in my Pivot Table:
agent | actual sales
Now I want to add another field which will calculate the percentage of actual sales to the total sales. How do I write the formula for calculated field?
[Edit: ] If you want to do this type of calculation in a calculated field in the Pivot Table, I'm afraid it will not work as you can only access certain variables from such a calculation. It is explicitly stated in the Office Support that "[f]ormulas cannot refer to totals" giving you a hard time to calculate your percentages.
But if you on the other hand will the calculation in a "separate column" you can do it in the sheet below, either using =GETPIVOTDATA("Sum of sales2";$E$3;"agent";E4) or =GETPIVOTDATA("Sum of sales";$E$3;"agent";E4)/GETPIVOTDATA("Sum of sales";$E$3)in the first row of the separate column.
However trivial, my original answer contain the straightforward way:
In Excel 2010 at least, you can in the Value Field Settings dialouge choose to show the values in your actual sales column as "% of Grand Total" among other calculations. Observe that this is not just a formatting, it actually calculates the values:

Why is Grand Total in Excel Pivot Table #DIV/0! (Divide by Zero) on this Calculated Field?

Here is my data:
I created a pivot table from the data and created a calculated field called "% Error" defined like this:
I set the option to "Classic Pivot Table Layout" and dragged the values so that they would be next to the items instead of across the top as shown:
The totals at the bottom for month 1, 2, and 3 are correct. The totals for each Item are correct. The grand total at the bottom shows the "% Error" with a divide by zero error. I am expecting it to be (40-30)/30=33.33%. If I ship any quantity of Item B in any month it calculates correctly. What do I have to do to get the correct value? Is this a feature or a bug?
So I can tell you what the problem is. I tried, but I cannot find a fix for it.
Basically you are looking at the final, Grand Total for Total Sum of Percent Error as still having the calculated field in it, however this is not the case.
For Month's 1, 2, and 3, you have the calculated field formula that you inserted is taking the values in Total Sum of Forecast and Total Sum of Shipped and is applying the calculation on the values in the those two cells above it to give you the Total Sum of % Error.
What happens in the Grand Total column is that it is updated by more than one formula, your calculated field formula, and the Sum of the Grand Totals for the Sum of % Error for A and the Sum of % Errors for B. If you click on the List Formulas button, at least in Excel 2016, you get a new sheet that says this.
Calculated Field
Solve Order Field Formula
1 % Error =(Forecast-Shipped)/Shipped
Calculated Item
Solve Order Item Formula
Note: When a cell is updated by more than one formula,
the value is set by the formula with the last solve order.
To change formula solve orders,
use the Solve Order command on the PivotTable command bar.
The only problem is that you do not get the option to change the Solve Order for this, so the sum of the fields in the Grand Total column override the calculation of (40-30)/30
Your option is that in the Design Tab, you can select Grand Totals -> For Columns Only, which will give you the totals at the bottom and then manually put the formulas into the Grand Total Column that is now hidden. Sorry I can't come up with a more elegant solution.
If you are really ambitious about getting this to work with Pivot Tables, many years ago I used to create them through VBA and not using the built in GUI, and you get much more control using the objects and methods than you do with the commands that are exposed to you in the user interface. Unfortunately most all of the knowledge on how to do that has been lost to time for me.

Use formula in custom calculated field in Pivot Table

In Excel Pivot table report there is possibility for user intervention by inserting "Calculated Field" so that user can further manipulate the report. This seems like best approach compared to using formula on Pivot table data, outside the Pivot table, for many obvious reasons.
"Calculated Field" dialog, looks like this:
and while it's easy to do calculation between available variables (as shown in screenshot) I can't find how to reference range of values for any of available variables.
For example, if for some reason I want to center the data in range A1:A100 I'd use = A1 - AVERAGE(A1:A100) and fill all rows in regular Excel table. But for Pivot table, if I use "Calculated Field" dialog and add new variable with formula: = 'Actual Sales' - AVERAGE('Actual Sales') I get 0 as output.
So my question is how can I reference whole range for 'Actual Sales' variable in "Calculated Field" dialog, so that AVERAGE() will return the average of all targeted cells ?
I'll post this comment as answer, as I'm confident enough that what I asked is not possible.
I) Couple of similar questions trying to do the same, without success:
Pivot Calculated formula: SUM(Field1)/AVG(Field2)
Excel Pivot Table Calculated Field
II) This article: Excel Pivot Table Calculated Field for example lists many restrictions of Calculated Field:
For calculated fields, the individual amounts in the other fields are summed, and then the calculation is performed on the total amount.
Calculated field formulas cannot refer to the pivot table totals or subtotals
Calculated field formulas cannot refer to worksheet cells by address or by name.
Sum is the only function available for a calculated field.
Calculated fields are not available in an OLAP-based pivot table.
III) There is tiny limited possibility to use AVERAGE() and similar function for a range of cells, but that applies only if Pivot table doesn't have grouped cells, which allows listing the cells as items in new group (right to "Fileds" listbox in above screenshot) and then user can calculate AVERAGE(), referencing explicitly every item (cell), from Items listbox, as argument. Maybe it's better explained here: Calculate values in a PivotTable report
For my Pivot table it wasn't applicable because my range wasn't small enough, this option to be sane choice.
Some of it is possible, specifically accessing subtotals:
"In Excel 2010+, you can right-click on the values and select Show Values As –> % of Parent Row Total." (or % of Parent Column Total)
And make sure the field in question is a number field that can be summed, it does not work for text fields for which only count is normally informative.
Source: http://datapigtechnologies.com/blog/index.php/excel-2010-pivottable-subtotals/
Thank you for planting a seed, Cel! I've been struggling with this for hours, finally got it. I was counting a text field, oops, calculation failed.
Created 2 helper columns in my raw data, each resulting in 1 if condition met, 0 if not. Then pulled each into a pivot column, mine are called, "Inbd" (for Inbound), "Back", where "Back" is a return to sending facility, so in reality the total is one trip, not 2 trips, i.e., back is a subset of inbound and not every inbd has a back (obviously). Trying to calculate in the pivot table so I can sort on the field the rate of back to inbound for each sending facility.
For my calculated field I used: =IFERROR(IF(Pvt_Back>0,Pvt_Back/Pvt_Inbd,0),0)
So: if we sent back to sending some number of times greater than 0, divide Back/Inbd to give me a rate; if equal to 0, then 0; if Inbd = 0, then 0 to avoid Div/0 error.
Thanks again!! :)
Pivot table Excel2007- average to exclude zeros
=sum(XX:XX)/count if(XX:XX, ">0")
Invoice USD
Qty Rate(count) Value (sum)
300 0.000 000.000
1000 0.385 385.000
Average Rate Count should Exclude 0.000 rate

Resources