Explanation
To do this, LOOKUP is configured as follows:
- Lookup values are ages in column C
- The lookup vector is the named range “age” (F5:F8)
- The result vector is the named range “group” (G5:G8)
With this setup, LOOKUP performs an approximate match on the numeric values in column F, and returns the associated value from column G.
The LOOKUP function always performs an approximate match, with the following behavior:
- If LOOKUP finds an exact match in the age column, the corresponding group is returned.
- If no exact match is found, LOOKUP will traverse the age column until a larger value is found, then “step back” to the previous row.
- If an age is greater than 50 (the highest value), LOOKUP will return the group associated with 50 (“50+”).
- If age is less than the smallest value in the age column, LOOKUP will return #N/A.
Note: ages must appear in ascending order. Double-check custom intervals - it’s easy to make a mistake :)
With hardcoded values
If you want to do this kind of grouping without a table on the worksheet, you can hardcode values into LOOKUP as array constants like this:
=LOOKUP(C5,{0,10,36,50},{"<10","10-35","36-49","50+"})
Explanation
In this example, the goal is to group ages into buckets. One way to do this is to prepare a table with age breakpoints in the first column, and the name of the appropriate group or bucket in the second column. Then use a lookup function to find the right bucket or group for each age. In the example shown, we are using the VLOOKUP function for this job, and the formula in cell F7, copied down, is:
=VLOOKUP(D5,age_table,2,TRUE)
The inputs to VLOOKUP are as follows:
- lookup_value - the age from cell D5
- lookup_table - “age_table”, which is the named range G5:H8.
- col_index_num - 2 to indicate “2nd column”
- range_lookup - TRUE to specify an approximate match.
Note that because the last argument, range_lookup , is optional and defaults to TRUE, it is not necessary to provide a value for this problem. However, I personally like to set this value explicitly as a reminder of the match behavior that is expected. Also note that we are using a named range for convenience only, since named ranges do not change when copied. If you prefer, you can use an absolute reference like $G$5:$H$8 instead. We don’t want this reference to change as the formula is copied down.
In the configuration above, VLOOKUP looks up each age in the table and returns the appropriate group from the second column. Because VLOOKUP is set to perform an approximate match, it has a particular behavior. It will scan through the ages and stop at the first exact match, or, when no exact match is found, it will stop at the first larger value and then “step back” to the previous row and return the group from that row. The groups in the second column can be any values you wish.
For more details on VLOOKUP, see How to use the VLOOKUP function .
The age table must be sorted in ascending order by age for this formula to work properly.
Pivot tables
Pivot tables can automatically group numbers , but the VLOOKUP approach allows you to perform completely custom grouping.