SAMPLE
A number of a table's rows spread evenly over an order, the first and last included.
Group: Window and visual calculations. Compatible with DAX.
Syntax
SAMPLE ( <n_value>, <table>, <orderBy_expression> [, <order>] [, <orderBy_expression> [, <order>]] … )
| Parameter | Type | Description |
|---|---|---|
n_value | Whole number | How many rows to keep. |
table | Table | The rows to sample from. |
orderBy_expression | Expression repeatable, evaluated for each row | A value worked out for each row that sets the order the sample is spread over. |
order | Keyword optional, repeatable | ASC (or 1, TRUE) for smallest first, DESC (or 0, FALSE) for largest first; DESC when left out. |
Returns
Table. n rows of the table: the first and the last in the order, and others evenly spaced between.
Remarks
- The same table and arguments always give the same rows.
- With n at least the number of rows, the whole table comes back; with n of 0 or less, an empty one.
- The direction matters when the spacing is uneven: eight rows sampled to three keep the 1st, 5th and 8th in the order taken, so smallest first and largest first may keep different middle rows.
- The result has the table's columns; sort it with
ORDER BYto show it in order.
Examples
Three of eight days
Smallest day first: the first, the fifth and the last.
EVALUATE
VAR Days =
DATATABLE (
"Day", INTEGER, "Liters", INTEGER,
{ { 1, 40 }, { 2, 55 }, { 3, 38 }, { 4, 61 }, { 5, 70 }, { 6, 52 }, { 7, 45 }, { 8, 66 } }
)
RETURN
SAMPLE ( 3, Days, [Day], ASC )
ORDER BY [Day]
| Day | Liters |
|---|---|
| 1 | 40 |
| 5 | 70 |
| 8 | 66 |
The same, in the default order
Without a direction the days are taken largest first: 8, 4 and 1.
EVALUATE
VAR Days =
DATATABLE (
"Day", INTEGER, "Liters", INTEGER,
{ { 1, 40 }, { 2, 55 }, { 3, 38 }, { 4, 61 }, { 5, 70 }, { 6, 52 }, { 7, 45 }, { 8, 66 } }
)
RETURN
SAMPLE ( 3, Days, [Day] )
ORDER BY [Day]
| Day | Liters |
|---|---|
| 1 | 40 |
| 4 | 61 |
| 8 | 66 |
Spread over the values
Ordered by liters, the sample keeps the smallest, a middle one and the largest.
EVALUATE
VAR Days =
DATATABLE (
"Day", INTEGER, "Liters", INTEGER,
{ { 1, 40 }, { 2, 55 }, { 3, 38 }, { 4, 61 }, { 5, 70 }, { 6, 52 }, { 7, 45 }, { 8, 66 } }
)
RETURN
SAMPLE ( 3, Days, [Liters], ASC )
ORDER BY [Liters]
| Day | Liters |
|---|---|
| 3 | 38 |
| 2 | 55 |
| 5 | 70 |
Related functions
TOPN, SAMPLEAXISWITHLOCALMINMAX, SAMPLECARTESIANPOINTSBYCOVER, INDEX.