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
hzapiainr
New Member

Median calculation of a Measure (Covariance)

Hello! 

 

I’m having a problem which has troubled me for the last week and I’m been unable to get the result I need.

What I do is to determine a Measure (I need to make it dynamically as there are several slicers in the dashboard) to calculate the Covariance factor based on the accumulated revenues of each customer per Quarter of the last 12 months.

 

COV = 
VAR Q1= CALCULATE(SUMX('A_6_Strat Full','A_6_Strat Full'[REV]),DATESINPERIOD('D_7_Calendar'[Date],DATEADD(FIRSTDATE('D_7_Calendar'[Date]),-2,MONTH),3,MONTH),ALLSELECTED('A_6_Strat Full'[Id Customer]))
VAR Q2=CALCULATE(SUMX('A_6_Strat Full','A_6_Strat Full'[REV]),DATESINPERIOD('D_7_Calendar'[Date],DATEADD(FIRSTDATE('D_7_Calendar'[Date]),-5,MONTH),3,MONTH),ALLSELECTED('A_6_Strat Full'[Id Customer]))
VAR Q3=CALCULATE(SUMX('A_6_Strat Full','A_6_Strat Full'[REV]),DATESINPERIOD('D_7_Calendar'[Date],DATEADD(FIRSTDATE('D_7_Calendar'[Date]),-8,MONTH),3,MONTH),ALLSELECTED('A_6_Strat Full'[Id Customer]))
VAR Q4=CALCULATE(SUMX('A_6_Strat Full','A_6_Strat Full'[REV]),DATESINPERIOD('D_7_Calendar'[Date],DATEADD(FIRSTDATE('D_7_Calendar'[Date]),-11,MONTH),3,MONTH),ALLSELECTED('A_6_Strat Full'[Id Customer]))
Var Tot=(Q1+Q2+Q3+Q4)
VAR Ave= Tot/4
VAR Desvest= (((Q1-Ave)^2 +(Q2-Ave)^2+(Q3-Ave)^2+(Q4-Ave)^2)/3)^(1/2)
Var COV = Ave/Desvest
RETURN
IF(ISBLANK(COV),BLANK(),COV)

This measure generates an output like this when in a table with the Id Customer and Segment.

Output COVOutput COV

 

From this point on, what I need is to determine a Median based on the records that have the same segment

Median per SegmentMedian per Segment

 

  in order to have an output just like this

Desired OutputDesired Output

 

I’ve tried several approaches but to be frank I’ve been unable to get it right. Do you have any suggestion on how to get around this issue?

 

I would appreciate any help that you guys can give me 😊

 

1 ACCEPTED SOLUTION

So I have found a waym around this, pretty much I had to reconfigure my data to allow for the desired result, and calculate my covariance in several steps applyng the same filters along all the way. Here's an example:

 

Z_1_B_TOT = SUMX(SUMMARIZE('A_09_COV','A_09_COV'[Id Cliente],'A_09_COV'[Segmento_Rel],"Total",SUM('A_09_COV'[Value])),[Total])

Z_2_AveQ = [Z_1_B_TOT]/4

Z_3_Desvest = STDEVX.S(SUMMARIZE('A_09_COV',A_09_COV[Id Cliente],'A_09_COV'[Segmento_Rel],A_09_COV[Attribute],"Desvest",[Z_1_B_TOT]),[Desvest])

Z_4_COV = DIVIDE([Z_2_AveQ],[Z_3_Desvest],0)

1_F_OrderConsistency = 
VAR GralMedian = IF(ISBLANK([Z_4_COV]),BLANK(),CALCULATE(MEDIANX(SUMMARIZE('A_09_COV','A_09_COV'[Id Cliente],'A_09_COV'[Segmento_Rel],"Medx",[Z_4_COV]),[Medx]),ALLSELECTED(C_1_IdClienteDB[Id Cliente])))
RETURN
SWITCH(TRUE(),ISBLANK([Z_4_COV]),BLANK(),[Z_4_COV]>1.5*GralMedian, "D",AND([Z_4_COV]<=1.5*GralMedian,[Z_4_COV]>GralMedian),"C",AND([Z_4_COV]<=GralMedian,[Z_4_COV]>.5*GralMedian),"B",[Z_4_COV]<=.5*GralMedian,"A","D")

 

View solution in original post

3 REPLIES 3
v-yulgu-msft
Employee
Employee

Hi @hzapiainr,

 

You can try MEDIANX function to return the median of numbers in a measure.

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-yulgu-msft

 

Thanks for your prompt response,

 

I've already tried Medianx however there are two issues on using it directly:

 

1) I can't find any way to determine the medians per category (in this case Segment) as the image with my desired output.

 

2) If I use MedianX integrated with time intelligence functions, I dont get the desired output; the way I've tried and failed is the next one:

 

 

Measure = CALCULATE(Medianx(A3_Merge,[COV]),DATESINPERIOD('C1_Calendar'[Date],DATEADD(FIRSTDATE('C1_Calendar'[Date]),-11,MONTH),12,MONTH),allselected(A3_Merge[Sucursal]))

where A3_Merge[Sucursal] is a slicer in the report with other types of attributes.

 

Any sugestions on how to workaround this?

 

Kind Regards

So I have found a waym around this, pretty much I had to reconfigure my data to allow for the desired result, and calculate my covariance in several steps applyng the same filters along all the way. Here's an example:

 

Z_1_B_TOT = SUMX(SUMMARIZE('A_09_COV','A_09_COV'[Id Cliente],'A_09_COV'[Segmento_Rel],"Total",SUM('A_09_COV'[Value])),[Total])

Z_2_AveQ = [Z_1_B_TOT]/4

Z_3_Desvest = STDEVX.S(SUMMARIZE('A_09_COV',A_09_COV[Id Cliente],'A_09_COV'[Segmento_Rel],A_09_COV[Attribute],"Desvest",[Z_1_B_TOT]),[Desvest])

Z_4_COV = DIVIDE([Z_2_AveQ],[Z_3_Desvest],0)

1_F_OrderConsistency = 
VAR GralMedian = IF(ISBLANK([Z_4_COV]),BLANK(),CALCULATE(MEDIANX(SUMMARIZE('A_09_COV','A_09_COV'[Id Cliente],'A_09_COV'[Segmento_Rel],"Medx",[Z_4_COV]),[Medx]),ALLSELECTED(C_1_IdClienteDB[Id Cliente])))
RETURN
SWITCH(TRUE(),ISBLANK([Z_4_COV]),BLANK(),[Z_4_COV]>1.5*GralMedian, "D",AND([Z_4_COV]<=1.5*GralMedian,[Z_4_COV]>GralMedian),"C",AND([Z_4_COV]<=GralMedian,[Z_4_COV]>.5*GralMedian),"B",[Z_4_COV]<=.5*GralMedian,"A","D")

 

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.