Agreed, we can use PivotTables for this. We are planning to automate the process, which is why we need VBA code. However, we can't use too many PivotTables in the Excel sheet.
x = [LET(a,DROP(PIVOTBY(Table1[[Parent Name]:[Child Name]],,Table1[Amt],SUM,0,-2),1),HSTACK(IF(CHOOSECOLS(a,2)="",CHOOSECOLS(a,1),CHOOSECOLS(a,2)),CHOOSECOLS(a,3)))]
Range("T2").Resize(UBound(x), UBound(x, 2)).Value = x
Table1 is the name of my source data table; adjust the code to match your table's name.
or
Code:
With Range("I2")
.Formula2R1C1 = "=LET(a,DROP(PIVOTBY(Table1a[[Parent Name]:[Child Name]],,Table1a[Amt],SUM,0,-2),1),HSTACK(IF(CHOOSECOLS(a,2)="""",CHOOSECOLS(a,1),CHOOSECOLS(a,2)),CHOOSECOLS(a,3)))"
.SpillingToRange.Value = .SpillingToRange.Value
End With