Using the FirstSortedValue() function to identify the median in a quartile range

Our next task is to find a claim corresponding to the median value in each quartile. A median is nothing but a value corresponding to the 50th percentile. We can achieve this using the FirstSortedvalue() and median() functions.

Getting ready

Continue with the same application as in the preceding recipe.

How to do it…

  1. Go to the Edit mode by clicking on How to do it….
  2. Select the table we created just now in the preceding recipe.
  3. Edit the CaseID dimension and put in the following calculation:
    =if(Match(CaseID,
    '$(=FirstSortedValue(distinct{<Value={"<=$(=Median({<Value= {'>=$(=fractile(Value, 0))<=$(=Fractile(Value, 0.25))'}>} Value))"}>} CaseID, -Value))',
    '$(=FirstSortedValue(distinct{<Value={"<=$(=Median({<Value= {'>$(=fractile(Value, 0.25))<=$(=fractile(Value, 0.5))'}>} Value))"}>} CaseID, -Value))',
    '$(=FirstSortedValue(distinct{<Value={"<=$(=Median({<Value= {'>$(=fractile(Value, 0.5))<=$(=fractile(Value, 0.75))'}>} Value))"}>} CaseID, -Value))',
    '$(=FirstSortedValue(distinct{<Value={"<=$(=Median({<Value= {'>$(=fractile(Value, 0.75))<=$(=fractile(Value, 1))'}>} Value))"}>} CaseID, -Value))'
    ),
    CaseID,
    Null()
    )
  4. Uncheck Show Null Values for CaseID.
  5. The resultant table will look like this:
    How to do it…
  6. As you can see, every quartile is now showing only the claim corresponding to the median value in each quartile.

How it works…

The calculated dimension for CaseID gives us the claims corresponding to the median values in each quartile. As you can see, a Match() function is being used to match the CaseID with each of the four expressions within.

Let's decipher the first expression inside the Match() function:

'$(=FirstSortedValue(distinct
{<Value={"<=$(=Median({<Value={'>=$(=fractile(Value, 0))<=$(=fractile(Value, 0.25))'}>} Value))"}>} CaseID, -Value))'

The details of the expressions are as follows:

  • The innermost set gives us the range of values which are between the 0th quartile value and the 25th Quartile value
  • The Median() function then gives us the value which lies at the median of this range
  • The FirstSortedvalue() returns the value of the output field (CaseID) based on the sorted values of the value field

In situations where the number of claims in any quartile is an even number, there will be two claims which will correspond to the median values. In such a scenario, we want to select only the claim which is higher in the sorting order. Hence, we use a –Value as the sort weight.

There's more…

Similar to medians, we can derive the quartiles within quartiles using the Fractile() function.

See also

  • Using the Fractile() function to generate quartiles
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset