# Calculating a Percentage Rank or Percentile

**URL:** https://community.holistics.io/t/calculating-a-percentage-rank-or-percentile/2113
**Category:** Ask the Community
**Created:** [September 26, 2024, 3:31pm UTC](https://community.holistics.io/t/calculating-a-percentage-rank-or-percentile/2113 "2024-09-26T15:31:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hannah.paxton](https://community.holistics.io/user_avatar/community.holistics.io/hannah.paxton/32/2117_2.png) [@hannah.paxton](https://community.holistics.io/u/hannah.paxton)
#### Post date: [September 26, 2024, 3:31pm UTC](https://community.holistics.io/t/calculating-a-percentage-rank-or-percentile/2113/1 "2024-09-26T15:31:58Z")

</div>

Hello,

Is there a way I can calculate a percentage rank or a percentile within AQL? I’m currently using a window function to rank rows by a certain metric, but I’m having a hard time getting the max of the rank to complete the percentage rank calculation.

---

<div class="post-metadata">

### Author: ![tan](https://community.holistics.io/user_avatar/community.holistics.io/tan/32/358_2.png) [@tan](https://community.holistics.io/u/tan)
#### Post date: [September 26, 2024, 4:46pm UTC](https://community.holistics.io/t/calculating-a-percentage-rank-or-percentile/2113/2 "2024-09-26T16:46:26Z")

</div>

In this case, I think you can do something like this

```js
metric rank_asc = rank(order: count_orders);
metric sample_size = window_count(count_orders, ..);
metric percentile = (rank_asc - 1) * 1.0 / (sample_size - 1);

```

This use the same formula as Excel:  
 ![CleanShot 2024-09-26 at 23.43.51](https://community.holistics.io/uploads/default/original/2X/8/8be19cf800ccbf444da534ec7bd329ffcf9014f2.png)

Where:

- **Number of values lower than the current value** is calculated as:  
 ![CleanShot 2024-09-26 at 23.46.05](https://community.holistics.io/uploads/default/original/2X/f/fe3e2c65e38e431540035f56846cc24c5c0bb3d5.png)
- **Sample size** is a simple `window_count` on the full range of data.

You can see this in action here

 ![CleanShot 2024-09-26 at 23.37.19](https://community.holistics.io/uploads/default/original/2X/e/e617f8a23c4adb13ae5060703a3bafcd9e8f81c8.png)

---

<div class="post-metadata">

### Author: ![hannah.paxton](https://community.holistics.io/user_avatar/community.holistics.io/hannah.paxton/32/2117_2.png) [@hannah.paxton](https://community.holistics.io/u/hannah.paxton)
#### Post date: [September 26, 2024, 5:36pm UTC](https://community.holistics.io/t/calculating-a-percentage-rank-or-percentile/2113/3 "2024-09-26T17:36:48Z")

</div>

Exactly what I was looking for - thank you!
