OpenBI Docs Buy

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>]] … )
ParameterTypeDescription
n_valueWhole numberHow many rows to keep.
tableTableThe rows to sample from.
orderBy_expressionExpression
repeatable, evaluated for each row
A value worked out for each row that sets the order the sample is spread over.
orderKeyword
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 BY to 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]
DayLiters
140
570
866

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]
DayLiters
140
461
866

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]
DayLiters
338
255
570

TOPN, SAMPLEAXISWITHLOCALMINMAX, SAMPLECARTESIANPOINTSBYCOVER, INDEX.

Last updated · OpenBI Studio 0.3.74