# TOPN

Rank your data based on a category for a particular measure, for example, you can display the top-performing region, based on revenue.

## 1. SETTOPN

### Syntax

```javascript
SETTOPN(CATEGORY_NAME, COLUMNS.COLUMNS_NAME, VALUE, BOOLEAN_ARG)
```

### Arguments

CATEGORY\_NAME - The category group within which to select the top range.

COLUMN\_NAME - The measure used to select the top range.

VALUE - The number of top ranks to display.

BOOLEAN\_ARG - Set to TRUE to enable ranking. Set to FALSE to disable ranking.

### Example

```
SETTOPN([Sub-Category], COLUMNS.[Qtr4 > Sales], 2, TRUE) #Shows the 2 sub categories with top Q4 sales
SETTOPN([Sub-Category], COLUMNS.[Qtr4 > Sales], 20, FALSE) #Shows the sub categories with top 20 percentage Q4 sales
SETTOPN([Sub-Category], COLUMNS.[Qtr4 > Sales], THIS, TRUE) #Use with a slider or stepper variable to pass the value for the 'THIS' parameter
SETTOPN([Sub-Category], COLUMNS.[Qtr4 > Sales], 2, THIS) #Control the 'THIS' parameter using a toggle variable button
```

<figure><img src="https://github.com/lumelinc/PowerTableDocs/blob/main/.gitbook/assets/image%20(1314).png" alt=""><figcaption><p>Top N</p></figcaption></figure>

## 2. SETBOTTOMN

### Syntax

```javascript
SETBOTTOMN(CATEGORY_NAME, COLUMNS.COLUMNS_NAME, VALUE, BOOLEAN_ARG)
```

### Arguments

CATEGORY\_NAME - The category group within which to select the bottom range.

COLUMN\_NAME - The measure used to select the bottom range.

VALUE - The number of bottom ranks to display.

BOOLEAN\_ARG - Set to TRUE to enable ranking. Set to FALSE to disable ranking.

### Example

```
SETBOTTOMN([Sub-Category], COLUMNS.[Qtr 1 > Profit], 3, TRUE) #Shows the 3 sub categories with the least Q1 profit
SETBOTTOMN([Sub-Category], COLUMNS.[Qtr4 > Sales], 20, FALSE) #Show the sub categories with the bottom 20 percentage of Q4 sales
```

<figure><img src="https://github.com/lumelinc/PowerTableDocs/blob/main/.gitbook/assets/image%20(1313).png" alt=""><figcaption><p>Bottom N</p></figcaption></figure>

## 3. SETTOPBOTTOMN

### Syntax

```javascript
SETBOTTOMN(CATEGORY_NAME, COLUMNS.COLUMNS_NAME, VALUE, BOOLEAN_ARG)
```

### Arguments

CATEGORY\_NAME - The category group within which to select the top and bottom range.

COLUMN\_NAME - The measure used to select the top and bottom range.

VALUE - The number of top and bottom ranks to display.

BOOLEAN\_ARG - Set to TRUE to enable ranking. Set to FALSE to disable ranking.

### Example

```
SETTOPBOTTOMN([Sub-Category], COLUMNS.[Grand Total > Profit], 1, TRUE) #Shows the sub category with the most and least overall profit
SETTOPBOTTOMN([Sub-Category], COLUMNS.[Grand Total > Profit], 10, FALSE) #Show the sub categories with the top and bottom 10 percentage of overall profit
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fabricplan.com/planning-sheets/formula-syntax/scripting-functions/topn.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
