Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
carsoncheng
Regular 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:

FacultyYearProgram codeQuota
A1B0000110
A1B0000220
A2B0000325
A3M0000130
B1B0000115
B2B0000220

 

I would like to generate a table of by-year totals of Quota under the records of each faculty:

FacultyYearProgram codeQuota
A1B0000110
A1B0000220
A2B0000325
A3M0000130
By-year1 30
 2 25
 3 30
 Total 85
    
B1B0000115
B2B0000220
By-year1 15
 2 20
 3 0
 Total 35
    
All1 45
 2 45
 3 30
 Total 120

 

Subtotal of pivot table does not work as a subtotal is put under each Year.  I'm familar with Power Query but I think it is not how it works.

 

I'm wondering if Power Pivot can do the work as it seems GROUPBY() or SUMMERIZECOLUMNS() is what I need.  But I'm not familar with DAX.

 

Please shed some light on me!  Oh, I'd like to stick with Excel. 

 

Thanks!! 

 

Regards,

Carson

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @carsoncheng, it is not ideal but possible also in PowerQuery:

 

Output

dufoq3_1-1739883591003.png

 

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

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

13 REPLIES 13
dufoq3
Super User
Super User

Hi @carsoncheng, it is not ideal but possible also in PowerQuery:

 

Output

dufoq3_1-1739883591003.png

 

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

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

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!

You're welcome 😉


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

v-pagayam-msft
Community Support
Community Support

Hi @carsoncheng ,
Thank you for connecting us on Microsoft Fabric Community Forum!

Thank you for the prompt response @Poojara_D12  and @Akash_Varuna , it helps to address the query.

I wanted to check and see if you had a chance to review our previous message or Please let us know if everything is sorted or if you need any further assistance.If our response addressed your query, please mark it as Accept as Solution and click Yes if you found it helpful.

Regards,
Pallavi.

Sorry, Pallavi, I don't see your previous message. And I haven't got a solution yet.

techies
Resolver III
Resolver III

Hi @carsoncheng have you tried using rollup function in dax? If you'd like, I can share a reference or example to help you get started.

No, I haven't.  I'm not familar with DAX functions so it will be helpful if you can share an example. Thanks a lot!

Hi @carsoncheng pls check this , here the ROLLUP function adds subtotals for each Year and Total for all product categories in the table.a.png

Thanks for the effort but it somehow does not work on my side. I guess DAX in Excel may be a bit different from that in Power BI.

 

(The field names are a bit different.)

carsoncheng_1-1739952091674.png

 

 

Anyway, thanks!

Poojara_D12
Super User
Super User

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.

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos"

Kind Regards,
Poojara - Proud to be a Super User
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS

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.

Akash_Varuna
Memorable Member
Memorable Member

Hi @carsoncheng , Could you try this please , After Loading the data please try these out in Power Query

  • Create Subtotals and Totals:

    • By-Year Subtotals: Duplicate the query → Group by Faculty and Year → Sum Quota → Add a column for "By-year."
    • Faculty Totals: Duplicate again → Group by Faculty → Sum Quota → Add a "Total" column.
    • Grand Total: Group the entire table (no grouping fields) → Sum Quota → Label as "All."
  • Combine Tables: Append the original data with these summaries.
    If this post helped please do give a kudos and accept this as a solution 
    Thanks In Advance

 

Thanks, @Akash_Varuna

 

The problem with using Power Query is that it is difficult to insert the subtotals of all years to each faculty.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.