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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
IK181
New Member

Power BI Report Builder - Total of Top N by Group Filtering in Matrix

Hi All, 

 

Hope all is well.

I am working on a paginated report which contains a matrix made of

 two Row groups having

  1. Main Category as a Parent Group and
  2. Area as Child Group

and have a 1 column group of Report Date with a column named "current date" & "previous date".

The resultant needs to be top 10 Area which was easy to achieve but the issue is unable to calculate the Grand total of top 10 returning Areas.

 

Would really appreciate if anyone can assist me in this regard and let me know how to calculate the Grand Total of Top 10 Areas in a matrix.

3 REPLIES 3
Anonymous
Not applicable

Please assist me if possible, I'll be grateful to you.

Anonymous
Not applicable

Thank you for the assistance, Sahir. 
However, the given solution isn't returning the filtered rows total.

I have tried your solution for Top 2 rows, keeping everthing quite simple.

IK_181_2-1691017042473.png


This is the design view: I have chosen Branch Banking in the Main Category group only.

IK_181_3-1691017474793.png

This is the resultant view: Sum should be 7 instead of 9.

IK_181_1-1691016863286.png

 

Sahir_Maharaj
Super User
Super User

Hello @IK181,

 

Can you please try the following:

 

1. Define a custom code function in the report:

Dim TotalTop10Areas As Integer = 0

Public Function AddToTotalTop10Areas(ByVal value As Integer) As Integer
    TotalTop10Areas = TotalTop10Areas + value
    Return value
End Function

Public Function GetGrandTotalTop10Areas() As Integer
    Return TotalTop10Areas
End Function

2. Add a textbox to your report where you want to display the grand total. In the Value property of the textbox:

=Code.GetGrandTotalTop10Areas()

3. In the cell of the matrix where you want to calculate the top 10 areas, add a custom expression to handle the top 10 calculation

=Code.AddToTotalTop10Areas(Sum(Fields!ValueToSum.Value))

4. Sort the matrix rows by the sum of the values in descending order.

 

Should you need further assistance please don't hesitate to reach out to me.


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,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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