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

Getting average for x number of most recent days that contain values

 

Hello,

 

I have source data with dates, member ID's and values for a metric. I would like to get an average for each member of the metric for the last 3 dates in the record that contain any values. Not every date entry has a metric value and for a given member there could be gaps between dates in the record. Also for a given date there might be multiple entries for a member but not all will necessarily have a metric value. From the table below the latest 3 days with values for member A are 4-12-21, 4-11-21 and 4-9-21. The average of the metric for member A would be (100+90+100+110+130+200)/6 = 121.7. Is there a way to do this?

 

 

DateMemberMetric 
4/9/21A100
4/9/21A 
4/9/21A90
4/9/21A100
4/10/21B150
4/10/21A 
4/10/21B120
4/11/21B140
4/11/21A110
4/11/21A 
4/11/21B160
4/11/21A130
4/12/21A 
4/12/21B180
4/12/21B 
4/12/21A200
4/12/21A 
4/13/21A 
4/13/21B 

 

Thank you,

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, @Anonymous 

Please check the link down below and the measure, whether it is what you are looking for.

 

https://www.dropbox.com/s/fj1nhttvpr8vd1u/sford.pbix?dl=0 

 

 

Latest 3 days avg =
VAR currentmembers =
MAX ( Data[Member] )
VAR lastnonblankdate =
CALCULATE (
MAX ( Data[Date] ),
FILTER (
ALLSELECTED ( Data ),
Data[Member] = currentmembers
&& CALCULATE ( SUM ( Data[Metric ] ) ) > 0
)
)
VAR beforelastnonblankdate =
CALCULATE (
MAX ( Data[Date] ),
FILTER (
ALLSELECTED ( Data ),
Data[Member] = currentmembers
&& CALCULATE ( SUM ( Data[Metric ] ) ) > 0
&& Data[Date] < lastnonblankdate
)
)
VAR beforebeforelastnonblankdate =
CALCULATE (
MAX ( Data[Date] ),
FILTER (
ALLSELECTED ( Data ),
Data[Member] = currentmembers
&& CALCULATE ( SUM ( Data[Metric ] ) ) > 0
&& Data[Date] < beforelastnonblankdate
)
)
VAR datesfilter =
CALCULATETABLE (
FILTER ( ALLSELECTED ( Data ), Data[Member] = currentmembers ),
Data[Date] = lastnonblankdate
|| Data[Date] = beforelastnonblankdate
|| Data[Date] = beforebeforelastnonblankdate
)
RETURN
DIVIDE (
CALCULATE ( SUM ( Data[Metric ] ), datesfilter ),
CALCULATE ( COUNTROWS ( Data ), datesfilter )
- CALCULATE ( COUNTBLANK ( Data[Metric ] ), datesfilter )
)

 

 

Hi, My name is Jihwan Kim.

If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

Linkedin: https://www.linkedin.com/in/jihwankim1975/


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi, @Anonymous 

Please check the link down below and the measure, whether it is what you are looking for.

 

https://www.dropbox.com/s/fj1nhttvpr8vd1u/sford.pbix?dl=0 

 

 

Latest 3 days avg =
VAR currentmembers =
MAX ( Data[Member] )
VAR lastnonblankdate =
CALCULATE (
MAX ( Data[Date] ),
FILTER (
ALLSELECTED ( Data ),
Data[Member] = currentmembers
&& CALCULATE ( SUM ( Data[Metric ] ) ) > 0
)
)
VAR beforelastnonblankdate =
CALCULATE (
MAX ( Data[Date] ),
FILTER (
ALLSELECTED ( Data ),
Data[Member] = currentmembers
&& CALCULATE ( SUM ( Data[Metric ] ) ) > 0
&& Data[Date] < lastnonblankdate
)
)
VAR beforebeforelastnonblankdate =
CALCULATE (
MAX ( Data[Date] ),
FILTER (
ALLSELECTED ( Data ),
Data[Member] = currentmembers
&& CALCULATE ( SUM ( Data[Metric ] ) ) > 0
&& Data[Date] < beforelastnonblankdate
)
)
VAR datesfilter =
CALCULATETABLE (
FILTER ( ALLSELECTED ( Data ), Data[Member] = currentmembers ),
Data[Date] = lastnonblankdate
|| Data[Date] = beforelastnonblankdate
|| Data[Date] = beforebeforelastnonblankdate
)
RETURN
DIVIDE (
CALCULATE ( SUM ( Data[Metric ] ), datesfilter ),
CALCULATE ( COUNTROWS ( Data ), datesfilter )
- CALCULATE ( COUNTBLANK ( Data[Metric ] ), datesfilter )
)

 

 

Hi, My name is Jihwan Kim.

If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

Linkedin: https://www.linkedin.com/in/jihwankim1975/


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

Works perfectly. Thank you.

amitchandak
Super User
Super User

@Anonymous , Try a measure like

 


measure =
var _max = maxx(allselected(Table), Table[Date])
return
calculate(divide(sum(Table[Metric]),count(Table[Metric])), filter(allselected(Table), table[Member] = max(table[Member]) && Table[date] <=_max && Table[Date] >=_max -3))

 

 

or

 


measure =
var _max = maxx(allselected(Table), Table[Date])
return
calculate(divide(sum(Table[Metric]),count(Table[Metric])), filter(all(Table), table[Member] = max(table[Member]) && Table[date] <=_max && Table[Date] >=_max -3))

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

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