Get date ranges from a schedule
This article demonstrates ways to extract names and corresponding populated date ranges from a schedule using Excel 365 and earlier Excel versions.
Table of Contents
1. Get date ranges from a schedule - Excel 365
The following formulas extract names and date ranges from the schedule in sheet1, the schedule has dates in row 2 and names column A.
The "x" indicates populated days. The formulas use the "x" to extract the correct name and the corresponding dates. Note that there may be multiple date ranges in the schedule for the same person.
Excel 365 dynamic array formula in cell B3:
Excel 365 dynamic array formula in cell C3:
Excel 365 dynamic array formula in cell D3:
1.1 Explaining formula in cell B3
The formulas in cell C3 and D3 are similar to the formula in cell B3, they extract start and end dates instead of names.
Step 1 - Compare cell ranges and check if values are not equal
The less than and larger than signs are logical operators, they return TRUE or FALSE which are boolean values. The less than and the greater than signs combined means "not equal to"
Sheet1!B3:NA7<>Sheet1!C3:NB7
becomes
{0,0,0,"X", ... ,0}<>{0,0,"X","X", ... ,0}
These are big arrays, and I have shortened them.
{0,0,0,"X", ... ,0}<>{0,0,"X","X", ... ,0}
returns
{FALSE,FALSE,TRUE,FALSE, ... , FALSE}
Note that the cell references are not perfectly aligned, the last cell reference (red) is offset one cell to the right. This technique lets us pinpoint when a populated cell range begins and ends.
The way comparing cell ranges works is that a cell in one cell range is compared to the corresponding cell in the second cell range. For example, cell B2 is compared to C2 and only C2. Cell B3 is compared to cell C3 and so on.
Step 2 - Check if cells are empty
The equal sign is also a logical operator, the following part cheks if a cell is empty "".
Sheet1!B3:NA7=""
becomes
{"","","","X", ... ,""}=""
and returns
{TRUE,TRUE,TRUE, FALSE, ... ,TRUE}
Step 3 - Multiply arrays
The asterisk lets you multiply numbers and boolean values in an Excel formula. This lets you create AND logic between boolean values meaning both values must be TRUE to return TRUE.
TRUE * TRUE = TRUE (1)
TRUE * FALSE = FALSE (0)
FALSE * TRUE = FALSE (0)
FALSE * FALSE = FALSE (0)
Excel converts the result to the boolean values numerical equivalents.
TRUE = 1
FALSE = 0
(Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7="")
becomes
{FALSE,FALSE,TRUE,FALSE, ... , FALSE}*{TRUE,TRUE,TRUE, FALSE, ... ,TRUE}
and returns
{0,0,1,0, ... ,0}
Step 4 - Replace with names from column A
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
Function syntax: IF(logical_test, [value_if_true], [value_if_false])
IF((Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7=""),Sheet1!A3:A7,"")
becomes
IF({0,0,1,0, ... ,0}, {"Graham Chapman"; "John Cleese"; "Eric Idle"; "Terry Gilliam"; "Michael Palin"},"")
and returns
{"","","Graham Chapman", ... ,""}
Step 5 - Rearrange values to a single column layout
The TOCOL function rearranges values in 2D cell ranges to a single column.
Function syntax: TOCOL(array, [ignore], [scan_by_col])
TOCOL(IF((Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7=""),Sheet1!A3:A7,""))
becomes
TOCOL({"","","Graham Chapman", ... ,""})
and returns
{"";"";"Graham Chapman"; ... ;""}
Note that the semicolons changed to commas. They are delimiting characters in an array, you may have other characters as they are based on the regional settings of your computer.
Step 6 - Check if cells are not empty
This step is to create a logical test that is then used in the next step to filter out empty values.
TOCOL(IF((Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7=""),Sheet1!A3:A7,""))<>""
becomes
{"";"";"Graham Chapman"; ... ;""}<>""
and returns
{FALSE;FALSE;TRUE; ... ;FALSE}
Step 7 - Filter out empty values
The FILTER function extracts values/rows based on a condition or criteria.
Function syntax: FILTER(array, include, [if_empty])
FILTER(TOCOL(IF((Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7=""),Sheet1!A3:A7,"")),TOCOL(IF((Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7=""),Sheet1!A3:A7,""))<>""))
becomes
FILTER({"";"";"Graham Chapman"; ... ;""},Sheet1!A3:A7,"")),{FALSE;FALSE;TRUE; ... ;FALSE})
and returns
{"Graham Chapman"; "Graham Chapman"; "Graham Chapman";Â ... ; "Michael Palin"}
Step 8 - 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...])
FILTER(TOCOL(IF((Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7=""),Sheet1!A3:A7,"")),TOCOL(IF((Sheet1!B3:NA7<>Sheet1!C3:NB7)*(Sheet1!B3:NA7=""),Sheet1!A3:A7,""))<>""))
x - Sheet1!B3:NA7
y - Sheet1!C3:NB7
w - TOCOL(IF((x<>y)*(x=""),Sheet1!A3:A7,""))
LET(x,Sheet1!B3:NA7,y,Sheet1!C3:NB7,w,TOCOL(IF((x<>y)*(x=""),Sheet1!A3:A7,"")),FILTER(w,w<>""))
1.2 Get Excel *.xls file
2. Get date ranges from a schedule - earlier Excel versions
The above picture shows you two formulas that extract names (column B) and date ranges (column C and D) based on a cross-reference schedule.
The schedule has dates in row 2, however, the cell is formatted to show only the day number.
Names are in column A and there are no duplicate names. An X indicates that a date is selected for the given person, multiple x's in a sequence is a date range.
The array formula in cell B3 returns the correct number names needed to return all date ranges:
Array formula in cell C3:
Array formula in cell D3:
- Sheet1!$A$3:$A$7 is the cell reference to the names in the schedule.
- Sheet1!$B$3:$AA$7 and Sheet1!$C$3:$AB$7 are a cell reference to the X's in the schedule.
Two are needed for the formula to count the number of date ranges.
Note! The cell refs have the same size, however, the latter is offset by one column.
How to enter an array formula
To enter an array formula, type the formula in cell B3 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, they appear automatically.
Explaining the formula in cell B3
Use "Evaluate Formula" on tab "Formula" on the ribbon to go through the steps.
Step 1 - Count previous values
COUNTIF($B$2:B2,Sheet1!$A$3:$A$7)
becomes
COUNTIF("Name", {"Graham Chapman"; "John Cleese"; "Eric Idle"; "Terry Gilliam"; "Michael Palin"})
and returns {0;0;0;0;0}. None of the names have been displayed in cells above, remember that I am right now showing the calculation steps in cell B3.
As soon as the first name has been displayed the same number of times as it has date ranges the formula continues to the second name.
We are now going to count how many date ranges there is in the schedule per row.
Step 2 - Calculate where a cell value changes in a row
To count date ranges the formula needs to compare a cell with the next. To be able to compare all values in one calculation I use two cell ranges. The last cell range has the same size as the first, however, it is offset one column to the right.
(Sheet1!$B$3:$AA$7<>Sheet1!$C$3:$AB$7)*1
becomes
{0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0;0, 1, 1, 0, 0, 0, ... , 0}
Part of the array is displayed in cell range B8:X12 in picture below.
Step 3 - Sum values per row
The amazing MMULT function allows you to sum numbers in an array per row or column. The earlier calculation step created an array that shows when a cell has a different value compared to the next cell.
That makes the array show when a date range starts and ends, to be able to count date ranges we must divide the sum of each row with 2.
(MMULT((Sheet1!$B$3:$AA$7 <> Sheet1!$C$3:$AB$7)*1, TRANSPOSE(COLUMN(Sheet1!$B$3:$AA$4)^0))/2)
becomes
({6; 6; 2; 6; 4}/2)
and returns {3; 3; 1; 3; 2}.
The array tells us that the first name has three date ranges, the second name has three date ranges and so on.
The image above shows that the first name has three date ranges, the second has 3, the third has 1, the fourth has 3, and the last name has 2 date ranges.
The calculation is correct.
Step 4 - Compare arrays
The number of names displayed must match the corresponding number of date ranges.
To do that I compare the arrays, if they are equal the logical expression returns TRUE. If not equal it returns FALSE.
COUNTIF($B$2:B2, Sheet1!$A$3:$A$7)=(MMULT((Sheet1!$B$3:$AA$7<>Sheet1!$C$3:$AB$7)*1, TRANSPOSE(COLUMN(Sheet1!$B$3:$AA$4)^0))/2)
{0;0;0;0;0}={3; 3; 1; 3; 2}
and returns {FALSE; FALSE; FALSE; FALSE; FALSE}.
Step 5 - Find position of first name that is shown less times than there is corresponding date ranges
The MATCH function allows you to identify the position of a specific value in an array or cell range, if the third argument in the MATCH function is 0 (zero) meaning EXACT match.
MATCH(FALSE, COUNTIF($B$2:B2, Sheet1!$A$3:$A$7)=(MMULT((Sheet1!$B$3:$AA$7<>Sheet1!$C$3:$AB$7)*1, TRANSPOSE(COLUMN(Sheet1!$B$3:$AA$4)^0))/2), 0)
MATCH(FALSE, {FALSE; FALSE; FALSE; FALSE; FALSE}, 0)
and returns 1.
Step 6 - Get the name
The INDEX function gets the first value in cell range Sheet1!$A$3:$A$7.
INDEX(Sheet1!$A$3:$A$7, MATCH(FALSE, COUNTIF($B$2:B2, Sheet1!$A$3:$A$7)=(MMULT((Sheet1!$B$3:$AA$7<>Sheet1!$C$3:$AB$7)*1, TRANSPOSE(COLUMN(Sheet1!$B$3:$AA$4)^0))/2), 0))
becomes
becomes
INDEX({"Graham Chapman"; "John Cleese"; "Eric Idle"; "Terry Gilliam"; "Michael Palin"}, 1)
and returns "Graham Chapman" in cell B3.
Dates category
Question: I am trying to create an excel spreadsheet that has a date range. Example: Cell A1 1/4/2009-1/10/2009 Cell B1 […]
This article demonstrates how to return the latest date based on a condition using formulas or a Pivot Table. The […]
This article demonstrates how to match a specified date to date ranges. The image above shows a formula in cell […]
Schedule category
In this post I am going to add one more function to the weekly schedule I built in a previous […]
Geoff asks: Hi Oscar, I have a cross reference table we use for shift scheduling. The x-axis is comprised of […]
In a previous post I created a simple weekly schedule with dynamic dates, in this post I am going to […]
Two dimensional lookup category
Geoff asks: Hi Oscar, I have a cross reference table we use for shift scheduling. The x-axis is comprised of […]
Question: How would I go about looking up data in a cross-reference table. I have the header row (i.e. 24) […]
This article demonstrates a few different formulas that extract values from the table column header names and the right-most column. […]
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.