简体   繁体   中英

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM