community postAL Developmentcommunity - not Microsoftreviewed - checked by Opus

Dynamics 365 Business Central: How to calculate the total (sum) of a column in a table (Four ways)

The post explains four methods to calculate column totals in Business Central: using AL loops (for, foreach, while, repeat-until), the Record.CalcSums method, Query objects with aggregation, and FlowFields. Each approach has different use cases and performance characteristics for developers calculating sums of data.

post Dynamics 365 Lab

Yun Zhu · 2025-05-30 · 1,561 words · about 7 min

"The CalcSums function is used to calculate a total for a specific field, based on the filters of the dataset."

Key points

  • AL loops such as repeat-until work in every situation but are not the most efficient way to total a column
  • CalcSums totals a field using current filters, is fastest with matching SumIndexFields keys, and does not accept FlowFields
  • Query objects with the Sum method, optionally grouped by Year, are recommended for frequent calculations at the cost of an extra object
  • FlowFields defined with CalcFormula show totals on pages without extra code but need a new field in a table

Quotes

  • “The CalcSums function is used to calculate a total for a specific field, based on the filters of the dataset.” (Explains the core purpose and mechanism of the CalcSums method, the most efficient approach for total calculations)
  • “This is very useful for displaying totals on a page, as there is no need to add additional logic, just the properties.” (Highlights the key advantage of FlowFields: automatic calculation without code, reducing development effort)
  • “If frequent calculations are required, this method is recommended.” (Identifies when Query objects should be used, guiding developers to choose the right approach for their scenario)

AL objects mentioned

As named in the post. A name that matches one object page by exact type and name links to it; the others stay as named.

Not found in BC28-30: query “CustLedgerEntriesGroupByYear”.

No source states these links: they share an object, a model read them, or a person found the same subject.

Heads up

  • warning: “But please note that this method does not support Flowfield.”

Context

  • Features: record loops, CalcSums method, Query aggregation, FlowFields, Sum calculations, Date grouping in queries, SumIndexFields
  • Versions: 2025 wave 1

Source: Dynamics 365 Lab - Yun Zhu, community blog. Summary, key points and quotes are derived (CONTENT-NOTICE.md); read the original for the full text.