Skip to main content
Solved

How to compute a median ?

  • December 11, 2023
  • 4 replies
  • 644 views

Forum|alt.badge.img+9

Hi,

How can I do a median in Pigment ?

Thanks

Best answer by Matt D

Hi Alexandre,

 

We don’t have a function for calculating the median right now, but you can calculate it by combining a few modifiers and functions in a formula like the one below:

rev2[RANK(rev2, Month, ASC, Sequential) IN (ROUNDDOWN((rev2[REMOVE COUNT: Country] + 1) / 2):ROUNDUP((rev2[REMOVE COUNT: Country] + 1) / 2))][REMOVE AVG: Country]

It might look a little complex, but see the below example to see it in action. We are calculating the median of the revenue across all countries for a given month.

 

Hopefully you can leverage this example for your own modelling :)
 

Cheers,

4 replies

Matt D
Employee
Forum|alt.badge.img+8
  • Employee
  • 51 replies
  • Answer
  • December 11, 2023

Hi Alexandre,

 

We don’t have a function for calculating the median right now, but you can calculate it by combining a few modifiers and functions in a formula like the one below:

rev2[RANK(rev2, Month, ASC, Sequential) IN (ROUNDDOWN((rev2[REMOVE COUNT: Country] + 1) / 2):ROUNDUP((rev2[REMOVE COUNT: Country] + 1) / 2))][REMOVE AVG: Country]

It might look a little complex, but see the below example to see it in action. We are calculating the median of the revenue across all countries for a given month.

 

Hopefully you can leverage this example for your own modelling :)
 

Cheers,


Forum|alt.badge.img+9
  • Author
  • Master Author
  • 31 replies
  • December 11, 2023

Hi, thank you for the tips! :)

 


Forum|alt.badge.img+6
  • Master Trendsetter
  • 14 replies
  • January 24, 2024

Hey @Matt D thanks for this response.  I’m trying to do something similar but with slightly more dimensionality.  Using your example, let’s say the rev2 metric was structured by country, month, AND customer - what I’d like to do is calculate the median customer revenue amount by country & month.  My assumption is that I’m running into limitations with the “Group” portion of the RANK formula - in your example above it resets the ranking by the month dimension, but with what I’m doing I’d think it would need to reset rankings on month and country. 

Are you aware of any ways to accomplish something like this? 

EDIT: I believe I was able to find what I needed in the article on the RANK function, using a unique dimension property along with the “&” symbol to reset across multiple dimensions.  Thanks again for providing this example 

Thanks, 


Syuen
Employee
Forum|alt.badge.img+5
  • Employee
  • 21 replies
  • September 24, 2025

Hello, 
We’re happy to share that we now have a new Median aggregator using the REMOVE modifier which should help with these calculations!

Updated doc here: 

I hope this helps with needing to calculate Median on metrics with multiple dimensionality.