Forum Discussion
Summary under subtotal
- 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
Hi carsoncheng
To achieve your desired table structure in Excel, Power Pivot with DAX is the best approach. First, you need to load your data into Power Pivot by adding it to the Data Model. Once inside Power Pivot, create a measure named TotalQuota using SUM(Data[Quota]) to sum the Quota column. Then, insert a Pivot Table from Power Pivot and place Faculty and Year in the Rows section while placing TotalQuota in the Values section. To generate by-year totals within each faculty, define another measure called ByYearQuota using IF(HASONEVALUE(Data[Year]), [TotalQuota], CALCULATE([TotalQuota], ALLEXCEPT(Data, Data[Year]))), ensuring that subtotals correctly roll up within each faculty while maintaining individual year-wise quotas. Additionally, to compute the total quota across all faculties, define another measure, GrandTotalQuota = CALCULATE([TotalQuota], ALL(Data[Faculty])), ensuring an overall summary at the bottom. Finally, format the Pivot Table by enabling Subtotals at the bottom and Grand Totals for both rows and columns under the Pivot Table Design options. This setup allows Power Pivot to display faculty-specific quotas, year-wise subtotals, and grand totals in the correct format, overcoming the limitations of default Pivot Table subtotals.
- carsoncheng1 year agoRegular Visitor
Thanks a lot, Poojara!
I added the three measures successfully, but I have no idea how I can insert the subtotals to the end of each Faculty. I'd like to stress that I'm using Power Pivot in Excel, not Power BI.
I would appreciate if you could explain a bit more.