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
Anonymous
Not applicable

Subtotal by one report column in another column

Hi

 

I'm trying to produce a report that has a subtotal column for a particular field filtered by <= date that is output on the pivot table (excel) and I'm struggling to get it to work. It seems like it should be really easy, but perhaps not!

 

Example Data

Date-------- Reference 1------------------Reference 2--------------Amount

01/07/19----A100_________________________U100___________________200.00

01/08/19----A100_________________________U100___________________-100.00

01/07/19----A100_________________________U200___________________-100.00

01/08/19----A101_________________________U101___________________150.00

01/07/19----A101_________________________U102___________________200.00

01/07/19----A101_________________________U102___________________-300.00

01/08/19----A101_________________________U102___________________150.00

01/09/19----A101_________________________U102___________________150.00

 

I have a date table in my model. I want to have a measure of Total by Reference1 and a measure of Total by Reference2 like:

Ref1Total:=CALCULATE(SUM(Amount),FILTER(ALL(dDate),dDate[Date]<=MAX(dDate[Date])&&data[Reference1]=VALUES(data[Reference1])))

and

Ref2Total:=CALCULATE(SUM(Amount),FILTER(ALL(dDate),dDate[Date]<=MAX(dDate[Date])&&data[Reference2]=VALUES(data[Reference2])))

To produce an output of the following when filtering on date 01/08/19: 

Reference 1------------------Reference 2--------------Ref1Total------Ref2Total

A100_________________________U100___________________0.00____________100.00

A100_________________________U200___________________0.00____________-100.00

A101_________________________U101___________________200.00__________150.00

A101_________________________U102___________________200.00__________50.00

 

Individually, my measures work fine, but when I add Reference 1 and 2 together the reference 1 total appears to be affected by the Ref2 column being added. In the above example, the Ref1Total for the first line reads 100 and the second reads -100 when I output it. Any suggestions on how I add a subtotal by Reference1 measure for each line?

 

Thanks,

Rico 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Ref1Total :=
var __lastVisibleDate = MAX( dDate[Date] )
return
	CALCULATE(
		SUM( data[Amount] ),
		dDate[Date] <= __lastVisibleDate,
		ALLEXCEPT( data, data[Reference1] )
	)
	
Ref2Total :=
var __lastVisibleDate = MAX( dDate[Date] )
return
	CALCULATE(
		SUM( data[Amount] ),
		dDate[Date] <= __lastVisibleDate,
		ALLEXCEPT( data, data[Reference2] )
	)

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Ref1Total :=
var __lastVisibleDate = MAX( dDate[Date] )
return
	CALCULATE(
		SUM( data[Amount] ),
		dDate[Date] <= __lastVisibleDate,
		ALLEXCEPT( data, data[Reference1] )
	)
	
Ref2Total :=
var __lastVisibleDate = MAX( dDate[Date] )
return
	CALCULATE(
		SUM( data[Amount] ),
		dDate[Date] <= __lastVisibleDate,
		ALLEXCEPT( data, data[Reference2] )
	)
Anonymous
Not applicable

I tried an ALLEXCEPT() the other day and couldn't get it to work as I was then filtering by date, rather than adding the date filter into the calculate function as you have done. That works perfectly, thanks!

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.