Daily DAX : Day 350 SAMPLE
Power BI DAX SAMPLE Function
Description
The SAMPLE
function in Power BI DAX (Data Analysis Expressions) is used to retrieve a random sample of rows from a table. It is useful for statistical analysis, testing, or scenarios where you need a subset of data without bias.
Syntax
SAMPLE(<NumberOfRows>, <Table>, <OrderByExpression> [, <Order>] [, <OrderByExpression> [, <Order>]] ...)
- NumberOfRows: The number of rows to return (integer).
- Table: The table to sample from.
- OrderByExpression: (Optional) A column or expression to sort the table before sampling.
- Order: (Optional) ASC (ascending) or DESC (descending) for sorting. Default is ASC.
Return Value
A table containing a random subset of rows from the input table, with the specified number of rows.
Use Case
The SAMPLE
function is commonly used in:
- Statistical Analysis: To analyze a representative subset of a large dataset.
- Testing: To work with a smaller, random sample of data during development or testing.
- Data Exploration: To quickly inspect a random subset of data without processing the entire table.
Example
Suppose you have a table Sales
with columns Product
, Region
, and Revenue
. You want to select a random sample of 5 rows, ordered by Revenue
in descending order.
SampleSales = SAMPLE(5, Sales, Sales[Revenue], DESC)
This returns a table with 5 random rows from the Sales
table, sorted by Revenue
in descending order before sampling.
Notes
- The randomness is not guaranteed to be perfectly uniform, as it depends on the DAX engine's implementation.
- If
NumberOfRows
exceeds the table's row count, the entire table is returned. - Use with caution in reports requiring consistent results, as the sample is random and may change on refresh.
Comments
Post a Comment