How to sum the number of rows in a pivot table? - excel

I would like to have the number of voters per age group per party in this
excel sheet .
In SQL it would be something like
SELECT COUNT(*) from VOTING GROUP BY AGEGROUP GROUP BY PARTY
This might not be proper SQL syntax but you better get the idea what I want.
I created a pivot table and selected AGE GROUP and PARTY as rows but it is not clear how could I sum the number of voters in the cross-section of this two aspects.
There is a greek Sigma VALUES fiels right below but it is not clear how could I use that for summing up -- there is no menu on right-click.
How can count the rows per AGE GROUP per PARTY?

One approach is to use the party as columns and AGE GROUP as rows or vice versa
If he results is not comfortable for you to read then you can make additional row to your row data which concatenate the rows "age group" and "party" together.
This formula combines cells a1 and b1 together with comma inside:
=A1&","&B1
Just drag the formula to all of the rows…
Than make a pivot table and add for the rows only the additional column you just made
I added the comma so you can split them back easily with text to columns.

Try this:
SELECT
count(Voter) over (partition by PARTY, AgeGroup) as Counter, Party, AgeGroup
FROM Voting

Related

Count unique text values based on criteria in other column

I have two columns titled "Company" and "Department", example below. The third column is what I would like to create, but I am unsure how. I want the third column to show the number of departments for a given company, and only show that number in the first reference to the company.
Try this in D2 and fill down.
=IF(COUNTIF(A$2:A2, A2)=1, SUMPRODUCT((A$2:A$999=A2)/(COUNTIFS(B$2:B$999, B$2:B$999&"", A$2:A$999, A2)+(A$2:A$999<>A2))), "")
If you create a helper column in Column C that concatenates your two cells, you can then use a function like this:
=if(countif($C$1:C7,C7)=1,sumif(….),"")
If that's not an option, you'll need to use an Array which is likely not something you want to do either.
You might be able to capture your requirements with a pivottable too... Not sure.
You could use a pivot table to get distinct count of column department.
While creating the pivot table you need to add the table to the data model so that you expose "distinct count"
(one of numerous sources: http://www.howtoexcelatexcel.com/formulas/excel-tip-how-to-count-unique-items-in-excel-pivot-table/)

Count values for each row with a unique ID

I have a bunch of rows in a table. Each row reflects an event in a patient. However, one patient can have experienced multiple events, so it's possible for there to be multiple rows with the same patient number. Now I'd like to count the amount of male patients in my database, without counting the ones that had multiple events multiple times. Each patient is identified by a unique patient ID that could be used for this.
This shouldn't be all that complicated if not for the fact that I'm using a table that also has several filters, so I need to use SUBTOTAL for any counting functions.
I literally have no idea where to start, so I can't really provide any code...
Any function that could point me in the right direction would be greatly appreciated.
Thanks for the help.
~Laurens
Use a Pivot Table to filter and count all your patients database. Select your data and select Insert -> Tables -> Pivot Tables. Put your filters at the Filter section of the table and the Patient ID in the Rows section. Then, you can use COUNT to get the number of patients.
For more information about Pivot Tables, you can check this: https://support.office.com/en-us/article/Create-a-PivotTable-to-analyze-worksheet-data-a9a84538-bfe9-40a9-a8e9-f99134456576
To get the number of unique IDs in the same column, if the IDs are numeric, you can use SUM with FREQUENCY:
=SUM(IF(FREQUENCY($A$1:$A$1000,$A$1:$A$1000)>0,1))
If they're text and numbers mixed, you can get unique IDs with this one:
=SUM(IF(FREQUENCY(MATCH($A$1:$A$1000,$A$1:$A$1000,0),MATCH($A$1:$A$1000,$A$1:$A$1000,0))>0,1))
(From here)
Here you go
You've not mentioned whether an event is optional.
You might want to add extra column H with formula like h2=if(c2="",0,1) with 1/0 and multiply it as well in G.
Basically if column G contains a 1 you include it
Here's what the results of the formula look like:
Revision
Table is sorted by Patient id..
on change of patient id column H contains a 1, it'll be 0 otherwise.
So H2 is hard coded to 1, H3,H4,H6 will evaluate to 1.
So now G2=H2*E2 etc. You can filter by column H.
The beauty of mapping things into binary zeros and ones is you can do multiplication to achieve a logical AND result, whilst at the same time breaking a complex task into a series of steps. You can then apply a filter to the data to get the rows where column G are not zero, and see the totals count. Normally I'd insert a column between header and data on row 2 and then have G2=SUM(G3:G9)
Sum column H for number of patients.

Count number of unique combinations of two columns

I have a spreadsheet of statistics from sports games over a season, for different leagues - each row holds a single event that happened in a game, such as a penalty. There are many rows of events for each individual game. One of the columns is the league, another is the home team and another is the away team. How can I count the total number of games in a given league? In other words, I would need to count the number of unique pairs of strings from Home and Away, where League = "Ligue 1".
EDIT
I have tried:
=SUMPRODUCT(1/(COUNTIFS(E2:E81078,"Ligue 1",F2:F81078,F2:F81078,G2:G81078,G2:G81078)))
which returns a DIV/0 error (it does work if I dont include the column E = "Ligue 1" criteria).
This is similar to your formula but deals with the division by zero
=SUM(IFERROR((1/COUNTIFS(E2:E81078,"Ligue 1",F2:F81078,F2:F81078,G2:G81078,G2:G81078)),0))
Enter it with Ctrl+Shift+Enter rather than just Enter. If done correctly you will see {} around the formula
Try not to use ranges that are bigger than your data because it will slow these kind of formulas down significantly
Update
This might also work if your data is ordered the way you show in your question. It counts the number of times the home team changes in Ligue 1 data :
=SUMPRODUCT((F3:F81079<>F2:F81078)*(E2:E81078="Ligue 1"))
Note that the ranges in column F are offset by one row
You can do this with a Pivot Table.
Add a "helper" column where you concatenate the two teams, preferably with a delimiter in between, eg:
=CONCATENATE(B2, "|", C2)
Use, for example Teams for the column header
Then, Insert ► Pivot Table and be sure to select to Add to Data Model
This adds the option for Distinct Counts to the Values Settings
Then Drag "league" to the Rows area, "Teams" to the Values area, and select Distinct Count for the Value Setting
You might get a table similar to below, which you can format in many different ways:
EXCEL SCREENSHOT=SUMPRODUCT(1/COUNTIFS($B$1:$B$7,B1:B7,$C$1:$C$7,C1:C7))
TRY THIS =SUMPRODUCT(1/COUNTIFS($B$1:$B$7,B1:B7,$C$1:$C$7,C1:C7))

how to calculate the means of 100s of subgroups in excel

I have a spreadsheet with ~8000 records, there are ~400 unique identifiers (i.e. element 101, 102, 103....500) that I need to calculated means for. Is there a simple way to calculate means on large datasets like this?? Or will I have to do =average('select column block') for each subgroup/unique identifier?
Many Thanks
Use the following formula
=AVERAGEIF($A$1:$A$8000,"=IDNUMBER",$B$1:$B$8000)
Where
Column A is your column of ID numbers
Column B is your list that you need the mean from.
If your ID numbers are sequential, you can set up something like:
=AVERAGEIF($A$1:$A$8000,"="&100+row(A1),$B$1:B8000)
And copy that down from say C1 to C500
Alternatively you could make a list of the unique identifiers with another formula and place that unique list in C1 to C500 and then in column D use the following:
=AVERAGEIF($A$1:$A$8000,C1,$B$1:$B$8000)
If you have a header row you will need to adjust your ranges accordingly
The formula to generate a unique list of IDs is:
=INDEX($A$2:$A$8001,MATCH(0,INDEX(COUNTIF($C$1:C1,$A$2:$A$8001),0,0),0))
Use that in column C but in row 2 and copy down. So if your data starts in row 1 you will want to bump it down 1 row.
Create a pivot table with the unique identifiers in the rows and calculate the average of the values.
For data that is clustered up nicely and immediately ready to be handed off for a visual review of the averages try a creating a Subtotal:
Select your data
Go to Data > subtotal (far right on the tab)
On the menu popup in the At each change in field, select the column header name that corresponds to your unique identifier.
Select Average for Use function. Select the checkbox of the column for which you want to find the group's mean.
Select other formatting features if desired (defaults typically work best)
Click okay.
Take a sip of coffee and let the magic happen.

Distinct Count by Date in PowerPivot Column

I have a client I am importing lots of data from but to narrow it down it pulls in to powerpivot with a date column and a product column. They want to see a summary of how many days there was one item sold ("Solo Sale") two ("Double Sale") three, and four or more.
I am running into trouble due to the fact that they want to summarize the rest of their data by whether the day was a "Solo Sale" or "Double Sale" etc etc. Let's call this this the "Sale Count"
Because I need these "Sale Count" values on the rows of a Pivot Table, I'm trying to build a calculated column in the table that does a distinct count of the products by date but can't quite seem to get there using CALCULATE and/or FILTER. It always seems to give me a total distinct count of the products in the entire table rather than counting by each date.
Any advice on the DAX formula I should be using would be greatly appreciated. New to Power Pivot and I think I'm still thinking a little too excel-centric given there is very little row-logic in Power Pivot.
Thanks in advance!
Edited: Here is the table
PowerPivot Table
I would like the values in the SalesCount column to read 4 for every row with a date 1/1/2016, 1 for 1/2/2016, 2 for 1/3/2016, and 2 for 1/4/2016 (equivalent to the DISTINCTCOUNT of the Products on each day).
Then I would like to use conditional IF statements to replace those numbers with "Solo Sale" "Double Sale" etc so that I can use these values on the rows of a Pivot Table.
Try this as your calculated column:
=CALCULATE(
DISTINCTCOUNT(Table1[Product]),
ALLEXCEPT(Table1, Table1[Date])
)

Resources