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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Calculate average based on condition

Hi Team,

 

How can I calculate the average in the following scenario: 

NameStatusDate
ABC1S11/15/2018
ABC1S22/1/2018
ABC1S32/5/2018
ABC1S54/2/2018
ABC2S12/3/2018
ABC2S23/19/2018
ABC2S34/23/2018
ABC2S44/29/2018
ABC2S55/3/2018

 

In this case, the average number of days needs to be calculated between S3 and S5, if S4 is not available. If S4 is present, the average days should be between S4 and S5. How can I write a single measure for this?

 

In the above example, output will be: Date diff between S3 and S5 = 56 days and Date diff between S4 and S5 = 4 days. Final Average = (56+4)/2 = 30 days

1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

Create a calculated column as below:

 

S4 search = SEARCH("S4",'Table'[Status],1,0)

 

Then create 2 measures as below:

 

Measure = 
var _datediff1=DATEDIFF(CALCULATE(MIN('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S3"&&'Table'[Name]=MAX('Table'[Name]))),CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S5"&&'Table'[Name]=MAX('Table'[Name]))),DAY)
var _datediff2=DATEDIFF(CALCULATE(MIN('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S4"&&'Table'[Name]=MAX('Table'[Name]))),CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S5"&&'Table'[Name]=MAX('Table'[Name]))),DAY)
var _sum=SUMX(FILTER(ALL('Table'),'Table'[Name]=MAX('Table'[Name])),'Table'[S4 search])
Return
IF(_sum>=1,_datediff2,_datediff1)
average = 
var _count=DISTINCTCOUNT('Table'[Name])
VAR _TOTAL=SUMX(VALUES('Table'[Name]),'Table'[Measure])
Return
DIVIDE(_TOTAL,_count)

 

And you will see:

Annotation 2020-07-31 162337.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

View solution in original post

2 REPLIES 2
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

Create a calculated column as below:

 

S4 search = SEARCH("S4",'Table'[Status],1,0)

 

Then create 2 measures as below:

 

Measure = 
var _datediff1=DATEDIFF(CALCULATE(MIN('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S3"&&'Table'[Name]=MAX('Table'[Name]))),CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S5"&&'Table'[Name]=MAX('Table'[Name]))),DAY)
var _datediff2=DATEDIFF(CALCULATE(MIN('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S4"&&'Table'[Name]=MAX('Table'[Name]))),CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Status]="S5"&&'Table'[Name]=MAX('Table'[Name]))),DAY)
var _sum=SUMX(FILTER(ALL('Table'),'Table'[Name]=MAX('Table'[Name])),'Table'[S4 search])
Return
IF(_sum>=1,_datediff2,_datediff1)
average = 
var _count=DISTINCTCOUNT('Table'[Name])
VAR _TOTAL=SUMX(VALUES('Table'[Name]),'Table'[Measure])
Return
DIVIDE(_TOTAL,_count)

 

And you will see:

Annotation 2020-07-31 162337.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!
Greg_Deckler
Community Champion
Community Champion

@Anonymous Probably could do something like:

 

Measure = 
  VAR __FromStatus = IF("S4" IN SELECTCOLUMNS('Table',"Status",'Table'[Status]),"S4","S3")
  VAR __ToStatus = "S5"
  VAR __From = MAXX(FILTER('Table',[Status] = __FromStatus),[Date])
  VAR __To = MAXX(FILTER('Table',[Status] = __ToStatus),[Date])
RETURN
  (__From - __To) * 1.


Average = 
  VAR __Table = SUMMARIZE('Table',[Name],"Days",[Measure])
RETURN
  AVERAGEX(__Table,[Measure])

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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