Top() evaluates an expression at the first (top) row of a column segment in a table. The row for which it is calculated depends on the value of offset, if present, the default being the top row. For charts other than tables, theTop() evaluation is made on the first row of the current column in the chart's straight table equivalent.
Syntax:
Top([TOTAL] expr [ , offset [,count
]])
Return data type: dual
Arguments
Argument
Description
expr
The expression or field containing the data to be measured.
offset
Specifying an offset of n, greater than 1, moves the evaluation of the expression
down n rows below the top row.
Specifying a negative offset number makes the Top
function work like the Bottom function
with the corresponding positive offset number.
count
By specifying a third parameter count
greater than 1, the function will return a range of count values, one for each of the last count rows of the current column segment. In this form, the
function can be used as an argument to any of the special range
functions. Range functions
TOTAL
If the table is one-dimensional or if the qualifier TOTAL is used as argument, the current column segment is always equal to the
entire column.
Information noteA column segment is defined as a consecutive subset of cells having the same values for the dimensions in the current sort order. Inter-record chart functions are computed in the column segment excluding the right-most dimension in the equivalent straight table chart. If there is only one dimension in the chart, or if the TOTAL qualifier is specified, the expression evaluates across full table.
Information noteIf the table or table equivalent has multiple vertical dimensions, the
current column segment will include only rows with the same values as
the current row in all dimension columns, except for the column showing the last dimension in the inter-field sort order.
Limitations:
Recursive calls will return NULL.
Sorting on y-values in charts or sorting by expression columns in tables is not allowed when this chart function is used in any of the chart's expressions. These sort alternatives are therefore automatically disabled. When you use this chart function in a visualization or table, the sorting of the visualization will revert back to the sorted input to this function.
Example: Chart expressions
Example
Result
Top( Sum(Sales) )
Returns the value of Sales for the first (top) row of a column segment in a table.
Example - Top fundamentals
Overview
Open the Data load editor and add the load script below to a new section.
The load script contains:
A dataset which is loaded into a data table called Example.
Load the data and open a sheet. Create a new table and add this field as a dimension:
Year
Create the following measures:
=Sum(Sales), to find the annual sales.
=Top(Sum(Sales)), to find the sales value for the first year, that is the first (top) row of a column in a table.
=Sum(Sales) - Top(Sum(Sales)), to calculate the variance between the Sales value of the current row and the value for the first (top) row of a column.
=Sum(Sales)-Top(Sum(Sales), 3), to calculate the variance between the sales value of the current row and the value of the 3rd row from the top.
=(Sales - Top(Sales)) / Top(Sales), to return the change in Sales as a ratio / percentage as compared to first row. To show this value as a percentage, under Number formatting, select Number > Formatting Simple > 12.34%.
Sort the table on the dimension Year in ascending order.
Results table
Year
Sum(Sales)
Top(Sum(Sales))
Sum(Sales) - Top(Sum(Sales))
Sum(Sales)-Top(Sum(Sales), 3)
(Sales - Top(Sales)) / Top(Sales)
Totals
7200
1000
6200
6000
-
2019
1000
1000
0
-200
0.00%
2020
1500
1000
500
300
50.00%
2021
1200
1000
200
0
20.00%
2022
1800
1000
800
600
80.00%
2023
1700
1000
700
500
70.00%
Looking at the results, you can compare the Sales values in the current period with the top (or first) period.
The Sum(Sales) - Top(Sum(Sales)) measure returns the change in Sales values comparing the current period to the first period.
The Sum(Sales)-Top(Sum(Sales), 3) measure returns the sum of Sales for the current row minus the third period from the top.
The =(Sales - Top(Sales)) / Top(Sales) measure returns the change in Sales compared to the first period as a percentage.
Tip noteThis function can also be used in charts other than tables, for example bar charts.
Do the following:
Create a bar chart, and then add this field as a dimension:
Year
Create the following measure:
=Sum(Sales)- Top(Sum(Sales)), to return the variance in sales between the current period and the top period.
Bar chart showing results for the Top function
Using the Top function in a bar chart provides a visual comparison of the variance between the sales of the current period versus the top (or first) period.
Example - Top scenario
Overview
Open the Data load editor and add the load script below to a new section.
The load script contains:
A dataset which is loaded into a data table called Example.
Load the data and open a sheet. Create a new table and add this field as a dimension:
Customer
Create the following measures:
=Sum(Sales), to calculate the sum of sales.
=Top(Sum(Sales)), to show the sum of sales of the Customer in the top row.
=Sum(Sales)+Top(Sum(Sales)), to add the Sum(Sales) value of the current Customer with the Customer in the top row.
=Sum(Sales)+Top(Sum(Sales), 3), to add the Sum(Sales) value of the current Customer with the value for the Customer that is third from the top row.
Results table
Customer
Sum(Sales)
Top(Sum(Sales))
Sum(Sales)+Top(Sum(Sales))
Sum(Sales)+Top(Sum(Sales), 3)
Totals
2566
587
3153
3249
Astrida
587
587
1174
1270
Betacab
539
587
1126
1222
Canutilty
683
587
1270
1366
Divadip
757
587
1344
1440
The measure Top(Sum(Sales)) returns 587 for all rows because this is the value of the top row: Astrida.
For the measure Sum(Sales)+Top(Sum(Sales)), the row Betacab returns 1126, which is the result of adding the Sum(Sales) value for Betacab and Astrida, the Customer in the top row.
For the measure Sum(Sales)+Top(Sum(Sales), 3), the argument offset is set to 3. The expression adds the Sum(Sales) value for the current row and the third row from the top, Canutility. For example, the value 1440 is returned for customer Divadip. This is the result of adding the Sum(Sales) value for Divadip and the SumSales for Canutility (757+683).