# Data Selection Functions

A data selection function selects data from a source. The source can be either a selection or results of a query. The function returns a table of data, where each column has a name, and each row has an index, starting with 0. From the table, you can select one or more rows, one or more columns, or a cell to include in your interaction.

In cases where multiple rows or columns of data are selected, the function returns a two-dimensional array. When a single row or single column is selected, the function returns a one-dimensional array. When a cell is selected, the function returns a scalar value. The function returns null if the source is empty. If the function tries to select data that doesn’t exist, an interaction error occurs. For example, the table only has two rows, but you try to select data from the third row.

## cell Function

Returns a single cell of data as a scalar, like `"This salesperson rocks"`, `2`, or `null`. An error occurs if the `rowIndex` isn’t an integer, the `columnName` isn’t a string, or the cell doesn’t exist in the table.

### Syntax

```nolang
cell(source, rowIndex, columnName)
```

### Arguments

| Argument   | Description                                                           |
| ---------- | --------------------------------------------------------------------- |
| source     | (Required) Specify the name of the query and `selection` or `result`. |
| rowIndex   | (Required) Specify the row using its index. Row index starts at 0.    |
| columnName | (Required) Specify the column name.                                   |

This example is based on the `myStep` source query. Assume that `myStep.selection` retrieves these rows from the query.

```json
[
    {stateName: ‘CA’, Amount:100},
    {stateName: ‘TX’, Amount:200},
    {stateName: ‘OR’’, Amount:300},
    {stateName: ‘AL’, Amount:400},
]
```

Although CRM Analytics doesn’t store this data as a table, let’s show the data in this format to make it easier to understand the example.

| (row index) | stateName | Amount |
| ----------- | --------- | ------ |
| 0           | CA        | 100    |
| 1           | TX        | 200    |
| 2           | OR        | 300    |
| 3           | AL        | 400    |

### Example

Function:

```javascript
cell(myStep.selection, 1, "stateName")
```

Output:

```nolang
"TX"
```

## column Function

Returns one column of data as a one-dimensional array or multiple columns of data as a two-dimensional array.

### Syntax

```javascript
column(source, [columnNames...])
```

### Arguments

| Argument    | Description                                                                                                                                                                                                                                                                                                     |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| source      | (Required) Specify the name of the query and `selection` or `result`.                                                                                                                                                                                                                                           |
| columnNames | (Required) Specify an array of column names. The order of the listed columns affects the order that the columns appear in the output. The order is important for serialization functions. For example, the `asOrder` function requires the first element to be a field name and the second to be the direction. |

These examples are based on the `myStep` source query. Assume that `myQuery.selection` retrieves these rows from the query.

```json
[
    {stateName: ‘CA’, Amount:100},
    {stateName: ‘TX’, Amount:200},
    {stateName: ‘OR’’, Amount:300},
    {stateName: ‘AL’, Amount:400},
]
```

### Examples

Although CRM Analytics doesn’t store this data as a table, let’s show the data in this format to make it easier to understand the examples that follow.

| (row index) | stateName | Amount |
| ----------- | --------- | ------ |
| 0           | CA        | 100    |
| 1           | TX        | 200    |
| 2           | OR        | 300    |
| 3           | AL        | 400    |

Function:

```javascript
column(myStep.selection, ["stateName"])
```

Output:

```sql
["CA", "TX", "OR", "AL"]
```

***

Function:

```javascript
column(myStep.selection, [])
```

Output:

```sql
[ ["CA", "TX", "OR", "AL"], ["100", "200”," "300", "400"] ]
```

## row Function

Returns one row of data as a one-dimensional array or multiple rows as a two-dimensional array. For selection interactions, you typically use this function to return the first row or all rows. For results interactions, you might want specific rows. To determine the row index, display the query results in a values table.

### Syntax

```javascript
row(source), [rowIndices...], [columnNames...])
```

### Arguments

| Argument    | Description                                                                                                                                                                                                                                 |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| source      | (Required) Specify the name of the query and `selection` or `result`.                                                                                                                                                                       |
| rowIndices  | (Required) Specify an array of row indices, where each element of the array identifies a row. Row index `0` identifies the first row. To include all rows, specify an empty array.                                                          |
| columnNames | (Optional) Specify an array of column names to select and order them. If not specified, all columns are selected and every row has the same order of columns. However, that order isn't guaranteed to be the same across different queries. |

These examples are based on the `myStep` source query. Assume that `myStep.selection` retrieves these rows from the query.

```json
[
    {stateName: ‘CA’, Amount:100},
    {stateName: ‘TX’, Amount:200},
    {stateName: ‘OR’’, Amount:300},
    {stateName: ‘AL’, Amount:400},
]
```

### Examples

Although CRM Analytics doesn’t store this data as a table, let’s show the data in this format to make it easier to understand the examples that follow.

| (row index) | stateName | Amount |
| ----------- | --------- | ------ |
| 0           | CA        | 100    |
| 1           | TX        | 200    |
| 2           | OR        | 300    |
| 3           | AL        | 400    |

Function:

```javascript
row(myStep.selection, [0], ["Amount"])
```

Output:

```sql
["100"]
```

***

Function:

```javascript
row(myStep.selection, [0,2], [])
```

Output:

```sql
[ ["CA", "100"], ["OR", "300"] ]
```

***

Function:

```javascript
row(myStep.selection, [], ["stateName"])
```

Output:

```sql
[ ["CA"], ["TX"], ["OR"], ["AL"] ]
```

***

Function:

```javascript
row(myStep.selection, [0,2], ["stateName", "Amount"])
```

Output:

```sql
[ ["CA", "100"], ["OR", "300"] ]
```
