transcript

excelisfun - Excel Magic Trick 1410: Calculate Closing Balance for Each Month from Balance Transaction Table

Thumbnail of the video “excelisfun - Excel Magic Trick 1410: Calculate Closing Balance for Each Month from Balance Transaction Table”
Download Markdown
Community upload. AI output has not been independently verified.

Welcome to Excel magic trick number 1,410. If you want to download this Excel workbook so you can follow along, click on the link below the video. In this video, we see how to calculate the closing balance for each month from a balance transaction table.

Here is our balance transaction table, and I made just a small table with only two customers. It is not even that big. The fundamental problem for us, if we are getting the ending balance, is that for January I need to find the last day of the month and add all the values on the last day of the month. Our table over here will have the row headers as the actual end of the month for our report. We need the end of the month, but here is the problem: for some months, the actual ending balance does not fall on the last day of the month. So over here, if we use end of the month as criteria, we somehow have to pick out these two records and add these two numbers.

Let's come over here. I am going to use SUMIFS, but when I get to the criteria, I have to pick out the last date from this date column. So let's look at how we will do that internally, because this will be the criteria for SUMIFS to add.

For the first example, this will only work if you have Excel 2016 Insider Edition. I am going to use the MAXIFS function. The max range is the set of values we want to find a maximum in, which is the date column. This is an Excel table, so I can point to the very top part of the field name or column header. When I see the black downward-pointing arrow, I can click and it puts the table formula nomenclature in. That is the table name, and then in square brackets the field name. Then, comma, the criteria range is the very same column, comma, and then the criteria. I want to look through every single date and ask whether it is less than or equal to. You have to put your comparative operator in double quotes, "<=", and join it to the end-of-month date here.

What does MAXIFS do? It finds TRUE for all of these dates, so it looks through all of them and picks out 1/31/2017. When it gets to February, it gets a TRUE for everything less than or equal to 2/28. But since 2/27 is the maximum, MAXIFS reports that. I close the parentheses, press Ctrl+Enter, and then double-click to send it down. As our criteria inside SUMIFS, it will totally work. I press Ctrl+Z and F2.

This MAXIFS approach and a few other examples do not depend on the column being sorted. If the column is always sorted, there is an easier way to do this, but MAXIFS does not care how the table is sorted.

Now I type an equals sign and SUMIFS. Our sum range is the balance AR column, which I click right above. There is our table and field name, comma, then the criteria range, which is the date column. Then the MAXIFS result delivers the criteria to SUMIFS. I close the parentheses, press Ctrl+Enter, double-click, and send it down. I go to the last cell with F2, and the cell references look good. I press Escape. Now I come to the top cell with F2 and carefully copy the first part of this formula, since I will use it a few different times.

If you have Excel 2010 or earlier, instead of MAXIFS we can use AGGREGATE to get our date. The great thing about AGGREGATE is that it has a set of functions, and we would like to use MAX, but we cannot, because functions 1 to 13 do not allow array calculations, and we are going to do an array calculation. Luckily, number 14 is LARGE. If we say LARGE with the value one, it is the same as doing MAX. We are going to get errors in our array operation, so I use the sixth option and then the array. We use the top argument, not the bottom one. I need to pick out the date, so I say the whole date column, and we divide it by the date column in parentheses. But I need to pick out only the dates that are less than or equal to 1/31, so I do a direct array operation: how many of the dates are less than or equal to that end of the month? I close the parenthesis.

If I highlight just the denominator and press F9, we get TRUEs and FALSEs. TRUEs in the denominator serve as one, and FALSEs are zero. So when we evaluate TRUEs and FALSEs in the denominator with a set of dates, F9 gives us all the valid dates that are less than or equal to, and dividing by zero comes from dividing by FALSE, which is zero. AGGREGATE ignores all of those errors and picks out the right date. I press Ctrl+Z, then type the comma and the function number for the largest value, so we put in the first, biggest one. I close the parenthesis, press Ctrl+Enter, double-click, and send it down. We can see we get all the correct dates. I press Ctrl+Z. Then I come to the top with F2, backspace, and paste with Ctrl+V. I come to the end and close the parenthesis. Now our formula has a sum range, a criteria range, and the criteria is AGGREGATE. We use AGGREGATE instead of MAXIFS, as in the first one. I press Ctrl+Enter, double-click, and send it down.

If the table is always sorted, there is a much easier way to get that date, which is to use VLOOKUP. I simply look up this date, then the whole column range, and I want to return something from column one. I definitely want approximate match, since that is the default. I backspace and do not even put in the range lookup argument. I close the parenthesis.

How does approximate match lookup work? It takes this date and, as a metaphor, races through until it finds the first bigger one, which would be 2/15, then jumps back and gets the one before it. It actually does a binary search, but because the column is sorted, it always gets the last date. When it gets to 2/28, it races through, finds the first bigger one, jumps back, and gets 2/27. I press Ctrl+Enter, then Ctrl+1 for date formatting, press Enter, double-click, and send it down. We can see we get the correct date down the whole column. I press Ctrl+Z and F2, come right to the beginning, backspace, and paste with Ctrl+V. I close the parenthesis. Our formula has a sum range and a criteria range, and VLOOKUP, because the column is sorted, gets the right value. I press Ctrl+Enter, double-click, and send it down. We will sort this at the end, after our last example, to prove that this one will not work.

If we were before Excel 2010, like Excel 2007 or 2003, we could use MAX, and inside the MAX we need only a certain set of dates. So I use the IF function to filter the whole date column. For the logical test, I say the whole date column, and ask how many are less than or equal to the end of the month. If they are TRUE, comma, then what do I want? I put in the whole date column, comma, because I will leave the FALSEs out. Backspace, and I do not even put it in. Wherever there is a FALSE, a FALSE is put into the returned array. I close the parentheses. Now I want to come inside MAX, click the number one, and press F9 to prove that there are our serial number dates and FALSEs. With MAX and IF in a direct array operation, instead of divide-by-zero errors we get FALSEs as our filter. Luckily, the MAX function and all the AGGREGATE functions are programmed to ignore those FALSEs. I press Ctrl+Z.

This direct array operation sits in the logical test argument, and that argument cannot calculate an array operation correctly unless we use Control+Shift+Enter. Back with AGGREGATE: AGGREGATE is one of five functions that can calculate array operations automatically, but if we enter this with the IF version, we have to use Control+Shift+Enter. Immediately we go up to the formula bar and verify that the curly brackets have been added. Those curly brackets tell us Excel understood this is an array calculation and calculated it correctly. I press Ctrl+Shift+Enter, double-click, and send it down. We can see we have all the correct dates. I press Ctrl+Z, F2, come right after the equals sign, backspace, and paste with Ctrl+V. I close the parenthesis. In this formula we have a sum range, a criteria range, and a direct array operation inside the criteria argument. We have to enter it with Control+Shift+Enter, verify the curly brackets, and we are good to go. I double-click and send it down.

Now, what happens if we sort the table so the largest balances are at the top? MAXIFS, AGGREGATE, and the array calculation with MAX and IF all still work correctly. But VLOOKUP approximate match depends on this column always being sorted, so it will not work. I press Ctrl+Z.

That was a bit of fun with SUMIFS and MAXIFS: SUMIFS with MAXIFS, SUMIFS with AGGREGATE, SUMIFS with VLOOKUP if the column is sorted, and SUMIFS with a direct array calculation using the IF and MAX functions. We will see you in the next video.