I have the following data on excel:
day | week | #pieces
1 | 1 | 5
2 | 1 | 5
3 | 1 | 5
4 | 1 | 5
5 | 1 | 5
1 | 2 | 5
2 | 2 | 5
3 | 2 | 5
4 | 2 | 5
5 | 2 | 5
1 | 3 | 5
2 | 3 | 5
I did a pivot table that gets the total of #pieces per week.
Now I want to get the average of #pieces per week. I tried to use a calculated field using #pieces/5 but this is not always true for the current week. See week 3 for example:
Week | Average #pieces
1 | 5
2 | 5
3 | 2 (this number '2' should be also '5')
Does anyone know how to do that?
Thanks
As far as I understand what you're trying to do, you don't need a calculated field (which is messing you up, by the way, because you're dividing by the hard-coded 5).
I would pivot with week number as your row label and average of pieces as your values. You can change sum to average in the value field options of the pivot table.
Related
Let's say I have the following table with money spent per day (timestamp)
timestamp| spent
0 | 0
1 | 0
2 | 1
3 | 4
4 | 0
5 | 0
6 | 1
7 | 3
The result I'm looking for is a table adding columns for the cummulative money spent in the last "n" days, for example the last 2 days and the last 5 days. Resulting in something like this.
timestamp | spent | spent-2d |spent-5d | ....
0 | 0 | null | null | ...
1 | 0 | 0 | null | ...
2 | 1 | 1 | null | ...
3 | 4 | 5 | null | ...
4 | 0 | 4 | 5 | ...
5 | 0 | 0 | 5 | ...
6 | 1 | 1 | 6 | ...
7 | 3 | 4 | 8 | ....
One possible solution is to add lagged columns and then sum but for say, 180 days I would need to add 180 columns and I want to to this process with not just one but several columns in the dataframe. For example for 100-500 columns I want the lagged sum over 1,2,5,7,15,30,90 and 180 days. So adding 180*500 columns seems to be a bad idea.
Any other ideas to make this in a very efficient way?
Window "rangeBetween" method can be used, example for 5 days column:
val lastFiveDaysWindow = Window
.orderBy("timestamp")
.rangeBetween(Window.currentRow - 4, Window.currentRow)
df
.withColumn("spent-5d",
when(
$"timestamp" >= 4,
sum("spent").over(lastFiveDaysWindow)
)
)
Note: Only for small Dataframes, warning exists:
No Partition Defined for Window operation! Moving all data to a single partition, this can cause serious performance degradation.
For bigger DataFrames, inner join can be used, like in answer here:
Count of all element less than the value in a row
Let's say I have the following table:
CORP | COAST | CITY | DONE | MISSING | TOTAL
-------------------------------------
New | West | LAX | 2 | 4 | 6
Old | West | SFO | 3 | 3 | 6
New | East | NYC | 4 | 2 | 6
I make a pivot table that looks like this:
CORP:
COAST: NEW | OLD
-------------------------------------
EAST |
SUM OF TOTAL | 6 |
SUM OF DONE | 4 |
SUM OF MISSING | 2 |
WEST
SUM OF TOTAL | 6 | 6
SUM OF DONE | 2 | 3
SUM OF MISSING | 4 | 3
I want to now add a ROW that is the percentage DONE of the TOTAL amount. I.e. something like this:
CORP:
COAST: NEW | OLD
-------------------------------------
EAST |
SUM OF TOTAL | 6 |
SUM OF DONE | 4 |
SUM OF MISSING | 2 |
% DONE | %67|
WEST
SUM OF TOTAL | 6 | 6
SUM OF DONE | 2 | 3
SUM OF MISSING | 4 | 3
% DONE | %33| %50
The formula for % DONE is DONE / TOTAL
I've tried adding another DONE field to the pivot table (i.e. SUM OF DONE2) then setting the value to be % OF, the base field TOTAL and the base value (previous) however that makes % DONE return the value of #N/A. I've tried various combinations of the above however all are returning the same #N/A. I.e. my pivot table is looking like this:
CORP:
COAST: NEW | OLD
-------------------------------------
EAST |
SUM OF TOTAL | 6 |
SUM OF DONE | 4 |
SUM OF MISSING | 2 |
% DONE |#N/A|
WEST
SUM OF TOTAL | 6 | 6
SUM OF DONE | 2 | 3
SUM OF MISSING | 4 | 3
% DONE |#N/A| #N/A
I've tried Googling how to do it but still to no avail. Please let me know if what I am looking for is possible.
On the Analyze tab, add a Calculated field with the formula
= 'DONE'/'TOTAL'
There is an option in Pivot Table Options that allows you to show blank if there is an error, so you don't get an N/A for old/east. You can go into field settings and format the field as a percentage.
I am working on an Excel Spread Sheet. Where one column has tiers and another column has ratings, so basically what I am trying to do is:
+---+-------+--------+
| | A | B |
+---+-------+--------+
| 1 | Tiers | Rating |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 2 | 3 |
| 7 | 1 | 3 |
| 8 | 1 | 3 |
+---+-------+--------+
I count each tiers
4 1's
1 2's
2 3's
so to get the maximum rating for each tier I multiply them by 3 so
4*3 = 12 for tier 1
1*3 = 3 for tier 2
2*3 = 6 for tier 3
now comparing that to the rating columns I add up all the values for tier one so 1+1+3+3 = 8
Now to get the percentage I take the 8/12 get 66.67%
so how can I achieve this in Excel? Please Help :(
You can use the following to calculate the 8
=SUMIF(A2:A8,1,B2:B8)
and the following to calculate the 12
=COUNTIF(A2:A8,1)*3
Then you can just calculate the percentage using:
=SUMIF(A2:A8,1,B2:B8)/(COUNTIF(A2:A8,1)*3)
Here's the formula you need. The formula calculates for tier 1
=sumif(A2:A8,1,B2:B8)/(countif(A2:A8,1)* max(if(A2:A8=1,B2:B8)))
Broken down
Count the tier 1s. Count the cells in column A if they contain a 1
=countif(A2:A8,1)
Get the maximum rating. Return the maximum value in column B if column A contains a 1
=max(if(A2:A8=1,B2:B8))
Sum the ratings for Tier 1. Sum if column A contains a 1
=sumif(A2:A8,1,B2:B8)
You can replace the 1 in the formula with a cell reference e.g D1 and enter 1 in D1. Lock the ranges in the formula so that you can autofill across and put 2 in E1 and 3 in F1 etc.
=sumif($A$2:$A$8,D1,$B$2:$B$8)/(countif($A$2:$A$8,D1)* max(if($A$2:$A$8=D1,$B$2:$B$8)))
I have the following tables
Orders:
OrderID|Cost|Quarter|User
-------------------------
1 | 10 | 1 | 1
2 | 15 | 1 | 2
3 | 3 | 2 | 1
4 | 5 | 3 | 3
5 | 8 | 4 | 2
6 | 9 | 2 | 3
7 | 6 | 3 | 3
Goals:
UserID|Goal|Quarter
-------------------
1 | 20 | 1
1 | 15 | 2
2 | 12 | 2
2 | 15 | 3
3 | 5 | 3
3 | 7 | 4
Users:
UserID|Name
-----------
1 | John
2 | Bob
3 | Homer
What I'm trying to do is to sum up all orders that one user had, divide it by the sum of his goals, then sum up all orders, devide the result by the sum of all goals and then add this result to the previous result of all Users.
The result should be:
UserID|Name |Goal|CostSum|Percentage|Sum all
---------------------------------------------------
1 |John | 35 | 13 | 0.37 |
2 |Bob | 27 | 23 | 0.85 |
3 |Homer| 12 | 20 | 1.67 |
the calculation is as follow:
CostSum: 10+3=13
Goal: 20+15=35
Percentage: CostSum/Goal=13/35=0.37
Sum all: 10+15+3+5+8+9+6=56
Goal all: 20+15+12+15+5+7=74
percentage all= Sum_all/Goal_all=56/74=0.76
Result: percentage+percentage_all=0.37+0.76=1.13 for John
1.61 for Bob
2.43 for Homer
My main problem is the last step. I cant get it to add the whole percentage. It will always filter the result so making it wrong.
To do this you're going to need to create some measures.
(I will assume you've already set your pivot table to be in tabular layout with subtotals switched off - this allows you to set UserID and Name next to each other in the row labels section.)
This is what our output will look like.
First let's be sure you've set up your relationships correctly - it should be like this:
I believe you already have the first 5 columns set up in your pivot table, so we need to create measures for CostSumAll, GoalSumAll, PercentageAll and Result.
The key to making this work is to ensure PowerPivot ignores the row label filter for your CostSumAll and GoalSumAll measures. The ALL() function acts as an override filter when used in CALCULATE() - you just have to specify which filters you want to ignore. In this case, UserID and Name.
CostSumAll:
=CALCULATE(SUM(Orders[Cost]),ALL(Users[UserID]),ALL(Users[Name]))
GoalSumAll:
=CALCULATE(SUM(Goals[Goal]),ALL(Users[UserID]),ALL(Users[Name]))
PercentageAll:
=Orders[CostSumAll]/Orders[GoalSumAll]
Result:
=Orders[Percentage]+Orders[PercentageAll]
Download - Example file available for download here. (Don't actually read it in Google Docs - it won't be able to handle the PowerPivot stuff. Save locally to view.)
I am trying to list all of the unique occurrences of one cell on a table and then list them.
TABLE 1
PLACES WEEK
| Place 1 | Week 2 |
| Place 1 | Week 2 |
| Place 1 | Week 2 |
| Place 1 | Week 3 |
| Place 2 | Week 2 |
| Place 2 | Week 3 |
| Place 3 | Week 2 |
| Place 4 | Week 3 |
| Place 4 | Week 3 |
| Place 4 | Week 3 |
So on another sheet I want a list of all Places that are in week 2 but I only want one occurrence of each. The result being from this that I would get.
| Place 1 |
| Place 2 |
| Place 3 |
but not place 4 on my list.
Is this possible with a formula.
Thanks,
If you ok with spaces this is the first thing to come to mind
=IF(AND(Sheet1!B1= 2,$A$1:A1 <>Sheet1!A1),Sheet1!A1, "")
When you enter it, enter as array (Ctrl + Shift + Enter), make sure you get "{}" Surrounding the formula then drag down. And make sure to start in the second row of the column.