Skip to main content

topRows

Returns the top N rows in a table region.

If N = 3, the output is the first three rows in the region, working top to bottom.

The output is always a table region, but a single-row region is printed as a list of values.

Note

Available in "Describe the Table" and "Describe Row in Context" projects only.

Parameters

  • TABLE REGION (table region)

    The input table region.

  • N (number)

    The number of rows to select.

    If N = 3, the output is the input region's first three rows.

Example

Assume a "Describe the Table" project with this data:

ID

Branch

salesRev

COGS

otherRev

otherExp

netProfit

Row 1

1

Aberdeen

14,000

8,000

2,000

1,500

6,500

Row 2

2

Edinburgh

33,000

19,000

2,000

3,500

12,500

Row 3

3

Inverness

16,000

8,250

1,200

1,500

7,450

Row 4

4

Glasgow

32,500

17,750

1,250

1,700

14,300

Row 5

5

Dundee

15,750

7,500

1,250

1,200

8,300

Row 6

6

Perth

14,500

8,000

1,000

1,400

6,100

To get the first three rows from the table:

[[topRows(WholeTable, 3)]]

The output is this table region:

Branch

salesRev

COGS

otherRev

otherExp

netProfit

1

Aberdeen

14,000

8,000

2,000

1,500

6,500

2

Edinburgh

33,000

19,000

2,000

3,500

12,500

3

Inverness

16,000

8,250

1,200

1,500

7,450

You might apply the function to a sorted table region. For example:

[[

sortedByHighestSales = sortByColumnNumbersReverse(WholeTable, 'salesRev')

topRows(sortedByHighestSales, 3)

]]

The output is this table region:

Branch

salesRev

COGS

otherRev

otherExp

netProfit

2

Edinburgh

33,000

19,000

2,000

3,500

12,500

4

Glasgow

32,500

17,750

1,250

1,700

14,300

3

Inverness

16,000

8,250

1,200

1,500

7,450

Here's how you could use topRows with other functions to generate narrative text:

[[

sortedByHighestSales = sortByColumnNumbersReverse(WholeTable, 'salesRev')

topThreeRows = topRows(sortedByHighestSales, 3)

branchNames = columnsInRegion(topThreeRows, Branch)

salesRevValues = columnsInRegion(topThreeRows, salesRev)

salesRevTotal = abbreviateNumber(currencyFormat(totalVal(salesRevValues)))

"The top three branches for sales — [[branchNames]] — combined for a total of [[salesRevTotal]]."

]]

The printed output text is:

The top three branches for sales — Edinburgh, Glasgow and Inverness — combined for a total of $81.5K.