cancel
Showing results for
Did you mean:

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

Helper II

## 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 Province Municipality LGU Code

 SID Site ID LGU Code

 Consolidated TD List Site ID TD No Property Type MV

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

2 ACCEPTED SOLUTIONS
Super User
``Median = COALESCE(medianx(TDList,TDList[MV]),CALCULATE(medianx(TDList,TDList[MV]),REMOVEFILTERS(LGUlist[Municipality])))``
Helper II

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

13 REPLIES 13
Super User

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.

Helper II

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?

Super User

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

Helper II

Hello, it is getting the median of the tds and not the sum of tds per site. I suppose medianx(sid, calculate(sum(tdlist[Mv]) can address that however as you can see in the result of your powerbi file. it has no value for city of legaspi, oas and jovellar. the values for these municipality wll be using the median for the entire albay province instead. the thing is if we can optimize the measure i do not have to use the medianx twice? (i.e. per municipality and per province) is there a way to create a variable table showing the values per site then use median instead of medianx?

Super User

Looks like I still don't understand what you are trying to achieve, and why. Are you maybe looking for the average instead of the median?

Helper II

Hello,

As you can see in the pbix file i sent to you, each site contains 2 or more TDs with different market values. I need to get the sum of all these tds for each site to get the market value per site. then for all the sites located in the specific municipality, get the median (not the average since i want to remove all possible outlier values that will distort the average value).

Please see the above screenshot, value of median is different if it simply based on the tds rather than the sum of the tds per site. As mentioned again, i know i can do medianx  to achieve the result, however there are certain municipalities which do not have tds hence i can't get the median for that particular municipality. As remedy, i will need to get the median for the entire province. if that is the case that will mean i will also need to do medianx on per province. so my goal is to have an optimal measure that will allow me to get the median per lgu, if available, or else get the median per province. I hope this clears up my query.

Super User

Ah so you want the MEDIANX() for the SUM per category, not for individual values?

Helper II

sorry for the late reply, i will need to get the median per site for the entire municipality except those without mv such as jovelar, oas, and city of legaspi in which i will need to get the median per site for the entire province. Hope this clears things up. thanks

Super User

No, still not clear. Quite the opposite. Maybe you can walk me through a particular example.

Helper II

For Camalig, since it has 3 sites with MV, the median measure should get 2319620 (the median per site for that particular municipality). Meanwhile, for city of legaspi, since it does not have sites with MV, the measure should get all the individual MV for each site for the entire province of albay then get the median.

Super User
``Median = COALESCE(medianx(TDList,TDList[MV]),CALCULATE(medianx(TDList,TDList[MV]),REMOVEFILTERS(LGUlist[Municipality])))``
Helper II

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

Helper II

made some minor modification but thanks for the help

``````Median Value =
VAR PerMuni =
CALCULATE(
MEDIANX(
SID,
[MV Value]
),
REMOVEFILTERS(SID[SiteID])
)
VAR PerProv =
CALCULATE(
MEDIANX(
SID,
[MV Value]
),
REMOVEFILTERS(SID[SiteID]),
REMOVEFILTERS(LGUlist[Municipality])
)
Return
COALESCE(PerMuni,PerProv)*[No of Sites]``````

Announcements

#### New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

#### Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

#### Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors