Forum Discussion
carsoncheng
1 year agoRegular Visitor
Summary under subtotal
Hi, (Sorry if I post this to the wrong forum but I couldn't find a proper one.) I have these columns in my (simplified) data table in Excel: Faculty Year Program code Quota A 1 B0...
- 1 year ago
Hi carsoncheng, it is not ideal but possible also in PowerQuery:
Output
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYicDIAAxDA2UYnXQxI2ADCOEuBFM3BjEMYWLg7i+MHOMIeqdMMw3hYsboZsfCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Faculty = _t, Year = _t, #"Program code" = _t, Quota = _t]), ChangedType = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Quota", type number}}), GroupedRows = Table.Group(ChangedType, {"Faculty"}, {{"T", each [ A = _, // A = GroupedRows{[Faculty="A"]}[T], SelectedCols = Table.Buffer(Table.SelectColumns(A,{"Year", "Quota"})), Ad_Totals = List.Accumulate(let lst = List.Distinct(SelectedCols[Year]) in lst & {lst} , A, (s,c)=> if c is number then Table.InsertRows(s, Table.RowCount(s), { SelectedCols{0} & [Faculty=if c = SelectedCols{0}[Year] then "By-year" else null, Year=c, Program code=null, Quota=List.Sum(Table.SelectRows(SelectedCols, each [Year] = c)[Quota])] }) else Table.InsertRows(s, Table.RowCount(s), { SelectedCols{0} & [Faculty=null, Year="Total", Program code=null, Quota=List.Sum(Table.SelectRows(SelectedCols, each List.Contains(c, [Year]))[Quota])] })), Ad_SpaceRow = Table.InsertRows(Ad_Totals, Table.RowCount(Ad_Totals), { List.Accumulate(Table.ColumnNames(Ad_Totals), [], (s,c)=> Record.AddField(s, c, null)) } ) ][Ad_SpaceRow], type table}}), CombinedT = Table.Combine(GroupedRows[T]), SelectedCols2 = Table.Buffer(Table.SelectRows(CombinedT, each [Program code] = null and [Year] <> null)[[Year], [Quota]]), Ad_Total = List.Accumulate(List.RemoveNulls(List.Distinct(SelectedCols2[Year])), CombinedT, (s,c)=> Table.InsertRows(s, Table.RowCount(s), { SelectedCols2{0} & [Faculty=if c = SelectedCols2{0}[Year] then "All" else null, Year=c, Program code=null, Quota=List.Sum(Table.SelectRows(SelectedCols2, each [Year] = c)[Quota])] })) in Ad_Total
carsoncheng
1 year agoRegular Visitor
Thanks a ton, dufoq3 !! Your answer is eye-opening. I thought I was familar with Power Query. Thanks again for showing me the real power of Power Query!
dufoq3
1 year agoCommunity Champion
You're welcome 😉