> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://help.sigmacomputing.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://help.sigmacomputing.com/_mcp/server.

# CountDistinctIf

The **CountDistinctIf** function returns the number of unique values within a column or group for which all given conditions are true.  You can use [operators](/docs/operators-overview) in conditions.

The **CountDistinctIf** function is an aggregate function.

Aggregate functions evaluate one or more rows of data and return a single value.

In a table element, the aggregate is calculated for each grouping. For information on how to add a grouping with an aggregate calculation to a table, see [Group columns in a table](/docs/create-and-manage-tables#group-columns-in-a-table).

In a table with no groupings, the aggregate is calculated for each row. For information on how to calculate summary statistics across all rows in a table, see [Add summary statistics to a table](/docs/create-and-manage-tables#add-summary-statistics-to-a-table).

To learn more about using aggregate functions, see [Building complex formulas with grouped data](https://www.sigmacomputing.com/resources/training-videos/table-grouping-and-functions#complex-formulas).

## Syntax

`CountDistinctIf([field], condition 1, condition 2+...)`

The function has the following arguments:

<dl>
  <dt>
    field
  </dt>

  <dd>
    Required
  </dd>

  <dd>
    The column of values to be counted. Duplicate and Null or empty values are skipped.
  </dd>

  <dt>
    condition 1
  </dt>

  <dd>
    Required
  </dd>

  <dd>
    The condition to test. If the condition is true, then the row will be counted.
  </dd>

  <dt>
    condition 2+
  </dt>

  <dd>
    Optional
  </dd>

  <dd>
    Additional conditions to test. If multiple conditions are used, they must all be True for the row to be counted.
  </dd>
</dl>

## Examples

`CountDistinctIf([City], [Population] > 5000)`

Finds the number of unique city names where the population is greater than 5000.

`CountDistinctIf([City], [Population] > 5000 AND [Population] < 10000, [State] = "Ohio")`

Finds the number of unique city names where the population is between 5000 and 10,000 and the state is Ohio.

## Related resources

* [Count](/docs/count)
* [CountDistinct](/docs/countdistinct)
* [CountIf](/docs/countif)