Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Get the median based on certain column

Hello im trying to compute median of the market value (MV) for each of site based on municipality which the site is located, and if there is no record of MV for the particular municipality, get the median based on the province which the site is located. 

 

I have 3 tables:

 

LGU List
ProvinceMunicipalityLGU Code

 

SID
Site IDLGU Code

 

Consolidated TD List
Site IDTD NoProperty TypeMV

 

LGU list has one to many relationship with SID using LGU Code, wile SID has one to many relationship with Consolidated TD List using site ID.
I tried to come up with a measure like this:


 

Median Measure = 
VAR MedMun=
CALCULATE(
    MEDIAN('Consolidated TD List'[MV]),
    'Consolidated TD List'[TDNo] <> BLANK(),
    CONTAINSSTRING('Consolidated TD List'[Property Type], "Building"),
    REMOVEFILTERS('LGU List'[LGU Code]),
    VALUES('LGU List'[Municipality])
)
VAR MedProv =
CALCULATE(
    MEDIAN('Consolidated TD List'[MV]),
    'Consolidated TD List'[TDNo] <> BLANK(),
    CONTAINSSTRING('Consolidated TD List'[Property Type], "Building"),
    REMOVEFILTERS('LGU List'[LGU Code]),
    VALUES('LGU List'[Province])
)
RETURN
IF(
    MedMun>0,
    MedMun,
    MedProv
) 

 

Somehow it is not working. My visual is a simple one, list of site ids and the median, with grandtotal. Let me know what needs to change. Thanks

  • lbendlin's avatar
    lbendlin
    4 years ago
    Median = COALESCE(medianx(TDList,TDList[MV]),CALCULATE(medianx(TDList,TDList[MV]),REMOVEFILTERS(LGUlist[Municipality])))
  • Anonymous's avatar
    Anonymous
    4 years ago

    ok thanks will check the measure will get back to you if i encounter any issues. anyway many thanks for your time

13 Replies

  • Please provide sanitized sample data that fully covers your issue. I cannot help you without usable sample data.
    Please paste the data into a table in your post or use one of the file services like OneDrive or Google Drive. I cannot use screenshots of your source data.
    Please show the expected outcome based on the sample data you provided. Screenshots of the expected outcome are ok.

    https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello Ibendlin,

       

      I manage to create a sample pbix file so you can check, my goal is create another measure to get the median of the mv per site per municipality (if not available per province). me initially thinking using mediax but that will entail me coming up two mediax for each site (one per municipality and one per province). another thing, there are unmatched site ids between td list and sid, my work around was to create a calculated column in the td list. Can you suggest another way to only count those matched site ids? 

      https://drive.google.com/drive/folders/1f0ew6RAl4s26fKzJFnNvy9CHaweTRpQR?usp=sharing 

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        I don't see what's wrong with your median calculation. Seems to work as designed?