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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Calcular el promedio en función de la condición

Hola equipo,

¿Cómo puedo calcular el promedio en el siguiente escenario:

NombreEstadoFecha
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

En este caso, el número medio de días debe calcularse entre S3 y S5, si S4 no está disponible. Si S4 está presente, el promedio de días debe estar entre S4 y S5. ¿Cómo puedo escribir una sola medida para esto?

En el ejemplo anterior, la salida será: Diferencia de fecha entre S3 y S5 a 56 días y Diferencia de fecha entre S4 y S5 a 4 días. Promedio final (56+4)/2 a 30 días

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

Hola @pbiguru999 ,

Cree una columna calculada como se muestra a continuación:

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

A continuación, cree 2 medidas como se indica a continuación:

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)

Y verás:

Annotation 2020-07-31 162337.png

Para el archivo .pbix relacionado, pls ver adjunto.

Saludos
Kelly
¿He respondido a tu pregunta? ¡Marca mi puesto como una solución!
Greg_Deckler
Super User
Super User

@pbiguru999 Probablemente podría hacer algo como:

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.

@ 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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.