## Sort rows based on frequency and criteria

**Andre asks:**I am trying to list people with the highest scores based on certain criteria.

My data:

column

A B C D

Mike 207 Yes Life

Greg 207 Yes Life

Sid 207 Yes Life

Greg 207 Yes Life

Greg 207 Yes Life

Sid 207 Yes Life

Greg 207 No Life

Sid 204 No Health

criteria

countif b= 207 and column c= yes and column d= Life

and it then needs to arrange it from the highest to the lowest

(Large)

and then match it with the name eg, Greg Sid or Mike

so what I am looking for is eg.

Greg 3

Sid 2

Mike 1

but it has to be in one formula.

#### Table of Contents

## 1. Sort rows based on frequency and criteria - Excel 365

The formula counts the number of rows that meet all criteria specified in row 2. It then returns a list of names that are sorted by how often they appear in the rows that match the criteria.

The data set is in B5:E12, the criteria is specified in cells C2, D2, and E2. The formula uses the criteria and the names in B5:B12 to count rows meeting the criteria.

For example, all rows except row 11 and 12 meet the criteria. Mike (row 5) exists only once, Greg (row 6) exists three times, the other rows are 8 and 9. Note that Greg is in row 11 as well, however, the adjacent values do not meet the criteria specified in row 2.

Sid has three rows, however, only two rows meet the criteria and they are row 7 and 10.

Excel 365 formula in cell B15:

This is an Excel 365 dynamic array formula that spills values to cells below and to the right, enter this formula as a regular formula.

### Explaining formula

#### Step 1 - List unique distinct items

[insertunc id="UNIQUE"]

UNIQUE(B5:B12)

becomes

UNIQUE({"Mike";"Greg";"Sid";"Greg";"Greg";"Sid";"Greg";"Sid"})

and returns

{"Mike";"Greg";"Sid"}

#### Step 2 - Count rows based on criteria and item

The COUNTIFS function calculates the number of cells across multiple ranges that equals all given conditions.

Function syntax: COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2]â€¦)

COUNTIFS(B5:B12,UNIQUE(B5:B12),C5:C12,C2,D5:D12,D2,E5:E12,E2)

becomes

COUNTIFS({"Mike";"Greg";"Sid";"Greg";"Greg";"Sid";"Greg";"Sid"},{"Mike";"Greg";"Sid"},{207;207;207;207;207;207;207;204},207,{"Yes";"Yes";"Yes";"Yes";"Yes";"Yes";"No";"No"},"Yes",{"Life";"Life";"Life";"Life";"Life";"Life";"Life";"Health"},"Life")

and returns

{1;3;2}

#### Step 3 - Add arrays horizontally

The HSTACK function combines cell ranges or arrays. Joins data to the first blank cell to the right of a cell range or array (horizontal stacking)

Function syntax: HSTACK(array1,[array2],...)

HSTACK(UNIQUE(B5:B12),COUNTIFS(B5:B12,UNIQUE(B5:B12),C5:C12,C2,D5:D12,D2,E5:E12,E2))

becomes

HSTACK({"Mike";"Greg";"Sid"},{1;3;2})

and returns

{"Mike",1;"Greg",3;"Sid",2}

#### Step 4 - Sort array by second column

The SORT function sorts values from a cell range or array

Function syntax: SORT(array,[sort_index],[sort_order],[by_col])

SORT(HSTACK(UNIQUE(B5:B12),COUNTIFS(B5:B12,UNIQUE(B5:B12),C5:C12,C2,D5:D12,D2,E5:E12,E2)),2,-1)

becomes

SORT({"Mike",1;"Greg",3;"Sid",2},2,-1)

and returns

{"Greg",3;"Sid",2;"Mike",1}

#### Step 5 - Shorten formula

The LET function lets you name intermediate calculation results which can shorten formulas considerably and improve performance.

Function syntax: LET(name1, name_value1, calculation_or_name2, [name_value2, calculation_or_name3...])

SORT(HSTACK(UNIQUE(B5:B12),COUNTIFS(B5:B12,UNIQUE(B5:B12),C5:C12,C2,D5:D12,D2,E5:E12,E2)),2,-1)

x - B5:B12

y - UNIQUE(x)

LET(x,B5:B12,y,UNIQUE(x),SORT(HSTACK(y,COUNTIFS(x,y,C5:C12,C2,D5:D12,D2,E5:E12,E2)),2,-1))

### Links

Count unique distinct records (rows) in a Pivot Table

Sort based on frequency row-wise

Extract a unique distinct list across multiple columns and rows sorted based on frequency

Unique distinct values sorted based on frequency

Extract the most frequent text value - MODE.SNGL function

How to create a frequency table based on text values

How to create a frequency table based on numerical values

Test if a data set has a unique mode or multiple modes?

Extract the most frequent text values

### Useful resources

Count how often a value occurs

How to Use Multiple Criteria in Excel COUNTIF and COUNTIFS Function

## 2. Sort rows based on frequency and criteria - earlier Excel versions

This formula does the exact same thing as the Excel 365 formula described in section 1. Read the details in section 1.

Array formula in B15:

To enter an array formula, type the formula in a cell then press and hold CTRL + SHIFT simultaneously, now press Enter once. Release all keys.

The formula bar now shows the formula with aÂ beginning and ending curly bracket telling you that you entered the formula successfully. Don't enter the curly brackets yourself.

Copy cell F2 and paste it down.

Formula in C15:

Copy cell C15 and paste it down as far as needed. Try changing criteria in cell range C2:E2.

You can do this in a pivot table as well:List people with the highest scores based on criteria in a pivot table (Excel 2007)*)*

### Explaining formula in cell B15

#### Step 1 - Apply critera - AND logic

TheÂ COUNTIF functionÂ counts values based on a condition, we need three different COUNTIF functions in order to check all three critera to corresponding cell ranges.

COUNTIF($C$2,$C$5:$C$12)*COUNTIF($D$2,$D$5:$D$12)*COUNTIF($E$2,$E$5:$E$12)

becomes

COUNTIF(207,{207; 207; 207; 207; 207; 207; 207; 204})*COUNTIF("Yes",{"Yes"; "Yes"; "Yes"; "Yes"; "Yes"; "Yes"; "No"; "No"})*COUNTIF("Life",{"Life"; "Life"; "Life"; "Life"; "Life"; "Life"; "Life"; "Health"})

becomes

{1; 1; 1; 1; 1; 1; 1; 0}*{1; 1; 1; 1; 1; 1; 0; 0}*{1; 1; 1; 1; 1; 1; 1; 0}

and returns

{1;1;1;1;1;1;0;0}

#### Step 2 - Filter values meeting criteria

TheÂ IF functionÂ has three arguments, the first one must be a logical expression. If the expression evaluates to TRUE then one thing happens (argument 2) and if FALSE another thing happens (argument 3). The following lines explain the logical expression:

IF(COUNTIF($C$2, $C$5:$C$12)* COUNTIF($D$2, $D$5:$D$12)* COUNTIF($E$2, $E$5:$E$12),(COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), "")

becomes

IF({1; 1; 1; 1; 1; 1; 0; 0},(COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), "")

The COUNTIF function in the second argument has a < less than sign concatenated to all values in the second argument, this makes the COUNTIF function return an array containing numbers representing the rank order if the list were sorted. This is needed to create unique numbers for each value because the FREQUENCY function can't handle text values only numerical values.

IF({1; 1; 1; 1; 1; 1; 0; 0},(COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), "")

becomes

IF({1; 1; 1; 1; 1; 1; 0; 0}, {5; 1; 6; 1; 1; 6; 1; 6}, "")

and returns

{5; 1; 6; 1; 1; 6; ""; ""}

#### Step 3 - Calculate frequency

The FREQUENCY function calculatesÂ how often values occur within a range of values and then returns a vertical array of numbers.

FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1)

becomes

FREQUENCY({5; 1; 6; 1; 1; 6; ""; ""}, {5; 1; 6; 1; 1; 6; 1; 6})

and returns

{1; 3; 2; 0; 0; 0; 0; 0; 0}.

#### Step 4 - Extract k-th largest number

The LARGE function returns the k-th largest number in a cell range or array. LARGE( array, k)

LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1))

becomes

LARGE({1; 3; 2; 0; 0; 0; 0; 0; 0}, ROWS($A$1:A1))

TheÂ ROWS functionÂ keeps track of the numbers based on an expanding cell reference. It will expand when the cell is copied to cells below.

LARGE({1; 3; 2; 0; 0; 0; 0; 0; 0}, ROWS($A$1:A1))

becomes

LARGE({1; 3; 2; 0; 0; 0; 0; 0; 0}, 1)

and returns 3.

#### Step 5 - Find the relative position of the value in the array

The MATCH function returns a number representing the position of a given value in an array or cell range.

MATCH(LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1)), FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0)

becomes

MATCH(3, FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0)

becomes

MATCH(3, {1; 3; 2; 0; 0; 0; 0; 0; 0}, 0)

and returns 2.

#### Step 6 - Return value

TheÂ INDEX functionÂ returns a value based on a cell reference and given column/row numbers.

INDEX($B$5:$B$12, MATCH(LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1)), FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0))

becomes

INDEX($B$5:$B$12, 2)

and returns "Greg" in cell B15.

#### Step 7 - Handle errors

The formula returns errors when all values have been displayed, theÂ IFERROR functionÂ converts the errors into a given value, in this case "" (nothing).

IFERROR(INDEX($B$5:$B$12, MATCH(LARGE(FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ROWS($A$1:A1)), FREQUENCY(IF(COUNTIF($C$2, $C$5:$C$12)*COUNTIF($D$2, $D$5:$D$12)*COUNTIF($E$2, $E$5:$E$12), (COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), ""), COUNTIF($B$5:$B$12, "<"&$B$5:$B$12)+1), 0)), "")

### Frequency table category

Excel 2013 allows you to count unique distinct values in a pivot table, this article explains how to count unique […]

What's on this page Unique distinct values sorted based on frequency Unique distinct values sorted based on frequency - Excel […]

This article demonstrates a formula that creates a frequency distribution table from a multi-column cell range which is useful in […]

### Excel categories

### Leave a Reply

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form