Sum by month ignore year
where amount (C5:C16) and date (B5:B16) are named ranges. The result in F5 is 225, the sum of amounts in January for both 2022 and 2023. As the formula is copied down, we get a sum of amount by month that ignores years.
In this example, the goal is to sum numeric values by month while ignoring the year that contains the date. The solution below is based on the SUMPRODUCT function, the MONTH function, and Boolean algebra. For convenience, amount (C5:C16) and date (B5:B16) are named ranges.
The basic concept in this formula is to extract just the month number from all dates and test this number against the month number of interest. For example, to extract the month number of the dates in date, we can use the MONTH function like this:
Because the named range date contains 12 dates, the result from MONTH is an array with 12 numbers like this:
These twelve numbers correspond to the month numbers of the dates seen in column B. If we want to test these numbers for dates in January (which is the first month in the year), we can write a formula like this:
This formula returns an array with 12 TRUE and FALSE values like this:
Notice there are two TRUE values in this array, which correspond to the two dates that occur in January: one in 2022 and one in 2023. All remaining values are FALSE, since other dates do not occur in January.
To use this array of Boolean values to sum amounts in January, we can write a formula like this:
After the first expression runs, we have the array of TRUE and FALSE values we looked at above:
Next, the math operation of multiplying the two arrays together automatically coerces the TRUE and FALSE values into 1s and 0s, which we can visualize like this:
Now you can see how the logic works. When the two arrays are multiplied together, the 1s in the return the corresponding value in amount, while the 0s "cancel out" the other amounts. We are left with a single array in SUMPRODUCT like this:
With just one array to process, SUMPRODUCT sums the array and returns 225 as the final result for January. By altering the month number, we can do the same thing for other months:
In each case, the year values of the dates being tested are completely ignored.
As seen above, we can hardcode month numbers into the formula and get correct results, but how can we make the formula dynamic, so that it will automatically apply the correct month number for each month seen in column F? In the worksheet, the values in F5:F7 are simply text values like "Jan", "Feb", and "Mar". One way to get a month number from a month name is to concatenate the month name to the number 1, and feed the result into MONTH like this:
=MONTH(E5&1) // returns 1
The result inside of of MONTH is the string "Jan1", which Excel interprets as the date January 1 of the current year, and MONTH returns 1. We can do the same thing with E6 and E7:
For a more detailed explanation, see this example.
Putting it all together
The last step is to combine the ideas above into one formula:
This is the formula in F5 of the worksheet shown. As the formula is copied down, the month changes at each new row, and SUMPRODUCT calculates the sum of amounts for each month, ignoring year values.
Count by month ignoring year
Using the same ideas explained above, you can get a count by month like this:
In this formula, we use a double negative (--) is used to coerce TRUE and FALSE values to 1s and 0s. This step is necessary because we don't have a math operation doing this conversion automatically.