List.Sum
The total of a list of numbers or durations, nulls left out.
Group: List. Power Query M has it too: a query that uses it is written as in Power BI.
Syntax
List.Sum(list as list, optional precision as nullable number) as any
| Parameter | Type | Description |
|---|---|---|
list | list | Numbers, or durations; nulls are skipped. |
precision | nullable number optional | Precision.Double (the default) adds as double-precision numbers; Precision.Decimal as decimals. |
Returns
any. A number for numbers, a duration for durations; null when the list has no value but null.
Remarks
- An empty list, or one of nulls only, sums to null, not 0. Wrap it in
if ... = null then 0 else ..., or useList.Sum(list) ?? 0, when you need a zero. - Numbers and durations do not mix: a list with both is an error.
- With
Precision.Decimaleach number is taken as the decimal Power Query shows for it (15 significant digits) and the total is exact before it becomes a number again: amounts such as 0.1 and 0.2 add up to 0.3.
Examples
An order's total
List.Sum({120, 80, null, 45.5})
245.5
Hours on a job
List.Sum({#duration(0, 1, 30, 0), #duration(0, 2, 15, 0)})
#duration(0, 3, 45, 0)
Amounts added as decimals
List.Sum({0.1, 0.2}, Precision.Decimal)
0.3
Without the precision the total is 0.30000000000000004.