transcript

excelisfun - Excel Magic Trick 1163: COUNTIFS For Dates, When Dates Have Time Value Too? COUNTIF too.

Thumbnail of the video “excelisfun - Excel Magic Trick 1163: COUNTIFS For Dates, When Dates Have Time Value Too? COUNTIF too.”
Download Markdown
Community upload. AI output has not been independently verified.

Welcome to Excel Magic Trick number 1,163. If you want to download this workbook, 1,163 to 1,165, click on the link below the video. In this video, we simply want to use COUNTIFS to count how many dates from this column happen on 1/20/2014. So if we use COUNTIFS, and I'm going to use COUNTIFS, we use the range, and I'm going to click in the top cell and press Control+Shift+Down Arrow, then F4 to lock it. That's the whole range. Now we have to give it a criteria, so I'm going to say the criteria is 1/20/2014. That means I want to count through this column and find out how many dates in January 2014 there were. The result is zero. When I double-click and send it down to check the cell references, it looks like they're working correctly, and they're all zero.

The problem is that this has a time value too. Dates are serial numbers, the number of days since December 31, 1899, and times are decimals between zero and one. So if I remove the number formatting from the Home tab's Number drop-down by choosing General, or I use the keyboard shortcut Control+Shift+Tilde, I can see the serial number that represents the date and the decimal that represents the time. What I really want is just the integer part, not the decimal. Control+Z to undo.

Let's first see how to use a helper column. We can simply use the INT function, which is the integer function. It takes the integer part, or the whole number. In our case, I close the parentheses, press Control+Enter, and I have my speak cells on Enter turned on, so I double-click to remove that. I'm going to go to Home and instead of a custom date format, I'll choose Short Date, and there we go. Now I double-click and send this down, and there we have it. Now I can come over here and enter =COUNTIFS. I'll highlight the whole helper column range, press Control+Shift+Down Arrow, then F4, then a comma, and then use the arrow keys to get my criteria, and close the parentheses. Press Control+Enter. I can't double-click because there's nothing to the left, so I'll scroll down. Notice I pressed Control+C to copy before scrolling down. Then I click right here, hold Shift, click, and press Control+V. If I look at one of these, I can see my cell references are correct now.

How do we do that without a helper column? We created a helper column using the INT function, and the INT function looks at a single number and gets just the integer part. What if we couldn't have this helper column and we wanted to simulate this whole column in a single cell? We can do an array calculation on the whole column. An array just means more than one item. So we'll use the INT function on the whole column: =INT, and again that expects a single number, but we'll give it a whole column. Press Control+Shift+Down Arrow, then F4 to lock it, and close the parenthesis. Now if I hit Enter, it won't show me anything. I can even double-click, and it shows the time. If I right-click and use the Mini Toolbar Format Painter and click, that copies just the formatting, but that won't work, because even though the INT function is spitting out lots of numbers, the cell can only show a single number.

So we'll do a trick. We highlight this and evaluate it using the F9 key, and you can see all the serial numbers now. If we could format that, you would see it's exactly the same as before. Control+Z to undo that, but now I have my whole helper column inside the cell.

Next, I'll do a second array calculation. By the way, that's a function argument array operation, because I put more than one item in there. INT spits out lots of answers. I put in however many there were, about 500, and INT spits out 500 answers. Now we need a second array operation. I need to ask a question: are any of those equal to the relative cell reference? When I highlight this and press F9, it gives me TRUEs and FALSEs. You can see there's a TRUE right there. Control+Z. Now I'll put this inside SUMPRODUCT. Remember, we want to add one for TRUE and zero for FALSE. We use SUMPRODUCT, not SUM, because SUMPRODUCT can handle array operations without needing Control+Shift+Enter. But if I press Enter right now and Control+Shift+Tilde to remove the number format, I get zero, and I already know there are 18. The reason is that SUMPRODUCT can't understand TRUEs and FALSEs directly. So we use the double negative trick, which converts TRUEs and FALSEs to ones and zeros. Any math operation on TRUEs and FALSEs converts them to ones and zeros, and I'm using the double negative because it's the most efficient inside SUMPRODUCT.

Now I come to the end and close the parenthesis. I had to put those orange parentheses around the comparison because the equal sign calculates after negation, so I force it to happen first to get the TRUEs and FALSEs. Close the parenthesis at the end of SUMPRODUCT, press Control+Enter, double-click, and send it down. Now if I were to right-click and delete the helper column over here, of course the helper-column version wouldn't work, but this one does without any helper column. Control+Z to bring that back.

So we have two options: a helper column, which is the easiest way with COUNTIFS, or we can switch to an array formula. We'll see you in the next trick.