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
BrianNeedsHelp
Helper III
Helper III

SUMX Group By Multiple Categories

I have this table, which shows the correct results in Total By ID and Month. 

UniqueID0  DateClosed  TotalHoursClosed  Total By ID and Month
19/30/202488
110/9/2024812
110/10/2024412
210/9/2024816
210/9/2024816
39/30/202444
310/9/202444

If I use this I get the equivilant of Total Hours Closed Column.  

 

 

TotalHoursByIDandMonth = 
Calculate(SUMX(VALUES('BCP (2)'[UniqueID0]),[TotalHoursClosed]),GROUPBY('BCP (2)','BCP (2)'[UniqueID0],'BCP (2)'[DateConversion]))

 

 

Even though it's not making sense because I've read that ALLEXCEPT just removes filters, and I don't have any filters, but it's closer to what I need-it sums by UniqueID0 but doesn't take into consideration the date, so the first 3 rows e.g. equals 20:  

 

 

TotalHoursByIDandMonth = 
Calculate(SUMX(VALUES('BCP (2)'[UniqueID0]),[TotalHoursClosed]),ALLEXCEPT('BCP (2)','BCP (2)'[UniqueID0],'BCP (2)'[DateConversion]))

 

 

[DateConversion] is a Calculated Column 

DateConversion = EOMONTH('BCP (2)'[DateClosed],-1)+1

How would I sum this grouping by UniqueID0 and DateConversion(FirstOfMonth)?  


1 ACCEPTED SOLUTION

@FreemanZ @Kedar_Pande  I did it!   This works! 

TotalHoursByIDandMonth2 = 
CALCULATE(
    SUMX('BCP (2)',[TotalHoursClosed]),
REMOVEFILTERS('BCP (2)'),VALUES('BCP (2)'[UniqueID0]),VALUES('BCP (2)'[ColumnMonthExtract]))

Could someone mark this as a solution and give a kudos, since I answered my own question?   Please see Using ALLEXCEPT versus ALL and VALUES - SQLBI for reference.  Hope it helps someone.  

View solution in original post

10 REPLIES 10
Kedar_Pande
Super User
Super User

@BrianNeedsHelp 

Create the Measure:

TotalHoursByIDandMonth = 
CALCULATE(
SUM('BCP (2)'[TotalHoursClosed]),
ALLEXCEPT('BCP (2)', 'BCP (2)'[UniqueID0], 'BCP (2)'[DateConversion])
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

I did this, but I figured out I could tack on .Month i.e.  [DateConversion].Month.  Thought that would solve it but it still sums the total by UniqueID0 without considering the month.   I think this doesn't like me.  

TotalHoursByIDandMonth =
CALCULATE(
    SUMX('BCP (2)',[TotalHoursClosed]),
ALLEXCEPT('BCP (2)', 'BCP (2)'[UniqueID0],'BCP (2)'[DateConversion].[Month]))
FreemanZ
Super User
Super User

With CALCULATE, try like:

column 2 = 
CALCULATE(
    SUM(BCP[TotalHoursClosed]),
    ALLEXCEPT(BCP, BCP[UniqueID0], BCP[DateClosed])
)

 

FreemanZ_1-1732068978245.png

 

SUMX(VALUES() ) is more for other more complicated occassion, like calculating with higher-than-record level of granularities. 

FreemanZ
Super User
Super User

Hi @BrianNeedsHelp ,

 

try like:

column = 
VAR _id = [UniqueID0]
VAR _ym = EOMONTH([DateClosed], 0)
VAR _result = 
SUMX(    
    FILTER(
        bcp,
        bcp[UniqueID0] = _id
            && EOMONTH(BCP[DateClosed], 0) = _ym
    ),
    bcp[TotalHoursClosed]
) 
RETURN _result

 

it works like:

FreemanZ_0-1732068806984.png

 

as a general rule, try to avoid use CALCULATE in creating calculated columns. 

I was trying to do this in a measure, so when I use this I get sum totals by ID, but doesn't take into consideration the date.  

TotalHoursByIDandMonth =
CALCULATE(
    SUMX('BCP (2)',[TotalHoursClosed]),
    ALLEXCEPT('BCP (2)', 'BCP (2)'[UniqueID0],'BCP (2)'[DateConversion]))

I tried using the column, but I get an error "A circular reference was detected" Any idea on that?  

hi @BrianNeedsHelp ,

 

you are using CALCULATE+SUMX, using CALCULATE+SUM instead.

I can't use SUM.  "Parameter is not the correct type".   

hi @BrianNeedsHelp ,

 

try like:

 

TotalHoursByIDandMonth =
CALCULATE(
    [TotalHoursClosed],
    ALLEXCEPT('BCP (2)''BCP (2)'[UniqueID0],'BCP (2)'[DateConversion]))

@FreemanZ @Kedar_Pande  I did it!   This works! 

TotalHoursByIDandMonth2 = 
CALCULATE(
    SUMX('BCP (2)',[TotalHoursClosed]),
REMOVEFILTERS('BCP (2)'),VALUES('BCP (2)'[UniqueID0]),VALUES('BCP (2)'[ColumnMonthExtract]))

Could someone mark this as a solution and give a kudos, since I answered my own question?   Please see Using ALLEXCEPT versus ALL and VALUES - SQLBI for reference.  Hope it helps someone.  

I've tried tha numerous times.  It doesn't recognize the date portion.  So I created another column by using Month('BCP (2)'[DateClosed]).  And then tried pretty much the same thing.  I tried to see what it would do if I just used the Date in the AllEXCEPT and it doesn't distinguish the months-it just sums all the hours...

TotalHoursByIDandMonth =
CALCULATE(
    SUMX('BCP (2)',[TotalHoursClosed]),
ALLEXCEPT('BCP (2)', 'BCP (2)'[UniqueID0],'BCP (2)'[ColumnMonthExtract]))

 

Helpful resources

Announcements
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.