SAMPLE

The SAMPLE function returns a sample of N rows from the specified table.

Syntax

SAMPLE(
    n_value,
    table,
    orderBy_expression
    [, order [, orderBy_expression [,order...]]]
)

Parameters
  • n_value: Number of rows to extract. It can be any DAX expression that returns a scalar.
  • table: Table or DAX expression that returns a table from which to extract the sample.
  • orderBy_expression: (Mandatory argument in Power BI despite Microsoft documentation) DAX expression (it can be a simple field) that will be evaluated for each row of the extracted sample and that will determine the order of the rows.
  • order: (Optional argument) Value that specifies whether the order will be ascending or descending. It can take the values ASC/DESC, 1/0 or TRUE/FALSE.
Returned value

The SAMPLE function returns a table.

Additional Information

The sample drawn always contains the first record, the last record, and N-2 equally spaced records (that is, it is not a random sample).

If n_value is zero or a negative number, the resulting table will be empty. If it is 1, the first record is returned. If it is 2, the first and last records are returned. If n_value is greater than the number of available records, the entire original table is returned.

The result is returned, by default, in descending order.

Examples

We start from the following table ("Data"):

Data table to use

We extract 5 records:

Sample = SAMPLE(5, Data, Data [Id])

Sample of 5 records

We see that, by default, the ordering is descending. If we specify that it is ascending:

Sample = SAMPLE(5, Data, Data [Id], ASC)

Displays sorted in ascending order

From the same table, we extract a single record:

Sample = SAMPLE(1, Data, Data [Id], ASC)

Sample with a single record

We now extract (or try to extract) 20 records:

Sample = SAMPLE(20, Data, Data [Id], ASC)
Sample configured to extract more records than existing ones

We check that only the initial table is returned.

Category
Statistical
Submitted by admin on Sun, 07/14/2019 - 17:35