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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Calculating last 6 month average but ignoring current month

Hello,

 

I am relatively new to Power BI. I use the following measure to calculate last 6 months average, excluding current month:

 

6MonthResolvedAVG =
CALCULATE(SUMX('Data Table',IF(DATEDIFF('Data Table'[Date],TODAY(),MONTH) >0,[ResolvedCR],0)),
DATESINPERIOD('Data Table'[Date],EOMONTH (lastdate('Data Table'[Date]) , -1 ),-6,MONTH))/6​

 

It works correctly for recent data. However.... if I apply some filters and there is no current data but , let's say data is from 2019, then the measure still calculates something, which is incorrect. I need to force the measure to use maximum date from the table as of today and not a "relative" date depending on the filters.

Expected behavior: if there isn't any data for the past 6 months,excluding current month and calculated from absolute maximum date in the table, then display 0.

Please advise.

 

Regards,

M. R.

1 ACCEPTED SOLUTION

OK. It makes sense now. You've defined a calculated column 'Combined_CQ_view'[ResolvedCR] instead of a measure.

 

Therefore you need to adjust the formula. Like this, for example:

 

6MonthResolvedAVG =
VAR PrevMonth =
    EOMONTH ( CALCULATE ( MAX ( 'Combined_CQ_view'[Date] ), ALL ( 'Combined_CQ_view' ) ), -1 )
RETURN
    CALCULATE (
        SUM ( 'Combined_CQ_view'[ResolvedCR] ),
        DATESINPERIOD ( 'Combined_CQ_view'[Date], PrevMonth, -6, MONTH )
    ) / 6​

View solution in original post

12 REPLIES 12
AlexisOlson
Super User
Super User

I'd suggest calculating the end of the last month as a variable first and then using in your date filtering.

 

6MonthResolvedAVG =
VAR PrevMonth =
    EOMONTH ( CALCULATE ( MAX ( 'Data Table'[Date] ), ALL ( 'Data Table' ) ), -1 )
RETURN
    CALCULATE (
        [ResolvedCR],
        DATESINPERIOD ( 'Data Table'[Date], PrevMonth, -6, MONTH )
    ) / 6​
Anonymous
Not applicable

Thank you Alexis but [ResolvedCR] is a measure, not a table column and I get "Cannot find name '[ResolvedCR]'".

Can you please advise how to resolve this issue?

That error doesn't make sense to me. If the measure exists, it should be referencing it just like it did in your version.

 

Did the measure name change perhaps?

Anonymous
Not applicable

Measure did not changed but after looking at some older posts, it seems that referencing a measure in another measure may not work:

 

mrusu_1-1613756068743.png

 

 

You can absolutely reference measures within other measures. Can you show a screenshot of your original measure that doesn't have an error but references the same measure? I'm really curious what the difference is.

Anonymous
Not applicable

This is the measure:

ResolvedCR = if('Combined_CQ_view'[DateStamp]="Resolved",1,0)

That's not a correctly written measure. It appears to be a calculated column, which might explain why it worked in your SUMX but not when called as a measure.

 

Anonymous
Not applicable

This is the original calculation, no error:

 

6MonthResolvedAVG =
CALCULATE(SUMX('Combined_CQ_view',IF(DATEDIFF('Combined_CQ_view'[Date],TODAY(),MONTH) >0,[ResolvedCR],0)),
DATESINPERIOD('Combined_CQ_view'[Date],EOMONTH ( lastdate('Combined_CQ_view'[Date]) , -1 ),-6,MONTH))/6

OK. It makes sense now. You've defined a calculated column 'Combined_CQ_view'[ResolvedCR] instead of a measure.

 

Therefore you need to adjust the formula. Like this, for example:

 

6MonthResolvedAVG =
VAR PrevMonth =
    EOMONTH ( CALCULATE ( MAX ( 'Combined_CQ_view'[Date] ), ALL ( 'Combined_CQ_view' ) ), -1 )
RETURN
    CALCULATE (
        SUM ( 'Combined_CQ_view'[ResolvedCR] ),
        DATESINPERIOD ( 'Combined_CQ_view'[Date], PrevMonth, -6, MONTH )
    ) / 6​
Anonymous
Not applicable

It works now, thank you very much for your help !

amitchandak
Super User
Super User

@Anonymous , Try like

 

6MonthResolvedAVG =
if( CALCULATE(distinctcount('Date'[Month Year]),
DATESINPERIOD('Data Table'[Date],EOMONTH (max('Data Table'[Date]) , -1 ),-6,MONTH) , filter('Data Table',not(isblank([ResolvedCR])) )) <=6,0, CALCULATE([ResolvedCR],
DATESINPERIOD('Data Table'[Date],EOMONTH (max('Data Table'[Date]) , -1 ),-6,MONTH))/6​)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Thank you Amit, but what is 'Date'[Month Year]? I don't have this table. Should I create it?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors