I have a pivot table that has a row for a set of cities. I want to count the number of cities in a certain state. It's not wanted to include the state in the pivot table (although that would make things simpler).
I tried using the following formula:
={countif(index(city_ref[state], match(G15:G30,city_ref[city_name],0)),"=CA")}
Where:
city_ref is a table that has at least the columns [city_name] and [state]
G15:G30 are the rows of the pivot table
The data looks like this (simplified):
city_ref:
city_name | state
====================
Los Angeles | CA
Seattle | WA
Portland | OR
etc.
G15:G30:
city_name
===========
G15: Los Angeles
G16: Seattle
etc. (but not all of city_ref)
I do realize that you could include the state in a pivot table but a) that's not wanted (even hiding it) and b) there is other, dis-related data that when the state is included the pivot table makes additional rows which messes up the count.
The index match lookup seems to return an array if used on its own; if I use the fill handle it does create an array of cells with the lookup values between G15:G30. However including this in countif does not seem to do anything.
In the help page for countif that I have in excel (unfortunately I cannot find an online reference to link to) it says that range (the first argument) can be an array.
I want to be able to take an arbitrary array of values, look them up in another table and count the ones that match my criteria.
Something like this ?
{=SUM((IFERROR(MATCH($D$2:$D$7,INDEX(IF($B$2:$B$13=$F$2,$A$2:$A$13),),0),0)>0)*1)}
Why don't you just make a copy of the PivotTable, add a Slicer so users can filter it on a particular State, and add the State field to the Values area so that it gets counted? Seems counter productive to point a formula at PivotTable 1 when you could simply have PivotTable2 do the sum by State.
Related
I'm quite new to using pivot tables and data models, so I don't even know if what I want to do is possible. I have a pivot table (PivotTable1) and its source (Table 25) and I would like to add a hundred or so measures which are listed in the TableCombinations.
For example, I entered the two first measure in orange, but they are not linked to TableCombination and entering them all one by one would be quite long. Each measure is for a distinct Sum wfn column that sums all other rows multiplied by a coefficient. The TableCombinations table simply states the coefficient to be used for each column. For the first three rows, these are my measure formulas :
sum wf1=1.4*Table25[Sum of wD]+0*Table25[Sum of wL]+0*Table25[Sum of wS]+0*Table25[Sum of wW]+0*Table25[Sum of wWSOUL]
sum wf2=1.25*Table25[Sum of wD]+1.5*Table25[Sum of wL]+1*Table25[Sum of wS]+0*Table25[Sum of wW]+0*Table25[Sum of wWSOUL]
sum wf3=1.25*Table25[Sum of wD]+1.5*Table25[Sum of wL]+0*Table25[Sum of wS]+0.4*Table25[Sum of wW]+0*Table25[Sum of wWSOUL]
...
Two questions :
Is there a way to link the tables so that any change made to TableCombination would then be updated in the pivot table measures?
Is there a way to generate all the of the measures without typing them in one by one.
You should be able to use just one DAX measure to do this, using the CROSSJOIN function.
Don't set up a relationship between the Tables, and drag # to the Columns area of the PivotTable. Then create this Measure:
=SUMx(CROSSJOIN(Table1,Table2),Table1[wD]*Table2[wD]+Table1[wL]*Table2[wL]+Table1[wS]*Table2[wS]+Table1[wW]*Table2[wW]+Table1[wWSOUL]*Table2[wWSOUL])
That should give you the exact answer you need.
Here's how it looks using some sample data:
...and here's the sample data I'm using:
You could certainly use VBA to add measures, and to update them when the Table changes. I might have a crack at writing up an answer along that approach shortly. But here's another way to achieve what you want.
I've previously written some code to slave a Table to a PivotTable, so that any change in the PivotTable's dimensions or placement will be reflected in the shadowing Table's dimensions and placement. This effectively gives us a way to add a calculated field to a PivotTable that can refer to something outside of that PivotTable. If the PivotTable grows, the Calculated Table will grow. If the PivotTable shrinks, the Calculated Table will shrink, and any redundant formulas in it will be deleted.
You can easily use this approach to perform your calculations in a 2nd table alongside your PivotTable, and each column x in that 2nd table could easily reference row x in your 'parameters' table.
See Select Newest Record and Create New Table of Unique Values in Excel
I have a [Company] column, I have a [Billing day] column in a powerpivot sheet.
A Company can have several different Billing days.
I want to show in a Pivot table, next to the company name, the Billing day with the highest number of occurences.
The tricky part is that the number must appear in a [Calculated Column] that will not be put as a Measure in my Pivot table, but as a Column.
I have reviewed such posts as:
this one or this one.
I have learned much but I am still unable to get the correct values.
Any idea ?
Many thanks
This may not be the best way to get you what I understand you are asking for, but it is a way. I believe it achieves what you want regarding the calculated column.
If I start with this table as Table1:
Then I add a column using this code: MaxByDate = COUNTX(FILTER(FILTER(Table1,Table1[Company]=EARLIER(Table1[Company])),Table1[Billing Day]=EARLIER(Table1[Billing Day])),Table1[Billing Day])
And I add another column using this code: MaxOverall = MAXX(FILTER(Table1,Table1[Company]=EARLIER(Table1[Company])),COUNTX(FILTER(FILTER(Table1,Table1[Company]=EARLIER(Table1[Company])),Table1[Billing Day]=EARLIER(Table1[Billing Day])),Table1[Billing Day]))
Then I add one last column using this code: DateOfMaxOverall = FORMAT(SUMX(FILTER(FILTER(Table1,[Company]=EARLIER([Company])),[MaxByDate]=[MaxOverall]),[Billing Day])/COUNTX(FILTER(FILTER(Table1,[Company]=EARLIER([Company])),[MaxByDate]=[MaxOverall]),[Billing Day]),"m/d/yyyy")
I get this table:
Then if I choose to create a Flattened PivotTable from it (Home tab / PivotTable dropdown arrow button / Flattened PivotTable), and set things up like this:
...and turn off the subtotals and grand totals, I get this:
Which is, I believe, what you requested.
But I think a simpler approach might be this:
Start with the first table (Table1):
And, using that table as is, create a Flattened PivotTable directly. Then set the PivotTable up like this:
...and turn off the subtotals and grand totals, to get this:
...Which you can then filter for the max occurring day for each company:
...to get this:
I have a student database, and I'm trying to show different metrics based on a student's score range in a PivotTable. Specifically (this is a simplified example, so don't worry about the content) I want to show this in my pivot:
StudentGPACat | Avg Post-Grad Salary
3-3.2 | 64,323
3.2-3.4 | 71,225
3.4-3.6 | etc
3.6-3.8 | etc
3.8-4.0 | etc
So I want the rows in my pivot table to show the range the student's average score falls in.
In order to generate that metric, right now, I did 2 things:
(1) Added a new column in my master table in PowerPivot called [avgGrade] that shows the value of the [TableAvgGrade] calculated field from the "Grades" table for each student (i.e., each row in the master table)
=CALCULATE([TableAvgGrade],
FILTER(Grades,Grades[studentID]=Master[studentID]))
(2) Created a new column [StudentGPACat] in PowerPivot and the formula goes:
=If([avgGrade]<3,"3",
If([avgGrade]<3.2,"3-3.2",
If([avgGrade]<3.4,"3.2-3.4",
If([avgGrade]<3.6,"3.4-3.6",
If([avgGrade]<3.8,"3.6-3.8","3.8-4.0")))))
This feels bulky and computationally expensive. Is there an easier way to create these ranges to use as rows in my PivotTable?
EDIT: made some edits to clarify my question
EDIT2: type
What you've done is the appropriate pattern for creating this sort of column. If you're concerned about the gnarly nested IF()s, you can replace with a SWITCH(), which is just syntactic sugar for nested IF()s, but what you've posted is all you need.
In a PivotTable (I don't know with PowerPivot), if you use a numeric value as a Row Label, you can Right click the field, choose Group, define the Starting at value, Ending at value and By step, and you will get an equivalent result quite easily.
I have an Excel table containing transactions of companies. It looks like this:
Customer Phone Item Price
==================================
Company1
Company2
Company1
Company3
Company1
I need to see the unique values of customer column.
The result I want is:
Customer Phone Item Price
==================================
Company1
Company2
Company3
Here is what I tried already:
Remove duplicates: To just get the unique values, I can use the remove duplicates of excel. However, since this is something I will be doing frequently, I would rather not have to make a copy of the table each time in order to delete duplicates.
Pivot table: A pivot table does this job perfectly. My problem here is that I need other column info as well (e.g., the phone number) which I want to appear in the column next to the company. [I haven't yet figured out how to do this in a pivot table (i.e., to show a value as text instead of sum or count etc.)] - This would be the best option for me.
As long as you don't have a super long table where the recalculation will kill you, you could just add a calculated column like this and then filter on value being 1:
=IF(COUNTIF($B$1:B2,"=" & B3) > 0,0,1)
If recalculation is an issue, you can use the same or similar method with VBA except paste static values.
PivotTables are well suited for this kind of stuff, so that'd be my first go-to, but PivotTables are also frustratingly difficult to adapt to uses beyond their envisioned use... so there's a steep decline in their usefulness as you stray from that.
Ultimately, you may want to consider a relational database paradigm, and/or using Access. That's one step up the sophistication ladder towards managing the kind of data you're talking about.
I am using Excel Powerpivot with data in two separate tables. Table 1 has ITEM level data with a BRAND characteristic. Table 2 has BRAND level data. The two tables are linked by the BRAND key. The measure I am using is non addable. i.e. the sum of the ITEMS does not equal the BRAND. The pivot is set up with ITEMS nested under BRANDS in the rows and the Measure in the column.
Excel assumes that I want to summarize ITEM to a BRAND level by applying SUM, MAX, MIN, AVG, etc. I would like to return the actual values from the appropriate ITEM or BRAND level table and not apply any calculations to the values. Is this possible?
If what you are effectively trying to do is produce a different result for the Brand rows (e.g. blank()) then the answer is to write a further measure that does a logic check to determine whether or not the row in question is an ITEM or a BRAND.
= IF (HASONEVALUE(table1[Item]), [Measure], Blank() )
Bear in mind that this will work for your current pivot but may not be adaptable to all pivots.
This assumes that you have explicitly created a measure called [Measure] and you are not just dragging the numeric column into the values box. If not you can create the initial [Measure] something like this:
= Sum(table1[Value])
Where Value is the column you want to use in the measure. Although you have used a sum, if it relates to a single item which has a single row in the table it will give the desired result.