Skip to main content

filterRows

Filters a table region by retaining rows that return true for the filter conditions.

The first parameter takes a table region, the second specifies which columns to use in the filter, and the third defines the filter conditions. For the third parameter, use a predefined helper function or write a lambda expression that returns a Boolean true or false.

How to filter tables is covered in greater depth in Filtering > Filtering a table region.

Note

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

Parameters

  • INPUT (table region)

    The table region to filter. Use WholeTable to filter the whole table.

  • COLUMNS (column, string, or list)

    The columns to use in the filter. Give either the column variable or the column name as a string. When using multiple columns, give each variable/name in a list. See the examples for guidance.

  • FUNCTION (function)

    A function defining the filter conditions. Use a predefined helper function or write a lambda expression that returns a Boolean true or false. Use a lambda to apply two or more conditions.

Helper functions

The helper functions are for single-condition filters only. They work by comparing a column value against a constant, which is typically a hard-coded value or a variable returning a value.

Helper Function

Description

Equivalent Lambda Expression

gt(constant)

Tests if column value is greater than the constant.

x -> x > constant

gte(constant)

Tests if column value is greater than or equal to the constant.

x -> x >= constant

lt(constant)

Tests if column value is less than the constant.

x -> x < constant

lte(constant)

Tests if column value is less than or equal to the constant.

x -> x <= constant

eq(constant)

Tests if column value is equal to the constant.

x -> x == constant

Tip

You must use a lambda expression to apply two or more filter conditions.

Examples

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

Code

Quarter

Branch

Sales

Target

Row 1

1001

Q1

Boston

294,293.49

300,000

Row 2

2001

Q1

Chicago

403,603.17

350,000

Row 3

3001

Q1

New York

457,359.45

450,000

Row 4

4001

Q1

Pittsburgh

279,238.52

250,000

Row 5

1002

Q2

Boston

307,856.14

300,000

Row 6

2002

Q2

Chicago

395,187.43

350,000

Row 7

3002

Q2

New York

439,211.84

450,000

Row 8

4002

Q2

Pittsburgh

282,555.26

250,000

Important

The first table column — Code in the table above — contains the row names.

Each value in this first column must be unique; therefore, you cannot filter by this column.

Single-condition filters

These examples show how to apply a single-condition filter. This can be done with a helper function in the third parameter, but you can use a lambda expression if you prefer. We've provided example ATL for both.

To retain rows with a Branch value equal to 'Boston':

[[filterRows(WholeTable, Branch, eq('Boston'))]]

OR

[[filterRows(WholeTable, Branch, x -> x == 'Boston')]]

The filtered table region:

 

Quarter

Branch

Sales

Target

1001

Q1

Boston

294,293.49

300,000

1002

Q2

Boston

307,856.14

300,000

Note

You can give the column name (as a string) rather than the column variable:

  • [[filterRows(WholeTable, 'Branch', eq('Boston'))]]

  • [[filterRows(WholeTable, 'Branch', x -> x == 'Boston')]]

To retain rows with a Sales value greater than or equal to 400,000:

[[filterRows(WholeTable, Sales, gte(400000))]]

OR

[[filterRows(WholeTable, Sales, x -> x >= 400000)]]

The filtered table region:

 

Quarter

Branch

Sales

Target

2001

Q1

Chicago

403,603.17

350,000

3001

Q1

New York

457,359.45

450,000

3002

Q2

New York

439,211.84

450,000

Note

In a "Describe Row in Context" project, the column variable is SalesColumn:

  • [[filterRows(WholeTable, SalesColumn, x -> x >= 400000)]]

If you give the column name as a string, the ATL works in both project types:

  • [[filterRows(WholeTable, 'Sales', x -> x >= 400000)]]

Multiple-condition filters

To apply two or more filter conditions, you must use a lambda expression in the third parameter.

When filtering by multiple columns, the input to the second parameter must be a list. These examples use a list of strings (column names) rather than a list of column variables, but you can use either.

To retain rows with a Quarter value equal to 'Q2' AND a Sales value greater than 300,000:

[[filterRows(WholeTable, ('Quarter', 'Sales'), (x,y) -> x == 'Q2' && y > 300000)]]

The filtered table region:

 

Quarter

Branch

Sales

Target

1002

Q2

Boston

307,856.14

300,000

2002

Q2

Chicago

395,187.43

350,000

3002

Q2

New York

439,211.84

450,000

To retain rows when Quarter = 'Q2' AND Sales exceeds 300,000 AND Sales exceeds Target:

[[filterRows(WholeTable, ('Quarter', 'Sales', 'Target'), (x,y,z) -> x == 'Q2' && y > 300000 && y > z)]]

The filtered region:

 

Quarter

Branch

Sales

Target

1002

Q2

Boston

307,856.14

300,000

2002

Q2

Chicago

395,187.43

350,000

You can use other ATL functions to help define filter conditions. For example, to retain rows where Sales exceeds 350,000 AND Sales exceeds Target by more than 12.5%, you can use the percentageChange function.

[[filterRows(WholeTable, (Sales, Target), (x,y) -> x > 350000 && percentageChange(x,y) > 12.5)]]

The filtered table region:

 

Quarter

Branch

Sales

Target

2001

Q1

Chicago

403,603.17

350,000

2002

Q2

Chicago

395,187.43

350,000

Tip

The examples above use the AND operator (&&) to combine two or more logical conditions; however, you might use OR (||) instead. See Syntax for conditionals for guidance.

Working with filtered table regions

You can interrogate a filtered table region with other ATL functions.

Here's how you might get a sales performance summary for Q1:

[[

Q1data = filterRows(WholeTable, 'Quarter', eq('Q1'))

Q1sales = totalVal(columnsInRegion(Q1data, Sales))

Q1target = totalVal(columnsInRegion(Q1data, Target))

performance = percentageChange(Q1sales, Q1target)

direction = performance > 0 ? 'exceeded' : 'fell short of'

joinStrings('Q1 sales ', direction, ' the target by ', abs(performance), '%.')

]]

The output text:

Q1 sales exceeded the target by 6.25%.

Tip

For a wider range of examples, see Filtering > Filtering a table region.