The SAMPLE function returns a sample of N rows from the specified table.
SAMPLE(
n_value,
table,
orderBy_expression
[, order [, orderBy_expression [,order...]]]
)
- 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.
The SAMPLE function returns a table.
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.
We start from the following table ("Data"):
We extract 5 records:
Sample = SAMPLE(5, Data, Data [Id])
We see that, by default, the ordering is descending. If we specify that it is ascending:
Sample = SAMPLE(5, Data, Data [Id], ASC)
From the same table, we extract a single record:
Sample = SAMPLE(1, Data, Data [Id], ASC)
We now extract (or try to extract) 20 records:
We check that only the initial table is returned.