databasesortinggoogle-sheetssum

DSum mistake in choosing the right database records with multiple criteria in 3 fields


In the following Google-sheets database, I cannot get the choice of correct records with multiple criteria in 3 fields.

Plant Number Field Expire date
Oak 10 A 2034-11-01
Cherry 5 B 2026-04-01
Elm 20 A 2038-12-01
Maple 5 B 2025-11-01
Oak 30 B 2033-05-01
Pine 10 A 2025-12-31
Yew 5 B 2025-03-01
Yew 20 A 2028-06-01
Elm 3 A 2024-12-31

with this array

Plant Field Expire Date
Yew A <=2034-10-21
B

and this function

DSUM($A$1:$D$10;"Number";$I$16:$K$19)

I cannot obtain the correct sum (25) of the yew trees because the function calculate 108.

Then with the same database and the following array

Plant Field Expire Date
Oak B >=2026-04-24
Maple A <=2031-10-22
Cherry
Elm
Pine

I try to to get the sum of the Oak, Maple, Cherry, Elm and Pine trees, in the fields A and B, with a maturity between >=2026-04-24 to <=2031-10-22 with this function:

DSUM($A$1:$D$10;"Number";$I$8:$K$13)

and the obtained result is 68 while only 10 trees meet the requirements.

Just in case it is more convenient for a visual approach,

In the following link, I reported in the yellow box these two DSUM functions and one more that function correctly.

example database where I show the mistakes


Solution

  • I cannot obtain the correct sum (25) of the yew trees because the function calculate 108.

    When there's an empty value in the `criteria` array, the function doesn't impose any restriction on the filtering. This means that a row of empty values matches the whole database, that's why you get 108 for the sum. (Ref. Database functions)

    The correct way to do this is by changing the third table to:

    Plant Field Expire Date
    Yew A <=2034-10-22
    Yew B <=2034-10-22

    And restricting the range in `criteria` to the non-empty rows:

    =DSUM(A1:D10;"Number";I16:K18)
    

    enter image description here

    But a better solution would be to change the table to:

    Plant Field Expire Date
    Yew <=2034-10-22

    And restrict the `criteria` range to the first row:

    =DSUM(A1:D10;"Number";I16:K17)
    

    enter image description here

    You could also use SUMIFS:

    =SUMIFS(B2:B10;A2:A10;I17;D2:D10;K17)