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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
GuillaumeB
Helper I
Helper I

Aggregating calculation by months within a variable

I have a table that has a structure similar to this:

ItemDateProfitCost1Cost2
A

1-jan

9.53

0.157.10
A31-jan9.500.157.10
A01-Feb9.480.207.15
A28-Feb

9.75

0.207.15
A01-Mar9.680.257.20
A31-Mar9.400.257.20
B1-jan5.900.214.20
B31-jan5.780.214.20
B01-Feb5.890.264.25
B28-Feb5.870.264.25
B01-Mar5.670.284.30
B31-Mar5.330.284.30

 

I need to aggregate, inside a measure (not a physical table), considering ONLY the last day of each month through the following equation: (Profit-Cost1-cost2)/Cost2, combining all items together for each end of the month. 

 

I basically ignore all rows that aren't on the last of the month and calculate the result (which is  %) for each month so I can aggregate using Product/ProductX.

 

Ideally it would result in something like this:

DateEquationAnswer
31-Jan((9.50+5.60)-(0.15+0.21)-(7.10+4.20)/ (7.10+4.20)0.320354
28-Feb((9.75+5.89)-(0.20+0.26)-(7.15+4.25))/ (7.15+4.25)0.329825
31-Mar((9.75+5.89)-(0.20+0.26)-(7.15+4.25))/ (7.15+4.25)0.174419

 

And then the rest of my measure will be to Product() the Answers column.

The fact that I need to Product and still slice based on other columns means I can't use a physical table, I have to use a measure and I've had no luck with the Summarize function. 

Would anyone know how to go about doing this?

Let me know if you have questions, I tried to be as precise as possible.

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Your sample data does not agree with your explanation of the expected outcome. For example there is no 5.60 value (did you mean 5.78?) and no 5.89 value (did you mean 5.87 and 5.33 ?

 

First step: Create a calculated column identifying the month:

 

YearMonth = FORMAT('Table'[Date],"YYYYmm")

 

Next:  Write the measure :

 

Measure = 
// find max date per month
var a = GROUPBY('Table','Table'[YearMonth],"d",maxx(CURRENTGROUP(),'Table'[Date]))
// calculate quotient
var b = ADDCOLUMNS(a,"q",CALCULATE(DIVIDE(SUM('Table'[Profit])-SUM('Table'[Cost1])-SUM('Table'[Cost2]),SUM('Table'[Cost2]),0),Filter('Table','Table'[Date]=[d])))
//multiply result
return PRODUCTX(b,[q])

 

And finally, put the yearmonth and the measure into a table.

lbendlin_0-1628472420191.png

The way the measure is written will work both for the column total and the individual rows

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

Your sample data does not agree with your explanation of the expected outcome. For example there is no 5.60 value (did you mean 5.78?) and no 5.89 value (did you mean 5.87 and 5.33 ?

 

First step: Create a calculated column identifying the month:

 

YearMonth = FORMAT('Table'[Date],"YYYYmm")

 

Next:  Write the measure :

 

Measure = 
// find max date per month
var a = GROUPBY('Table','Table'[YearMonth],"d",maxx(CURRENTGROUP(),'Table'[Date]))
// calculate quotient
var b = ADDCOLUMNS(a,"q",CALCULATE(DIVIDE(SUM('Table'[Profit])-SUM('Table'[Cost1])-SUM('Table'[Cost2]),SUM('Table'[Cost2]),0),Filter('Table','Table'[Date]=[d])))
//multiply result
return PRODUCTX(b,[q])

 

And finally, put the yearmonth and the measure into a table.

lbendlin_0-1628472420191.png

The way the measure is written will work both for the column total and the individual rows

Absolutely for the sample data, I wrote it on the fly and I think I messed up the math.


That being said, your solution absolutely worked and basically boiled down my +1,000 line Dax measure to under 25 lines. That's a lifesaver. Thanks mate!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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