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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

@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])

 

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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