diff --git a/src/officecli/Core/PivotTableHelper.cs b/src/officecli/Core/PivotTableHelper.cs
index 9d199e2c5..3b7adb27a 100644
--- a/src/officecli/Core/PivotTableHelper.cs
+++ b/src/officecli/Core/PivotTableHelper.cs
@@ -4720,13 +4720,20 @@ private static OpenXmlElement BuildMultiRowItems(
int count = 0;
foreach (var (outer, inners) in groups)
{
- // Outer subtotal row:
+ // Outer subtotal row:
+ // ECMA-376 §18.10.1.44: r=0 requires fieldCount (2) children.
+ // Pad with the inner field's default item index.
var outerEntry = new RowItem();
var outerPivIdx = outerOrder[outer];
if (outerPivIdx == 0)
outerEntry.AppendChild(new MemberPropertyIndex());
else
outerEntry.AppendChild(new MemberPropertyIndex { Val = outerPivIdx });
+ var innerDefaultIdx = innerOrder.Count;
+ if (innerDefaultIdx == 0)
+ outerEntry.AppendChild(new MemberPropertyIndex());
+ else
+ outerEntry.AppendChild(new MemberPropertyIndex { Val = innerDefaultIdx });
container.AppendChild(outerEntry);
count++;
@@ -5036,6 +5043,20 @@ void Walk(AxisNode node)
if (idx == 0) item.AppendChild(new MemberPropertyIndex());
else item.AppendChild(new MemberPropertyIndex { Val = idx });
}
+ // ECMA-376 §18.10.1.44: each must have exactly
+ // (fieldCount - r) children. Subtotal entries have
+ // path.Length < fieldIndices.Count, so pad with the "default"
+ // item index for each remaining deeper field. The default item
+ // is appended after all value items by AppendFieldItems, so its
+ // 0-based index equals the unique value count for that level.
+ // Leaf entries already satisfy the requirement (path.Length ==
+ // fieldIndices.Count), so the loop is a no-op for them.
+ for (int i = path.Length; i < fieldIndices.Count; i++)
+ {
+ int defaultIdx = perLevelOrder[i].Count;
+ if (defaultIdx == 0) item.AppendChild(new MemberPropertyIndex());
+ else item.AppendChild(new MemberPropertyIndex { Val = defaultIdx });
+ }
// For col-axis leaves with K>1, append one extra for the
// first data field (index 0 = bare ). The K-1 subsequent
// entries below handle the remaining data fields.