{
  "category": "Statistical functions",
  "display_name": "AVERAGEIF",
  "entry_kind": "built_in_function",
  "eval_api": {
    "method": "POST",
    "url": "https://handbook.dnacalc.com/api/eval"
  },
  "function_id": "FUNC.AVERAGEIF",
  "implementation_state": {
    "answer_bank": null,
    "detail": "Recorded: 16 workbook cells.",
    "dnacalc_commit": "9e1884369a678158dd82d3d74188daf986995e87",
    "in_catalogue": true,
    "state": "matches",
    "state_label": "Matches every recorded Excel answer",
    "summary": "Matches all 16 recorded answers",
    "workbook_corpus": {
      "cells": 17,
      "matched": 16,
      "pending": 1
    }
  },
  "implementations": [
    {
      "commit": "9e1884369a678158dd82d3d74188daf986995e87",
      "flavour": "excel-compatibility-candidate",
      "language": "Rust",
      "name": "CalcFunc",
      "own_code": [
        "https://github.com/DnaCalc/DnaCalc/blob/9e1884369a678158dd82d3d74188daf986995e87/crates/calc-func/src/catalogue/conditional.rs#L169-L169",
        "https://github.com/DnaCalc/DnaCalc/blob/9e1884369a678158dd82d3d74188daf986995e87/crates/calc-func/src/catalogue/conditional.rs#L229-L239"
      ],
      "repository": "https://github.com/DnaCalc/DnaCalc",
      "shared_code": [
        "catalogue/aggregate::reader",
        "catalogue/aggregate::read_reference",
        "catalogue/comparison::numbers",
        "catalogue/comparison::in_band",
        "catalogue/comparison::Rounded::up",
        "catalogue/comparison::Roundings::of",
        "catalogue/comparison::Roundings::disjoint",
        "catalogue/comparison::texts",
        "catalogue/comparison/collation::compare",
        "catalogue/comparison/collation::weighed",
        "catalogue/comparison/collation::measured_iteration_extension",
        "catalogue/comparison/sort_key::table",
        "catalogue/comparison/sort_key::decode64",
        "catalogue/comparison/sort_key::Reader::byte",
        "catalogue/comparison/sort_key::Reader::varint",
        "catalogue/comparison/sort_key::Reader::done",
        "catalogue/comparison/sort_key::in_ranges",
        "catalogue/comparison/sort_key::build",
        "catalogue/comparison/sort_key::han_base",
        "catalogue/comparison/sort_key::derive",
        "catalogue/comparison/sort_key::shared_jamo_tails",
        "catalogue/comparison/sort_key::ordinary_jamo_prefix",
        "catalogue/comparison/sort_key::independent_jamo_suffix",
        "catalogue/comparison/sort_key::jamo_leading_primary",
        "catalogue/comparison/sort_key::leading_order",
        "catalogue/comparison/sort_key::Before::separate_special",
        "catalogue/comparison/sort_key::Gathered::kana",
        "catalogue/comparison/sort_key::Gathered::unit",
        "catalogue/comparison/sort_key::parts",
        "catalogue/comparison/sort_key::diacritic_order",
        "catalogue/comparison/sort_key::diacritics",
        "catalogue/conditional::conditional",
        "catalogue/conditional::total",
        "catalogue/conditional::mean",
        "catalogue/conditional::Range::shape",
        "catalogue/conditional::range",
        "catalogue/conditional::single",
        "catalogue/conditional::Plan::each_matched",
        "catalogue/conditional::row",
        "catalogue/conditional::offset",
        "catalogue/conditional::read_range",
        "catalogue/conditional::verdicts",
        "catalogue/conditional::target_cells",
        "catalogue/conditional::used",
        "catalogue/conditional::Match::of",
        "catalogue/conditional::Match::negated",
        "catalogue/conditional::Op::holds",
        "catalogue/conditional::Op::is_equality",
        "catalogue/conditional::spells",
        "catalogue/conditional::criterion",
        "catalogue/conditional::rank",
        "catalogue/conditional::criterion_text",
        "catalogue/conditional::decided",
        "catalogue/conditional::Criterion::verdict",
        "catalogue/conditional::equality",
        "catalogue/conditional::is_surrogate",
        "catalogue/conditional::wildcard",
        "decimal::low32",
        "decimal::Natural::from_u64",
        "decimal::Natural::pow10",
        "decimal::Natural::normalize",
        "decimal::Natural::is_zero",
        "decimal::Natural::mul_add_small",
        "decimal::Natural::times_pow10",
        "decimal::Natural::times_pow5",
        "decimal::Natural::times_pow2",
        "decimal::ExactDecimal::binary",
        "ported/text_kernels::text",
        "ported/text_kernels::text_from_str",
        "ported/text_kernels::fold_unit",
        "ported/text_kernels::push_singleton_or_original",
        "ported/text_kernels::push_lower",
        "ported/text_kernels::lower"
      ],
      "source": "https://github.com/DnaCalc/DnaCalc/blob/9e1884369a678158dd82d3d74188daf986995e87/crates/calc-func/src/catalogue/conditional.rs#L169-L169",
      "state": "matches",
      "suite": null,
      "summary": "Matches all 16 recorded answers"
    }
  ],
  "json_url": "https://handbook.dnacalc.com/api/functions/FUNC.AVERAGEIF.json",
  "name": "AVERAGEIF",
  "notes": [],
  "schema": "efh.function-reference/v1",
  "section_order": [
    "what-it-does",
    "try-it",
    "quirks",
    "how-to-implement",
    "version-differences",
    "alternatives"
  ],
  "sections": {
    "alternatives": "\n### Relationships\n\n- **[AVERAGEIFS](FUNC.AVERAGEIFS.md)** — the plural form. Not a superset: it requires exact\n  shape agreement, so it cannot express the anchoring shorthand, and its documentation states\n  the opposite logical rule. Argument order is also reversed — the averaged range comes first.\n- **`COUNTIF` / `SUMIF`** — the same criteria mechanism with a different reduction.\n  `AVERAGEIF` is mathematically `SUMIF/COUNTIF` **only** when every matched cell is numeric;\n  otherwise the divisors differ, because `COUNTIF` counts matched cells while `AVERAGEIF`\n  counts matched *numeric* cells.\n- **`MAXIFS` / `MINIFS`** — same module, same criteria mechanism, different reduction; note\n  they return `0` rather than an error when nothing matches, which `AVERAGEIF` does not.\n- **`DAVERAGE`** — the database-family conditional mean, with a criteria *range* rather than a\n  criteria expression; see [DAVERAGE](FUNC.DAVERAGE.md) for that mechanism.\n- **`FILTER` + `AVERAGE`** — the modern dynamic-array route, which uses ordinary worksheet\n  comparison rather than the criteria language and therefore does not inherit the wildcard or\n  the tolerant-numeric rules.\n\n\n",
    "how-to-implement": "\n### Numerical notes\n\n\nThe reduction is a plain sequential `f64` accumulation followed by one division, so the error\nanalysis is exactly [AVERAGE's](FUNC.AVERAGE.md#numerical-notes): a bound proportional to\n\\(\\sum|x_i|\\), a condition number of \\(\\sum|x_i|/|\\sum x_i|\\), and sensitivity to summation\norder. Nothing about the criteria stage improves it.\n\nWhat the criteria stage adds is a **second** numerical question that has nothing to do with\nsummation: the tolerant comparison. Because `compare_excel_numbers` works on a\n15-significant-digit truncation rather than the full 53-bit significand, the selection mask\nitself is a function of a rounding decision. Two numbers that differ in the sixteenth digit\nselect together. For a conditional average this is a bigger effect than any summation error:\na mis-selected cell changes the answer at full magnitude, while a summation error changes it in\nthe last bits. Anyone auditing an `AVERAGEIF` should look at the mask before looking at the\narithmetic.\n\n",
    "quirks": "\n### The criteria mechanism\n\n| Criterion form | Parsed as |\n|---|---|\n| `5` or a cell holding `5` | equality against the number 5 |\n| `TRUE` | equality against the logical TRUE |\n| `\">5\"` | greater-than against the number 5 |\n| `\"<>abc\"` | not-equal against the text `abc` |\n| `\"<=\"`, `\"<\"`, `\">\"`, `\">=\"`, `\"<>\"`, `\"=\"` prefixes | the corresponding comparison |\n| `\"apple\"` (no prefix) | equality against text |\n| `\"=\" `with nothing after it | equality against **blank** |\n| `\"a*\"`, `\"a?c\"` | text equality with wildcards |\n\n**Matching is kind-aware.** A numeric criterion matches numeric cells, and also text cells whose\ncontent parses as that number; it does not match logicals. A text criterion matches only text\ncells. A logical criterion matches logical cells, and text that reads as a logical. This means\n`AVERAGEIF(A:A, 1)` and `AVERAGEIF(A:A, TRUE)` select different cells even though `TRUE`\ncoerces to 1 in arithmetic.\n\n\n### The average_range anchoring rule\n\n\nMicrosoft states it directly: \"The average_range does not need matching dimensions as range;\ncalculations begin from the top-left cell and correspond to range dimensions.\"\n\nSo `AVERAGEIF(A1:A10, \">5\", B1)` averages `B1:B10`, not the single cell `B1`. The third\nargument behaves as an *anchor*, not as a range: only its top-left corner is load-bearing, and\nthe shape is taken from `range`.\n\nThis is a genuine trap, and it is the reason `AVERAGEIFS` — which requires exact shape\nagreement — is not simply \"`AVERAGEIF` with more criteria\". Moving a formula from one to the\nother can change which cells are averaged without changing any argument text.\n\n\n\n### Result and edge cases\n\n\nReturns `Number`.\n\n- **No matching cells** → `#DIV/0!`, documented by Microsoft: \"If no cells in the range meet\n  the criteria, AVERAGEIF returns the #DIV/0! error value.\"\n- **Empty cells in `average_range`** are ignored; Microsoft states this.\n- **`range` and `average_range` overlapping or identical** is ordinary and is the default.\n- **Arrays.** The surface lifts natively (`lift_broadcast_profile: surface_native`).\n\n\n\n### Documentation divergences\n\n\nThe Handbook exists partly to publish these. Three are visible on this surface without running\nExcel at all.\n\n3. **The `#DIV0!` spelling.** Microsoft's remark text renders the error as `#DIV0!` on both the\n   `AVERAGEIF` and `AVERAGEIFS` pages, while the same pages use `#DIV/0!` elsewhere. This is a\n   typographical defect in the documentation rather than a behavioural claim, but it is worth\n   recording so that a reader searching for the error code finds the remark.\n\n\n",
    "sources": "- Microsoft, \"AVERAGEIF function\" —\n  <https://support.microsoft.com/en-us/office/averageif-function-faec8e2e-0dec-4308-af69-f5576d8ac642>\n  (syntax and argument text; the ignored-logicals remark; ignored empty cells in\n  `average_range`; the blank-or-text `#DIV0!` remark; the empty-criteria-as-zero remark; the\n  no-matching-cells `#DIV/0!`; the wildcard set; and the top-left anchoring statement).\n- Microsoft, \"AVERAGEIFS function\" —\n  <https://support.microsoft.com/en-us/office/averageifs-function-48910c45-1fc0-4389-a028-f7c5c3001690>\n  (quoted here only for the contradicting TRUE-evaluates-as-1 remark and the same-size-and-shape\n  requirement).\n- Handbook projections `data/functions/FUNC.AVERAGEIF.json` and\n  `data/presence/FUNC.AVERAGEIF.json`.\n- Handbook, [AVERAGE](FUNC.AVERAGE.md) (the shared reduction and its error analysis),\n  [The value universe](../model/01-value-universe.md),\n  [Coercion and lifting](../model/02-coercion-and-lifting.md).\n- Handbook `CHARTER.md` sections 1, 3 and 7; `content/model/06-claim-language.md`.\n\n",
    "version-differences": "No comparison between specific Excel builds is recorded here yet. This does not establish that all builds behave alike.",
    "what-it-does": "\n`AVERAGEIF(range, criteria, [average_range])` builds a selection mask over `range` by testing\neach cell against `criteria`, and returns the arithmetic mean of the numeric cells of\n`average_range` at the selected positions:\n\n    M      = { i : criteria(range[i]) }\n    result = ( Σ_{i ∈ M, average_range[i] numeric} average_range[i] )\n             / #{ i ∈ M : average_range[i] numeric }\n\nTwo counts are in play and they are not the same. The mask counts *matching positions*; the\ndivisor counts *matching positions holding a number*. A matched cell that is empty, text, or\nlogical contributes to neither numerator nor divisor. That is why `AVERAGEIF` can return\n`#DIV/0!` on a range where the criterion matched plenty of cells.\n\nThe function decomposes cleanly into three independent stages, and every difficult question\nabout it belongs to exactly one of them:\n\n1. **Criteria parsing** — turning the `criteria` argument into a comparison operator plus an\n   operand.\n2. **Cell matching** — applying that comparison to each cell of `range`.\n3. **Filtered reduction** — averaging the selected cells of `average_range`.\n\n\n### Arguments\n\n\n| Argument | Meaning | Default |\n|---|---|---|\n| `range` | The cells the criterion is tested against. Required. | — |\n| `criteria` | The test. A number, expression, cell reference, or text. Required. | — |\n| `average_range` | The cells actually averaged. Optional. | `range` |\n\nMicrosoft describes `range` as \"One or more cells to average, including numbers or names,\narrays, or references that contain numbers\", `criteria` as \"The criteria in the form of a\nnumber, expression, cell reference, or text that defines which cells are averaged\", and\n`average_range` as \"The actual set of cells to average. If omitted, range is used.\"\n\n"
  },
  "signature": {
    "display": "AVERAGEIF(range, criteria, [average_range])",
    "parameters": [
      {
        "description": null,
        "name": "range",
        "optional": false,
        "repeats": false
      },
      {
        "description": null,
        "name": "criteria",
        "optional": false,
        "repeats": false
      },
      {
        "description": null,
        "name": "average_range",
        "optional": true,
        "repeats": false
      }
    ],
    "trailing_repeats": false
  },
  "sources": {
    "metadata": {
      "admission": {
        "label": "implemented",
        "reason": null
      },
      "arity": {
        "max": 3,
        "min": 2
      },
      "category": "Statistical functions",
      "classification": {
        "arg_preparation_profile": "RefsVisibleInAdapter",
        "coercion_lift_profile": "Custom",
        "determinism_class": "Deterministic",
        "error_collapse_profile": "ReductionFold",
        "fec_dependency_profile": "RefOnly",
        "host_interaction_class": "None",
        "kernel_signature_class": "Custom",
        "rich_value_usage": "RichBlind",
        "surface_fec_dependency_profile": "RefOnly",
        "thread_safety_class": "SafePure",
        "volatility_class": "NonVolatile"
      },
      "descriptions": {
        "long": null,
        "short": "Mean of the cells that meet one condition",
        "short_source": "dna-calc-own"
      },
      "docs": {
        "english_description": "Returns the average (arithmetic mean) of all the cells in a range that meet a given criteria",
        "microsoft_url": "https://support.microsoft.com/en-us/office/averageif-function-faec8e2e-0dec-4308-af69-f5576d8ac642"
      },
      "entry_kind": "built_in_function",
      "localized_family_names": null,
      "localized_names": [
        {
          "locale": "ar-SA",
          "name": "AVERAGEIF"
        },
        {
          "locale": "bg-BG",
          "name": "AVERAGEIF"
        },
        {
          "locale": "cs-CZ",
          "name": "AVERAGEIF"
        },
        {
          "locale": "da-DK",
          "name": "MIDDEL.HVIS"
        },
        {
          "locale": "de-DE",
          "name": "AVERAGEIF"
        },
        {
          "locale": "el-GR",
          "name": "AVERAGEIF"
        },
        {
          "locale": "en-AU",
          "name": "AVERAGEIF"
        },
        {
          "locale": "en-GB",
          "name": "AVERAGEIF"
        },
        {
          "locale": "en-US",
          "name": "AVERAGEIF"
        },
        {
          "locale": "es-ES",
          "name": "PROMEDIO.SI"
        },
        {
          "locale": "et-EE",
          "name": "AVERAGEIF"
        },
        {
          "locale": "fi-FI",
          "name": "KESKIARVO.JOS"
        },
        {
          "locale": "fr-FR",
          "name": "MOYENNE.SI*"
        },
        {
          "locale": "he-IL",
          "name": "AVERAGEIF"
        },
        {
          "locale": "hr-HR",
          "name": "AVERAGEIF"
        },
        {
          "locale": "hu-HU",
          "name": "ÁTLAGHA"
        },
        {
          "locale": "id-ID",
          "name": "AVERAGEIF"
        },
        {
          "locale": "it-IT",
          "name": "AVERAGEIF"
        },
        {
          "locale": "ja-JP",
          "name": "AVERAGEIF"
        },
        {
          "locale": "ko-KR",
          "name": "AVERAGEIF"
        },
        {
          "locale": "lt-LT",
          "name": "AVERAGEIF"
        },
        {
          "locale": "lv-LV",
          "name": "AVERAGEIF"
        },
        {
          "locale": "nb-NO",
          "name": "GJENNOMSNITTHVIS"
        },
        {
          "locale": "nl-NL",
          "name": "AVERAGEIF"
        },
        {
          "locale": "pl-PL",
          "name": "ŚREDNIA.JEŻELI"
        },
        {
          "locale": "pt-BR",
          "name": "MÉDIAIF"
        },
        {
          "locale": "pt-PT",
          "name": "MÉDIA.SE"
        },
        {
          "locale": "ro-RO",
          "name": "AVERAGEIF"
        },
        {
          "locale": "ru-RU",
          "name": "СРЗНАЧЕСЛИ"
        },
        {
          "locale": "sk-SK",
          "name": "AVERAGEIF"
        },
        {
          "locale": "sl-SI",
          "name": "AVERAGEIF"
        },
        {
          "locale": "sr-Latn-RS",
          "name": "AVERAGEIF"
        },
        {
          "locale": "sv-SE",
          "name": "MEDEL.OM"
        },
        {
          "locale": "th-TH",
          "name": "AVERAGEIF"
        },
        {
          "locale": "tr-TR",
          "name": "EĞERORTALAMA işlevi"
        },
        {
          "locale": "uk-UA",
          "name": "AVERAGEIF"
        },
        {
          "locale": "vi-VN",
          "name": "AVERAGEIF"
        },
        {
          "locale": "zh-CN",
          "name": "AVERAGEIF"
        },
        {
          "locale": "zh-HK",
          "name": "AVERAGEIF"
        },
        {
          "locale": "zh-TW",
          "name": "AVERAGEIF"
        }
      ],
      "signature": {
        "display": "AVERAGEIF(range, criteria, [average_range])",
        "parameters": [
          {
            "description": null,
            "name": "range",
            "optional": false,
            "repeats": false
          },
          {
            "description": null,
            "name": "criteria",
            "optional": false,
            "repeats": false
          },
          {
            "description": null,
            "name": "average_range",
            "optional": true,
            "repeats": false
          }
        ],
        "trailing_repeats": false
      },
      "surface_name": "AVERAGEIF",
      "version_marker": null,
      "xlcall": {
        "code": "483",
        "symbol": "xlfAverageif"
      }
    },
    "microsoft": "https://support.microsoft.com/en-us/office/averageif-function-faec8e2e-0dec-4308-af69-f5576d8ac642"
  },
  "try_it": {
    "formula": "=AVERAGEIF(B2:B4,\">1\")",
    "inputs": {
      "B2": 1,
      "B3": 2,
      "B4": 3
    }
  },
  "url": "https://handbook.dnacalc.com/functions/AVERAGEIF.html",
  "variants": [],
  "versions": []
}