Forum Discussion
Calculated Table Dax Code using Rollup needs Subtotal labels and sorting
- 1 year ago
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: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.
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
)
- AndreasHermle1 year agoRegular Visitor
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] ) ) }})RETURNUNION (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 DenseSortOrder =RANKX(FILTER('SalesSummary', 'SalesSummary'[Country] = EARLIER('SalesSummary'[Country]) && 'SalesSummary'[Category] <> "Subtotal"),'SalesSummary'[Total Sales],,DESC,Dense)