# GetStateCounts

The GetStateCounts() chart function is used to calculate the total number of unique values matching the specified selection states.

With GetStateCounts(), you can combine the calculations from the following functions into a single function call: GetSelectedCount(), GetNotSelectedCount(), GetAlternativeCount(), GetPossibleCount(), and GetExcludedCount(). The count of selected excluded values is also available to add to the calculation. You can specify whether each function calculation adds or subtracts from the returned total.

Syntax:

GetStateCounts (field\_name, state\_name [, state\_type1,...state\_typeN]))

**Return data type:** integer

Arguments

| Argument | Description |
| --- | --- |
| field\_name | The field for which you are calculating the selection state. A non-existing field name results in a null outcome. |
| state\_name | The name of the alternate state. If the argument is empty (`''`) or null, the inherited alternate state is used. Use `$` to explicitly use the default state. A named (non-empty) state name that doesn't match an existing state results in a null outcome. |
| state\_type | A list of one or more state types for the field value. These state types will be aggregated into a count. A state type is specified using a key. Enter each key with single quotation marks.<br><br>When this argument is omitted, the function returns a string with all available state counts for the field, in the same order as the enumeration.<br><br>For a list of states you can use, see the table below. |

State types are referenced using specific keys. You can use the numeric or textual version of the key. Combine more than one key in the same expression to further customize the result. You can subtract the state count from the total, rather than adding it. To do this, use the textual key and prefix the state type with a minus symbol (`-`).

The state type, and if applicable, the minus symbol, need to be enclosed in a set of single quotation marks.

Keys for each field state type

| Field state type | Description | Numeric key | Textual key |
| --- | --- | --- | --- |
| Selected | Includes selected values in the calculation. For the equivalent function, see [GetSelectedCount](https://help.qlik.com/en-US/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/FieldFunctions/getselectedcount.htm). | 1 | S |
| Optional | Includes optional (not selected, but possible to select) values in the calculation. For the equivalent function, see [GetPossibleCount](https://help.qlik.com/en-US/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/FieldFunctions/getpossiblecount.htm). | 2 | O |
| Deselected | Includes not-selected values in the calculation. This state type is only available when the field is in and-mode.<br><br>This state type returns the same calculation that the GetNotSelectedCount() function returns, assuming the include\_excluded argument in that function is set to the default value of False. For more information about GetNotSelectedCount(), see [GetNotSelectedCount](https://help.qlik.com/en-US/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/FieldFunctions/GetNotSelectedCount.htm). | 3 | D |
| Alternative | Includes alternative values in the calculation. For the equivalent function, see [GetAlternativeCount](https://help.qlik.com/en-US/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/FieldFunctions/getalternativecount.htm). | 4 | A |
| Excluded | Includes excluded (not selected) values in the calculation. For the equivalent function, see [GetExcludedCount](https://help.qlik.com/en-US/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/FieldFunctions/getexcludedcount.htm). | 5 | X |
| Selected excluded | Includes selected excluded values in the calculation. | 6 | XS |

## When to use it

With GetStateCounts(), you can calculate a custom selection state. The function allows you to consolidate multiple function calls into a single function call, simplifying the process of writing your expression.

For example, you might need to calculate the total number of excluded, alternative, and selected excluded values for a field. You can use GetStateCounts() to calculate this total.

Examples and results

| Examples | Results |
| --- | --- |
| `=GetStateCounts(ProductName, Null(), 'S')` | Returns the selected count for ProductName, in the inherited alternate state. |
| `=GetStateCounts(ProductName, '', 'A', 'XS')` | Returns the total count of the alternative and selected excluded values for ProductName. The inherited alternate state is used. |
| `=GetStateCounts(ProductName, '', 'S', 'XS')` | Returns the total user selection count for ProductName, in the inherited state. |
| `=GetStateCounts(ProductName, '', 'X', , 'A', 'XS')` | Returns the total count of excluded, selected excluded, and alternative values for ProductName. The inherited alternate state is used. |
| Given that the ProductName field is in and-mode.<br><br><br>`=GetStateCounts(ProductName, '', 'D', '-O')` | Returns the number of not-selected values, subtracted by the number of possible values, for ProductName. The inherited alternate state is used. |
| `=GetStateCounts(ProductName, '$', 'O')` | Returns the possible count for ProductName, in the default alternate state. |
| `=GetStateCounts(ProductName, 'StateA', 'S')` | Returns the selected count of ProductName, in the alternate state named StateA. |

## Example 1 – Counting total number of user selections (including selected excluded values)

[](https://help.qlik.com/en-US/qlikview/Subsystems/Client/Content/QV_QlikView/ChartFunctions/FieldFunctions/getstatecounts.htm)

#### Chart expression

Overview

The GetSelectedCount() function returns the total number of selected values, not including the total number of selected excluded values. With GetExcludedCount(), you can include the count of selected excluded values in the chart's calculation.

To get started, add the load script below to your document.

Load script

```
Names:
LOAD * inline [
First name|Last name|Initials|Has cellphone
John|Anderson|JA|Yes
Sue|Brown|SB|Yes
Mark|Carr|MC|No
Peter|Devonshire|PD|No
Jane|Elliot|JE|Yes
Peter|Franc|PF|Yes ] (delimiter is '|');
```

Results

Do the following:

1. Load the data and create two listboxes in your sheet.

2. Add First name as a dimension in the first listbox. Add Last name as a dimension in the second listbox.

3. Next, create a straight table with this measure:

   `=GetStateCounts([First Name],'', 'S','XS')`

4. In the First name listbox, select the values Mark and Peter. In the Last name filter listbox, select the value Carr.

5. Observe that the straight table shows a value of 2. This reflects that all values you selected, including the selected excluded value Peter, are counted.

## Example 2 – Combining excluded, selected excluded, and alternative counts

[](https://help.qlik.com/en-US/qlikview/Subsystems/Client/Content/QV_QlikView/ChartFunctions/FieldFunctions/getstatecounts.htm)

#### Chart expressions

Overview

The GetExcludedCount() function returns the total number of unique values that are not selected and excluded. If you want to include selected excluded and alternative counts in your visualization, you can use the GetStateCounts() function as follows.

To get started, add the load script below to your document.

Load script

```
Names:
LOAD * inline [
First name|Last name|Initials|Has cellphone
John|Anderson|JA|Yes
Sue|Brown|SB|Yes
Mark|Carr|MC|No
Peter|Devonshire|PD|No
Jane|Elliot|JE|Yes
Peter|Franc|PF|Yes ] (delimiter is '|');
```

Results

Do the following:

1. Load the data and create two listboxes in your sheet.

2. Add First name as a dimension in the first listbox. Add Last name as a dimension in the second listbox.

3. Next, create a straight table with this expression:

   `=GetStateCounts([First name], '', 'X', 'A', 'XS')`

4. In the First name listbox, select the values John and Peter. In the Last name listbox, select the value Franc.

5. Observe that the straight table shows a value of 4. This reflects that the selected excluded value John is included in the count, in addition to the count of the excluded values.

6. Edit the straight table and replace the existing expression with the following:

   `=GetStateCounts([Last name], '', 'X', 'A', 'XS')`

7. Keeping the existing selections, select the value Anderson in the Last name expression.

8. Observe that the straight table still shows a value of 4. This reflects that the alternative value Devonshire is included in the count, in addition to the count of the excluded values.