Skip to content

Data Subtotal: the grand total ends at the last data row, as in Excel - #4414

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/subtotal-grand-total-range
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/subtotal-grand-total-range

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4268

What was wrong

subtotal in crates/engine/src/cmd/data/grouping.rs wrote the grand total's SUBTOTAL down to the row above it, which is the last group's own total: =SUBTOTAL(9,B2:B6). Excel writes =SUBTOTAL(9,B2:B5), ending at the last group's last data row.

SUBTOTAL skips nested SUBTOTALs, so the two agree until the last group's total is replaced by a value. Then GridCraft's grand total counted it (160) and Excel's didn't (60).

What changed

The grand total's range ends at last - 2, the last data row, instead of last - 1. Group totals, labels, bold and the outline are unchanged. The Subtotal dialog (ui-egui/src/dialogs.rs) runs the same command.

QA report

Main vs branch, end to end (gridcraft-cli run --script)

This is the issue's JSONL, extended with its two other replacements (=50+50 over B6, then 100 over B4) and a cell.get of B7 after each step. main is a CLI built from origin/main (d60709a1) and the branch is this PR (7f277ad6). cmp confirmed the two binaries differ.

Step main B7 this PR B7 Excel (issue)
after Subtotal =SUBTOTAL(9,B2:B6) → 60 =SUBTOTAL(9,B2:B5) → 60 =SUBTOTAL(9,B2:B5) → 60
B6 (B's total) = 100 160 60 60
B6 = =50+50 160 60 60
then B4 (A's total) = 100 260 160 160 from the start, in both apps

The saved actual.xlsx has <f>SUBTOTAL(9,B2:B6)</f> in B7 from main and <f>SUBTOTAL(9,B2:B5)</f> from this PR.

Test

cmd::data::tests::subtotal_grand_total_ends_at_the_last_data_row runs the issue's range with Sum, Count and Max. For each it checks:

  • the formulas in B4, B6 and B7 (B2:B3, B5, B2:B5) and the grand total;
  • the grand total after each of the three replacements: Sum 60, 60, 60, 160; Count 3, 3, 3, 4; Max 30, 30, 30, 100.

The existing subtotal tests still pass, including the grand total over every data row.

Negative control

With the grand total's range back to last - 1 (on top of 7f277ad6, then restored with git checkout), the test fails at its first function: sum, left "=SUBTOTAL(9,B2:B6)", right "=SUBTOTAL(9,B2:B5)".

Gate (macOS arm64, at 7f277ad6)

Step Result
cargo fmt --all -- --check clean
cargo clippy -p gridcraft-engine --all-targets -- -D warnings clean
cargo test -p gridcraft-engine 1005 (+1 ignored) + 120 + 7 passed, 0 failed

Not covered

Summary by CodeRabbit

  • Bug Fixes
    • Corrected grand-total subtotal calculations so they exclude the final group’s subtotal row and use the intended data range.
    • Grand totals now remain accurate when the final group’s subtotal is replaced with a value or formula, while changes to earlier group subtotals are still reflected.

The grand total's SUBTOTAL ran to the row above it, the last group's total,
as =SUBTOTAL(9,B2:B6). Excel writes =SUBTOTAL(9,B2:B5), ending at the last
group's last data row. A value typed over the last group's total then reached
GridCraft's grand total (160) and not Excel's (60), while one typed over any
other group's total reaches both.

Fixes storytold#4268
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The subtotal calculation now excludes the final group subtotal from the grand-total range. A regression test covers sum, count, and max, and checks how replacing group subtotals affects the grand total.

Changes: Subtotal Grand-Total Range

Layer / File(s) Summary
Grand-total range and regression coverage
crates/engine/src/cmd/data/grouping.rs, crates/engine/src/cmd/data/tests.rs
The grand-total formula range now ends at last - 2. A regression test checks the formula range and verifies that replacing the final group subtotal does not change the grand total, while replacing an earlier subtotal does. The test covers sum, count, and max.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: aiongg, zidan44

Merge Risk: Minimal · up to 7f277

The grand-total formula now ends at the last data row, so replacing the final group subtotal with a value no longer changes the grand total. The change is small and covered by a regression test. No remaining merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main behavior change: the grand-total SUBTOTAL range now ends at the last data row, matching Excel. The raw summary and PR objectives both confirm that the grouping.rs range chang…
Linked Issues check Passed Issue #4268 requires that the grand-total SUBTOTAL generated by Data Subtotal not include the final group's total row. The reviewed head (7f277ad) changes the grand-total range in subtotal (crates/…
Out of Scope Changes check Passed The PR changes two areas. In grouping.rs, the change is limited to the grand-total range and its explanatory comment, which implements #4268 directly. The test file adds one regression test for the sa…
Full details: Title check

Explanation

The title states the main behavior change: the grand-total SUBTOTAL range now ends at the last data row, matching Excel. The raw summary and PR objectives both confirm that the grouping.rs range change (last - 2 instead of last - 1) is the primary change, and the regression test covers the same behavior. The title is specific, has no file list or vague wording, and is a single sentence. It is slightly long at 69 characters, but it is still clear and readable.

Full details: Linked Issues check

Explanation

Issue #4268 requires that the grand-total SUBTOTAL generated by Data Subtotal not include the final group's total row. The reviewed head (7f277ad) changes the grand-total range in subtotal (crates/engine/src/cmd/data/grouping.rs) from last - 1 to last - 2. The row layout gives last as the grand-total row and last - 1 as the final group's total row, so last - 2 is the final group's last data row. For the issue's A1:B4 example this yields =SUBTOTAL(9,B2:B5), which matches the expected formula. Replacing B6 no longer changes B7. The change also keeps group totals, labels, bold and the outline unchanged. The added test cmd::data::tests::subtotal_grand_total_ends_at_the_last_data_row covers Sum, Count and Max, the formulas in B4, B6 and B7, and the grand total after each replacement step. The PR reports that the test fails when the range returns to last - 1. I did not read the test file or run the tests. The result relies on the reported test results and the code reading of the range arithmetic. The issue's Excel comparison and #3790 (running Subtotal again) are not coding requirements of this PR.

Full details: Out of Scope Changes check

Explanation

The PR changes two areas. In grouping.rs, the change is limited to the grand-total range and its explanatory comment, which implements #4268 directly. The test file adds one regression test for the same behavior. The PR does not change the group total logic, labels, bold, outline, or the Subtotal dialog path, which uses the same command. The PR description states that existing workbook formulas and #3790 are not covered. These exclusions are not changes in this PR. I found no unrelated changes in the evidence available.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d60709a and 7f277ad.

Files selected for processing: 2

  • crates/engine/src/cmd/data/grouping.rs
  • crates/engine/src/cmd/data/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data Subtotal grand total includes a replacement value in the final group summary

1 participant