Share via


CountDistinct Function (Report Builder and SSRS)

Returns a count of all distinct non-null values specified by the expression, evaluated in the context of the given scope.

Note

You can create and modify report definitions (.rdl) in Report Builder and in Report Designer in SQL Server Data Tools. Each authoring environment provides different ways to create, open, and save reports and related items. For more information, see Designing Reports in Report Designer and Report Builder (SSRS) on the Web at microsoft.com.

Syntax

  
CountDistinct(expression, scope, recursive)  

Parameters

expression
(Variant) The expression on which to perform the aggregation.

scope
(String) Optional. The name of a dataset, group, or data region that contains the report items to which to apply the aggregate function. If scope is not specified, the current scope is used.

recursive
(Enumerated Type) Optional. Simple (default) or RdlRecursive. Specifies whether to perform the aggregation recursively.

Return Type

Returns an Integer.

Remarks

The value of scope must be a string constant and cannot be an expression. For outer aggregates or aggregates that do not specify other aggregates, scope must refer to the current scope or a containing scope. For aggregates of aggregates, nested aggregates can specify a child scope.

Expression can contain calls to nested aggregate functions with the following exceptions and conditions:

  • Scope for nested aggregates must be the same as, or contained by, the scope of the outer aggregate. For all distinct scopes in the expression, one scope must be in a child relationship to all other scopes.

  • Scope for nested aggregates cannot be the name of a dataset.

  • Expression must not contain First, Last, Previous, or RunningValue functions.

  • Expression must not contain nested aggregates that specify recursive.

For more information, see Aggregate Functions Reference (Report Builder and SSRS) and Expression Scope for Totals, Aggregates, and Built-in Collections (Report Builder and SSRS).

For more information about recursive aggregates, see Creating Recursive Hierarchy Groups (Report Builder and SSRS).

Example

The following code example shows an expression that calculates the number of unique non-null values of Size for the default scope and for a parent group scope. The expression is added to a cell in a row that belongs to the child group GroupbySubcategory. The parent group is GroupbyCategory. The expression displays the results for GroupbySubcategory (the default scope) and then for GroupbyCategory (the parent group scope).

Note

Expressions should not contain actual carriage returns and line breaks; these are included in the example code to support documentation renderers. If you copy the following example, remove carriage returns from each line.

="Distinct count (Subcategory): " & CountDistinct(Fields!Size.Value) &   
"Distinct count (Category): " & CountDistinct(Fields!Size.Value,"GroupbyCategory")  

See Also

Expression Uses in Reports (Report Builder and SSRS)
Expression Examples (Report Builder and SSRS)
Data Types in Expressions (Report Builder and SSRS)
Expression Scope for Totals, Aggregates, and Built-in Collections (Report Builder and SSRS)