Pivot Table considers duplicate elements for the average calculation - excel

I have a pivot table (with Data Model) based on below table
Id | Time
1 | 10
1 | 10
1 | 10
2 | 2
3 | 5
3 | 5
4 | 4
5 | 8
I need to calculate the average of time.
Average based on the pivot table calculation is 6.75 --> (10*3+2+5*2+4+8)/8
However, my expected result is 5.8 --> (10+2+5+4+8)/5
How can I setup the pivot table to not take into consideration the duplicate ?
Please note that I can't remove duplicate rows.
I tried to use AVERAGEX : =AVERAGEX(VALUES('Range'[Id]);'Range'[Time]) with DAX.
But I'm facing this issue: This formula is invalid or incomplete: 'Calculation error in measure 'Range'[475e7fe7-92b4-478c-bd5f-6e7c95df27d7]: A single value for column 'Time' in table 'Range' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.'.
Thank you in advance !
Solution
=AVERAGEX(VALUES('Range'[Id]);CALCULATE(AVERAGE('Range'[Time])))

Solution
=AVERAGEX(VALUES('Range'[Id]);CALCULATE(AVERAGE('Range'[Time])))

Related

DAX for MAX of a group

I have two data columns:
Record | Record Version
------------------------
1 | 1
1 | 2
1 | 3
2 | 1
2 | 2
2 | 3
2 | 4
3 | 1
3 | 2
3 | 3
4 | 1
4 | 2
4 | 3
4 | 4
4 | 5
4 | 6
5 | 1
6 | 1
Is it possible to create a calculated column within a MVS-2012 model or PowerPivot that will give the MAX of the Record Version for a corresponding Record? For example the MAX value of Record 1 is 3, MAX value of Record 2 is 4 etc.
Yes. Add that table to your Power Pivot model, either by pasting the data into Excel and then clicking Add to Data Model on the Power Pivot ribbon, or by opening the Power Pivot Window and adding it form it's original source there.
In the Power Pivot window, go to the table where this data is located.
Select the Record Version column, then click the drop-down next to AutoSum in the Calculations group on the Home tab and choose Max.
A calculated column will appear at the bottom. YOu can rename it to something like Max Record Version.
Make a PivotTable in Excel (click the PivotTable button on the Home tab).
Add the Record field to rows and the Max Record Version to values.
If you actually want a calculation that will give you the max for the group of record you could do something like:
=SUMX(VALUES(Table1[Record])
,CALCULATE(MAX(Table1[Record Version]))
)
Generally, as #mmarie suggests, you are better dealing with this kind of problem with a measure rather than a calculated column. That said, sometimes you just need that extra column (usually to use as a dimension).
Assuming you have a table called recordsFact, this gives the MAX of that record:
= CALCULATE(MAX(recordsFact[Record Version]),
FILTER(recordsFact, recordsFact[Record] = EARLIER(recordsFact[Record])
))

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.

Percentage of Sum of two Pivot cells

I'm trying to work out a small problem with my excel Pivot table. I have data from a Excel Sheet which i have made a Pivot table of. The data is structured as below
Name | Count Cell1 | Sum of Cell 2 |
Eric | 25 | 5 |
Sam | 5 | 1 |
Joe | 10 | 5 |
What i want to have is a formula that takes the Count of Cell 1 and divide it by Sum of Cell 2 and display it in % like the example below.
Name | Count Cell1 | Sum of Cell 2 | Difference|
------------------------------------------------
Eric | 25 | 5 | 20% |
Sam | 5 | 1 | 20% |
Joe | 10 | 5 | 50% |
All formulas i have tried only uses the original Table cells and not the sums of them.
So is there a smart way to have a formula lookup inside of a pivot table and display it in %?
In your Pivot Table, you can enter a calculated field to do what you want.
Select somewhere in your pivot table (e.g. one of the Sum of Cell2 fields)
In the PivotTable Tools > Options ribbon, in the Calculations section, click Fields, Items & Sets and from there pick Calculated Field
Change the name to Difference and the Formula =Cell2/Cell1
In the Field Settings for that field, change the Custom Name to Difference and Number Format to Percentage
EDIT - question updated for Count & Sum
So, as far as I can see, trying to do the combination of Sum/Count really upsets it... the only workaround I could find was adding a helper column in the data source with just the number 1... in that way, the sum of that gives you the count, and so the Calculated Field can be Cell2/HelperColumn -horrible!

Pivot table to return the FIRST value in a range

Glad to be joining the forum.
My question deals with attempting to return the FIRST value that occurs over several columns of data, using a pivot table that is filtered within a narrow time range. My current pivot table works by counting values in each column over the time rows. However I'm really only interested in the FIRST value that I come across for each person. So the raw looks something like this:
Person|TimeValue|Variable1|Variable2
1 | 1 | 1 | 0
1 | 2 | 1 | 0
2 | 1 | 1 | 0
2 | 2 | 0 | 1
What I currently get for a pivot using a range of time1 to time 2 is
1 | |2 | 0
2 | |1 | 1
Clearly, the time range I select includes MULTIPLE values in the same column, leading to counts of >1. What I'm thinking is that there is a way to use the same time sorting, but count only the FIRST time a value occurs in that variable, so that the pivot reports only the first time a value occurs within the range for the variables of interest.
Is there a simple way, or am I going to have to do this in VBA?
Much appreciated for any and all help. This is my first more complicated attempt with the newer pivots.
This is probably not the problem you would want to solve using a pivot table. You could just use the VLOOKUP Excel function to solve this issue in a simple way. VLOOKUP will always return the first value in the lookup range that matches the lookup value.

Custom Formula for Grand Total column

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.

Resources