Skip to content

database_formulas

shortfx.fxExcel.database_formulas

Excel Database Functions Module.

This module provides Excel-compatible database functions that operate on structured data (list of dictionaries). Functions include: - DCOUNT, DCOUNTA: Count functions - DAVERAGE, DSUM: Aggregate functions - DMAX, DMIN: Statistical functions - DSTDEV, DSTDEVP, DVAR, DVARP: Variance and standard deviation - DPRODUCT: Product calculation - DGET: Single record extraction

All functions follow Excel naming conventions and behavior.

Functions

DAVERAGE(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float

Calculate average of database field values matching criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to average.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Name Type Description
float float

Average value, 0 if no values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] DAVERAGE(db, 'Value', None) 20.0

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DAVERAGE(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float:
    """
    Calculate average of database field values matching criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to average.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        float: Average value, 0 if no values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> DAVERAGE(db, 'Value', None)
        20.0

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    return sum(values) / len(values) if values else 0

DCOUNT(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> int

Count cells containing numbers in a database field that match criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to count.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Name Type Description
int int

Count of numeric values matching criteria.

Example

db = [{'Name': 'Alice', 'Age': 30}, {'Name': 'Bob', 'Age': 25}] DCOUNT(db, 'Age', None) 2

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DCOUNT(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> int:
    """
    Count cells containing numbers in a database field that match criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to count.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        int: Count of numeric values matching criteria.

    Example:
        >>> db = [{'Name': 'Alice', 'Age': 30}, {'Name': 'Bob', 'Age': 25}]
        >>> DCOUNT(db, 'Age', None)
        2

    Cost: O(n) where n is the number of records.
    """
    return len(_get_db_values(database, field, criteria))

DCOUNTA(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> int

Count non-empty cells in a database field that match criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to count.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Name Type Description
int int

Count of non-empty values matching criteria.

Example

db = [{'Name': 'Alice', 'Age': 30}, {'Name': 'Bob', 'Age': None}] DCOUNTA(db, 'Age', None) 1

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DCOUNTA(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> int:
    """
    Count non-empty cells in a database field that match criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to count.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        int: Count of non-empty values matching criteria.

    Example:
        >>> db = [{'Name': 'Alice', 'Age': 30}, {'Name': 'Bob', 'Age': None}]
        >>> DCOUNTA(db, 'Age', None)
        1

    Cost: O(n) where n is the number of records.
    """
    count = 0
    for record in database:
        if criteria is None or _evaluate_criteria(record, criteria):
            if field in record and record[field] is not None:
                count += 1
    return count

DGET(database: List[Dict[str, Any]], field: str, criteria: Dict[str, Any]) -> Any

Extract a single value from a database field that matches criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to extract.

required
criteria Dict[str, Any]

Dictionary with filtering criteria.

required

Returns:

Name Type Description
Any Any

Field value from the matching record, None if not found.

Raises:

Type Description
ValueError

If multiple records match criteria (Excel behavior).

Example

db = [{'Name': 'Alice', 'Age': 30}, {'Name': 'Bob', 'Age': 25}] DGET(db, 'Age', {'Name': 'Alice'}) 30

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DGET(database: List[Dict[str, Any]], field: str, criteria: Dict[str, Any]) -> Any:
    """
    Extract a single value from a database field that matches criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to extract.
        criteria: Dictionary with filtering criteria.

    Returns:
        Any: Field value from the matching record, None if not found.

    Raises:
        ValueError: If multiple records match criteria (Excel behavior).

    Example:
        >>> db = [{'Name': 'Alice', 'Age': 30}, {'Name': 'Bob', 'Age': 25}]
        >>> DGET(db, 'Age', {'Name': 'Alice'})
        30

    Cost: O(n) where n is the number of records.
    """
    for record in database:
        if _evaluate_criteria(record, criteria):
            return record[field] if field in record else None
    return None

DMAX(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Optional[Union[int, float]]

Return maximum value from database field values matching criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to analyze.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Type Description
Optional[Union[int, float]]

Union[int, float, None]: Maximum value, None if no values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] DMAX(db, 'Value', None) 30

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DMAX(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Optional[Union[int, float]]:
    """
    Return maximum value from database field values matching criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to analyze.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        Union[int, float, None]: Maximum value, None if no values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> DMAX(db, 'Value', None)
        30

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    return max(values) if values else None

DMIN(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Optional[Union[int, float]]

Return minimum value from database field values matching criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to analyze.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Type Description
Optional[Union[int, float]]

Union[int, float, None]: Minimum value, None if no values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] DMIN(db, 'Value', None) 10

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DMIN(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Optional[Union[int, float]]:
    """
    Return minimum value from database field values matching criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to analyze.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        Union[int, float, None]: Minimum value, None if no values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> DMIN(db, 'Value', None)
        10

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    return min(values) if values else None

DPRODUCT(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Union[int, float]

Multiply values from database field that match criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to multiply.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Type Description
Union[int, float]

Union[int, float]: Product of values, 0 if no values.

Example

db = [{'Value': 2}, {'Value': 3}, {'Value': 4}] DPRODUCT(db, 'Value', None) 24

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DPRODUCT(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Union[int, float]:
    """
    Multiply values from database field that match criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to multiply.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        Union[int, float]: Product of values, 0 if no values.

    Example:
        >>> db = [{'Value': 2}, {'Value': 3}, {'Value': 4}]
        >>> DPRODUCT(db, 'Value', None)
        24

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    product = 1
    for v in values:
        product *= v
    return product if values else 0

DSTDEV(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float

Calculate sample standard deviation of database field values matching criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to analyze.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Name Type Description
float float

Sample standard deviation, 0 if less than 2 values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] DSTDEV(db, 'Value', None) 10.0

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DSTDEV(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float:
    """
    Calculate sample standard deviation of database field values matching criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to analyze.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        float: Sample standard deviation, 0 if less than 2 values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> DSTDEV(db, 'Value', None)
        10.0

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    return statistics.stdev(values) if len(values) > 1 else 0

DSTDEVP(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float

Calculate population standard deviation of database field values matching criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to analyze.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Name Type Description
float float

Population standard deviation, 0 if no values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] round(DSTDEVP(db, 'Value', None), 2) 8.16

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DSTDEVP(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float:
    """
    Calculate population standard deviation of database field values matching criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to analyze.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        float: Population standard deviation, 0 if no values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> round(DSTDEVP(db, 'Value', None), 2)
        8.16

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    return statistics.pstdev(values) if values else 0

DSUM(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Union[int, float]

Sum values from database field that match criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to sum.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Type Description
Union[int, float]

Union[int, float]: Sum of values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] DSUM(db, 'Value', None) 60

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DSUM(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> Union[int, float]:
    """
    Sum values from database field that match criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to sum.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        Union[int, float]: Sum of values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> DSUM(db, 'Value', None)
        60

    Cost: O(n) where n is the number of records.
    """
    return sum(_get_db_values(database, field, criteria))

DVAR(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float

Calculate sample variance of database field values matching criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to analyze.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Name Type Description
float float

Sample variance, 0 if less than 2 values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] DVAR(db, 'Value', None) 100.0

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DVAR(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float:
    """
    Calculate sample variance of database field values matching criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to analyze.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        float: Sample variance, 0 if less than 2 values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> DVAR(db, 'Value', None)
        100.0

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    return statistics.variance(values) if len(values) > 1 else 0

DVARP(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float

Calculate population variance of database field values matching criteria.

Parameters:

Name Type Description Default
database List[Dict[str, Any]]

List of dictionaries representing database records.

required
field str

Field name to analyze.

required
criteria Optional[Dict[str, Any]]

Optional dictionary with filtering criteria.

None

Returns:

Name Type Description
float float

Population variance, 0 if no values.

Example

db = [{'Value': 10}, {'Value': 20}, {'Value': 30}] round(DVARP(db, 'Value', None), 2) 66.67

Cost: O(n) where n is the number of records.

Source code in shortfx/fxExcel/database_formulas.py
def DVARP(database: List[Dict[str, Any]], field: str, criteria: Optional[Dict[str, Any]] = None) -> float:
    """
    Calculate population variance of database field values matching criteria.

    Args:
        database: List of dictionaries representing database records.
        field: Field name to analyze.
        criteria: Optional dictionary with filtering criteria.

    Returns:
        float: Population variance, 0 if no values.

    Example:
        >>> db = [{'Value': 10}, {'Value': 20}, {'Value': 30}]
        >>> round(DVARP(db, 'Value', None), 2)
        66.67

    Cost: O(n) where n is the number of records.
    """
    values = _get_db_values(database, field, criteria)
    return statistics.pvariance(values) if values else 0