top
Returns the top N values from the input data.
Note that here top doesn't mean highest. The ATL [[top(myList, 3)]]
returns the first three values in myList
, not the highest three. The function is typically used on already sorted data.
The return value is always a list, even when you request just one value.
Tip
Alternatively, use the syntax for slicing. See ATL Guide > Slicing for guidance.
Parameters
INPUT (list, column, row, array)
The input data. Can be a list, column, row, or JSON array.
N (number)
The number of values to return.
Examples
The first parameter can take a list.
ATL in Script | Resulting List | Printed Value |
---|---|---|
[[ myList = makeList = (20, 50, 30, 10, 40); top(myList, 1) ]] | (20) | 20 |
[[ myList = makeList = (20, 50, 30, 10, 40); top(myList, 3) ]] | (20, 50, 30) | 20, 50 and 30 |
[[ myList = makeList = (20, 50, 30, 10, 40); top(myList, 6) ]] | (20, 50, 30, 10, 40) | 20, 50, 30, 10 and 40 |
Note
If the value you give for N (second parameter) is greater than the number of values in the input list, the function returns the input list unchanged. See the last example above.
The top
function is typically applied to an already sorted list. For example:
ATL in Script | Resulting List | Printed Value |
---|---|---|
[[ myList = makeList = (20, 50, 30, 10, 40); sortedList = sortNumbers(myList); top(sortedList, 3) ]] | (10, 20, 30) | 10, 20 and 30 |
[[ myList = makeList = (20, 50, 30, 10, 40); sortedList = sortNumbersReverse(myList); top(sortedList, 3) ]] | (50, 40, 30) | 50, 40 and 30 |
Using top with table data
Assume a "Describe the Table" project with this data:
ID | Branch | Sales | Target | |
---|---|---|---|---|
Row 1 | 1001 | New York | 478,745.37 | 500,000 |
Row 2 | 1002 | Boston | 329,493.49 | 300,000 |
Row 3 | 1003 | Los Angeles | 467,359.45 | 450,000 |
Row 4 | 1004 | Chicago | 463,603.17 | 400,000 |
Row 5 | 1005 | Pittsburgh | 274,551.63 | 275,000 |
Row 6 | 1006 | Dallas | 311,842.26 | 300,000 |
Suppose you want to list the three top-selling branches. You can apply top
to a column, but first you need to sort the table by Sales in descending numerical order. The ATL solution is given below.
[[ sortedByHighestSales = sortByColumnNumbersReverse(WholeTable, 'Sales') branchesByHighestSales = columnsInRegion(sortedByHighestSales, Branch) topThreeBranches = top(branchesByHighestSales, 3) ]]
The printed result is:
New York, Los Angeles and Chicago
Note
In a "Describe the Table" project, the column variable is BranchColumn
.
Using top with JSON data
Assume a "Describe a JSON Object" project with this data:
{ "data": [ { "product": "Retine", "sales": 2497146.89 }, { "product": "Ointmo", "sales": 3486198.56 }, { "product": "Glower", "sales": 1756934.95 }, { "product": "Nutrali", "sales": 253771.24 } ] }
The "data" array contains four JSON objects. Each object contains three key–value pairs. Suppose you want to identify the top-selling product in the array. This requires a three-step process:
Sort the objects by sales in descending order.
Map the sorted array to a list of product names.
Use
top
to get the top product name.
The ATL solution is given below:
[[ sortedByHighestSales = sort(WholeJSON.data, (x,y) -> sign(y.sales - x.sales)) productsByHighestSales = map(sortedByHighestSales, object -> object.product) topProduct = top(productsByHighestSales, 1) joinStrings('The top-selling product is ', topProduct, '.') ]]
The printed output is:
The top-selling product is Ointmo.