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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
AndreasHermle
Regular Visitor

Calculated Table Dax Code using Rollup needs Subtotal labels and sorting

Dear Experts: 
I would like to create a calculated table with subtotals by country, as depicted in this graphic. 

That is ...
- the Country Subtotals should say 'Subtotal' in the 'Category'-Column and
- the Country Subtotals should appear at the bottom row for each country.

- The Overall Total Row should say 'Total' in the Category Column

 

Now the Subtotals per Country appear randomly somewhere within the Country related rows and they do not say 'Subtotal' as a Label.
Moreover the 'Total Sales' Column should be sorted descending within the respective Country rows. Currently there is no sorting at all for the Total Sales Values. 
Is this possible by tweaking the below code? That would be fantastic. 

Help is very much appreciated. Thank you very much in advance. 

 

Regards, Andreas

 

The Calculated Dax-Code is as follows so far: 

SalesSummary-simple =
SUMMARIZE(
    'DataSource',
    ROLLUP(DimCtry[Country],DimCat[Category]),
        "Total Sales", [Total Sales]
      )
 
 




RollUp-Subtotal-By-Country.png

 





1 ACCEPTED SOLUTION

Hi @AndreasHermle,

Thank you for reaching out to the Microsoft fabric community forum.

This is a work around you can go with this:

You can use the GROUP BY feature in Power Query to calculate total sales for each Country and Category. Then, create subtotals by grouping just by Country and label these as "Subtotal." Finally, add a row for the overall total labeled "Total." Combine all these results into one table and sort it so the subtotals appear under each Country group for a clear summary.

You can find official documentation  and screenshot here for reference:

vhjannapu_3-1747898965025.png

How to GROUP BY or summarize rows - Power Query | Microsoft Learn

If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it.

Thank you.

Harshitha.

Community Support Team.

View solution in original post

10 REPLIES 10
Learner27
Helper III
Helper III

Hai 

 

Could you please send the pbix file with some sample data hiding more sensitive data

Hi Learner, 

here you go. Thank you very much in advance for your valuable help. As I said before, your calculated table code is called SalesSummary, the second last calculated table in the Data Pane. 

https://drive.google.com/file/d/1MZjESs6GEcGij_SvBYU6e42t1nGlqY8W/view?usp=sharing

 

Hi @AndreasHermle,

Thank you for reaching out to the Microsoft fabric community forum.

This is a work around you can go with this:

You can use the GROUP BY feature in Power Query to calculate total sales for each Country and Category. Then, create subtotals by grouping just by Country and label these as "Subtotal." Finally, add a row for the overall total labeled "Total." Combine all these results into one table and sort it so the subtotals appear under each Country group for a clear summary.

You can find official documentation  and screenshot here for reference:

vhjannapu_3-1747898965025.png

How to GROUP BY or summarize rows - Power Query | Microsoft Learn

If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it.

Thank you.

Harshitha.

Community Support Team.

Great hjannapu, this did the trick. Thank you very much for your valuable help. Regards, Andreas

Dear Har**bleep**ha,

thank you very much for your great help. I wilm give it a try, as soon as I get home frm my vacation. Thank you, best regards, Andreas

Hi @AndreasHermle ,
If you get a chance to review my response, could you please confirm if the solution worked for you?
If it did, kindly consider marking it as "Accept as Solution" so that others with similar questions can find it more easily.If not, feel free to share more details so I can assist you further.

Thank you.

Harshitha.

Community Support Team.

 

Hi Learner, 

 

now here comes the file. Your code is integrated in a calculated table, named 'SalesSummary' and is the  second last calculated table at the bottom of the Data Pane. These sales figures along with other elements are all ficticious, so no need to hide any sensitive data.
Thank you very much in advance.
Oh, I am just looking for an icon to press so that I can upload my file. I cannot find one. I need to find out ...
Best Regards, Andreas

HI Learner, thank you very much for your offer. Great I will do that. Thank you very much again.

Learner27
Helper III
Helper III

SalesSummary =
VAR BaseTable =
ADDCOLUMNS (
'DataSource',
"Country", RELATED ( DimCtry[Country] ),
"Category", RELATED ( DimCat[Category] ),
"Total Sales", [Total Sales]
)
VAR SummaryTable =
SUMMARIZE (
BaseTable,
[Country],
[Category],
"Total Sales", SUM ( [Total Sales] )
)
VAR Subtotals =
ADDCOLUMNS (
SUMMARIZE ( BaseTable, [Country] ),
"Category", "Subtotal",
"Total Sales", CALCULATE ( SUM ( [Total Sales] ) )
)
VAR GrandTotal =
DATATABLE (
"Country", STRING,
"Category", STRING,
"Total Sales", CURRENCY,
{
{ BLANK (), "Total", CALCULATE ( SUM ( BaseTable[Total Sales] ) ) }
}
)
RETURN
UNION (
SummaryTable,
Subtotals,
GrandTotal
)

Subtotals per country, labeled as "Subtotal" in the Category column.
The total for all countries, labeled as "Total" in the Category column.

For sorting Desc order 

SortOrder =
RANKX(
FILTER('SalesSummary', 'SalesSummary'[Country] = EARLIER('SalesSummary'[Country]) && 'SalesSummary'[Category] <> "Subtotal"),
'SalesSummary'[Total Sales],
,
DESC,
Dense
)





 

Thank you very much learner for your swift and great help. I guess we are close to being perfect. 
I am afraid to tell you that the code throws a couple of errors, I was able to correct a couple of these myself, but the other ones, I just could not resolve.
Please find my Comments below. My Comments all start with three forward slashes followed by MY COMMENT (///MY COMMENT: ...)
Thank you very much in advance for your valuable help.

 

SalesSummary =
VAR BaseTable =
ADDCOLUMNS (
'DataSource',
"Country", RELATED ( DimCtry[Country] ),
"Category", RELATED ( DimCat[Category] ),
"Total Sales", [Total Sales]
)
VAR SummaryTable =
SUMMARIZE (
BaseTable,
[Country],
[Category],
//MY COMMENT - Coding by you: "Total Sales", SUM ( [Total Sales] ), subsequent line coding by me
"Total Sales", [Total Sales]
)
VAR Subtotals =
ADDCOLUMNS (
SUMMARIZE ( BaseTable, [Country] ),
"Category", "Subtotal",
///MY COMMENT - Coding by you: "Total Sales", CALCULATE ( SUM ( [Total Sales] ) ), subsequent line coding by me
"Total Sales", CALCULATE ( [Total Sales] )
)
VAR GrandTotal =
DATATABLE (
"Country", STRING,
"Category", STRING,
"Total Sales", CURRENCY,
{
///MY COMMENT: The subsequent line throwing an error on: SUM ( BaseTable[Total Sales] ) )
{ BLANK (), "Total", CALCULATE ( SUM ( BaseTable[Total Sales] ) ) }
}
)
RETURN
UNION (
SummaryTable,
Subtotals,
GrandTotal
)

//Subtotals per country, labeled as "Subtotal" in the Category column.
//The total for all countries, labeled as "Total" in the Category column.

//For sorting Desc order

///MY COMMENT: Here I am getting almost everything underlined with the Exception of RANKX, FILTER, <> "Subtotal", DESC and Dense
SortOrder =
RANKX(
FILTER('SalesSummary', 'SalesSummary'[Country] = EARLIER('SalesSummary'[Country]) && 'SalesSummary'[Category] <> "Subtotal"),
'SalesSummary'[Total Sales],
,
DESC,
Dense
)

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.