# Calculating Median

**URL:** <https://community.glideapps.com/t/calculating-median/62572>\
**Category:** Ask for Help\
**Created:** [June 8, 2023, 2:51pm UTC](https://community.glideapps.com/t/calculating-median/62572 "2023-06-08T14:51:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameraville](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/cameraville/32/60231_2.png) [@Cameraville](https://community.glideapps.com/u/Cameraville)\
**Post date:** [June 8, 2023, 2:51pm UTC](https://community.glideapps.com/t/calculating-median/62572/1 "2023-06-08T14:51:50Z")

</div>

Can anyone provide an **efficient** way to calculate median? I have a list of ‘things’ with relations to their various prices and I would like to show the median price for each thing. I have used rollups to easily get min, max, and average. Median is proving difficult but useful for my users. I am an experienced user. Thank you.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [June 8, 2023, 2:56pm UTC](https://community.glideapps.com/t/calculating-median/62572/2 "2023-06-08T14:56:49Z")

</div>

Just thinking out aloud…

- Use a Lookup column to create an array of values
- [Sort the array](https://www.glideapps.com/plugins/sort-array)
- Determine the [length of the array](https://www.glideapps.com/plugins/array-length)
- Pick out the one in the middle using a single value column

 ![CleanShot 2023-06-08 at 23.32.14@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/4/d/4d309b4d286261d97fdba4b50fe90b2e316f3eb0.png)

Alternatively, create a Joined List and feed that into a JavaScript column:

 ![CleanShot 2023-06-08 at 23.28.07@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/f/3/f33fb839ab5f3136974e7e621e9dc5b4559ab5f9.png)

```auto
function median(list) {
  const arr = list.split(', ').map(Number);
  const sorted = arr.sort((a, b) => a - b);
  const middle = Math.floor(sorted.length / 2);
  if (sorted.length % 2 === 0) {
      return (sorted[middle - 1] + sorted[middle]) / 2;    
  }
  return sorted[middle];
}

return median(p1);

```

---

<div class="post-metadata">

**Author:** ![Cameraville](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/cameraville/32/60231_2.png) [@Cameraville](https://community.glideapps.com/u/Cameraville)\
**Post date:** [June 8, 2023, 3:31pm UTC](https://community.glideapps.com/t/calculating-median/62572/3 "2023-06-08T15:31:19Z")

</div>

Thanks, but there is no middle value if the quantity of value is even.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [June 8, 2023, 3:34pm UTC](https://community.glideapps.com/t/calculating-median/62572/4 "2023-06-08T15:34:16Z")

</div>

The JavaScript option that I just added should deal with that.

---

<div class="post-metadata">

**Author:** ![Cameraville](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/cameraville/32/60231_2.png) [@Cameraville](https://community.glideapps.com/u/Cameraville)\
**Post date:** [June 8, 2023, 3:57pm UTC](https://community.glideapps.com/t/calculating-median/62572/5 "2023-06-08T15:57:53Z")

</div>

Thanks a lot I will check it out. I checked your first message on my phone and it showed no images.

---

<div class="post-metadata">

**Author:** ![Cameraville](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/cameraville/32/60231_2.png) [@Cameraville](https://community.glideapps.com/u/Cameraville)\
**Post date:** [June 10, 2023, 6:53am UTC](https://community.glideapps.com/t/calculating-median/62572/6 "2023-06-10T06:53:33Z")

</div>

So far I have successfully been able to make all my apps with no spreadsheet formulas or custom code - I aim for this over performance concerns. Additionally I do not have experience with javascript and I could not find sufficient documentation for this in glide, so I’m lacking in this area. Could you please show me what the Edit column for Median looks like? I have not been able to get this to work. Thank you very much.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [June 10, 2023, 8:09am UTC](https://community.glideapps.com/t/calculating-median/62572/7 "2023-06-10T08:09:32Z")

</div>

Sure, here is what it looks like:

 ![CleanShot 2023-06-10 at 16.04.23@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/f/1fc7b77592b3b8504435acb58f8da4d3da5712b4.jpeg)

- The column type is JavaScript (just search for it)
- Paste the code that I gave into the Data section
- Pass your Joined List of values as `p1`

Note that the JavaScript code expects the values to be joined with a comma + space (`, `), which is the default for the Joined List column. If you were to change that then you would need to adjust the second line of code. For example, if you got rid of the space and just used a comma, then line 2 would become:

```auto
const arr = list.split(',').map(Number);

```

---

<div class="post-metadata">

**Author:** ![Cameraville](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/cameraville/32/60231_2.png) [@Cameraville](https://community.glideapps.com/u/Cameraville)\
**Post date:** [June 10, 2023, 8:58am UTC](https://community.glideapps.com/t/calculating-median/62572/8 "2023-06-10T08:58:50Z")

</div>

> [@Darren\_Murphy](#):
>
> ```auto
> function median(list) {
> const arr = list.split(', ').map(Number);
> const sorted = arr.sort((a, b) => a - b);
> const middle = Math.floor(sorted.length / 2);
> if (sorted.length % 2 === 0) {
> return (sorted[middle - 1] + sorted[middle]) / 2;    
> }
> return sorted[middle];
> }
> 
> return median(p1);
> 
> ```

Thank you Darren. I can see from your extra info that I had done it right. The problem was that my dataset was prices and had a currency symbol it with resulted in 'NaN’s. I assumed glide would have used them in the joined list for display purposes only - as rollups work just fine with any units. With your code and see help from GTP I accounted was able to account for this, and output the result to the nearest whole number and dsiplay my original number format. It works great thank you.

```auto
function formatCurrency(amount) {
  const formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'EUR',
    maximumFractionDigits: 0,
  });
  return formatter.format(amount);
}

function median(list) {
  if (!list || list.trim() === '' || list === '0') {
    return;
  }
  
  const arr = list.split(', ').map(price => Number(price.replace(/[^0-9.-]+/g, '')));
  const sorted = arr.sort((a, b) => a - b);
  const middle = Math.floor(sorted.length / 2);
  let medianValue;
  if (sorted.length % 2 === 0) {
    medianValue = (sorted[middle - 1] + sorted[middle]) / 2;
  } else {
    medianValue = sorted[middle];
  }
  
  return formatCurrency(medianValue);
}

return median(p1);

```
