Well we must be getting closer… So by the looks of it all Parents fold to the top level.
See what you make of this.
let
Source = Excel.CurrentWorkbook(){[Name="Movements"]}[Content],
#"Changed Type" = Table.Buffer(Table.TransformColumnTypes(Source,{{"Account ID", Int64.Type}, {"Debit", type number}, {"Credit", type number}, {"Liquid", type number}})),
GroupAccountID = Table.Group(#"Changed Type", {"Account ID"}, {{"Debit", each List.Sum([Debit]), type nullable number}, {"Credit", each List.Sum([Credit]), type nullable number}, {"Liquid", each List.Sum([Liquid]), type nullable number}}),
MergeTOA = Table.NestedJoin(GroupAccountID, {"Account ID"}, #"Table of Accounts", {"Account ID"}, "Temp", JoinKind.LeftOuter),
ChildLevel = Table.Buffer( Table.ExpandTableColumn(MergeTOA, "Temp", {"Parent"}, {"Parent"})),
AddTopParentChild = Table.AddColumn(ChildLevel, "TopParent", each Number.From( Text.Start( Text.From([Account ID]), 2)), type number ),
TopParentLevel = Table.Buffer( Table.Group(AddTopParentChild, {"TopParent"}, {{"Debit", each List.Sum([Debit]), type nullable number}, {"Credit", each List.Sum([Credit]), type nullable number}, {"Liquid", each List.Sum([Liquid]), type nullable number}})),
SartWithTOA = #"Table of Accounts",
AddTopParent = Table.AddColumn(SartWithTOA, "TopParent", each if [AccountType] <> "M" then Number.From( Text.Start( Text.From([Account ID]), 2)) else null, type number ),
MergeChildren = Table.NestedJoin(AddTopParent, {"Account ID"}, AddTopParentChild, {"Account ID"}, "Temp", JoinKind.LeftOuter),
ExpandChildren = Table.ExpandTableColumn(MergeChildren, "Temp", {"Credit", "Debit", "Liquid"}, {"Credit", "Debit", "Liquid"}),
MergeParent = Table.NestedJoin(ExpandChildren, {"TopParent"}, TopParentLevel, {"TopParent"}, "Temp", JoinKind.LeftOuter),
ExpandParent = Table.ExpandTableColumn(MergeParent, "Temp", {"Debit", "Credit", "Liquid"}, {"Debit.1", "Credit.1", "Liquid.1"}),
UnpivotOthers = Table.UnpivotOtherColumns(ExpandParent, {"Account ID", "AccountType", "Parent", "TopParent"}, "Attribute", "Value"),
ExtractHeaders = Table.TransformColumns(UnpivotOthers, {{"Attribute", each Text.BeforeDelimiter(_, "."), type text}}),
RemoveColumns = Table.RemoveColumns(ExtractHeaders,{"TopParent"}),
PivotBack = Table.Pivot(RemoveColumns, List.Distinct(RemoveColumns[Attribute]), "Attribute", "Value", List.Sum)
in
PivotBack
.
Filtered snapshot
Here’s the updated file: account resume (1).xlsx (164.5 KB)