FILTER

The FILTER function returns a table that represents a subset of another table.

Syntax

FILTER(
    table,
    filter
)

Parameters
  • table: Table to be filtered or expression that returns a table.
  • filter: Boolean expression that will be evaluated for each row of the table.
Returned value

The FILTER function returns a table made up of those rows from the original table that have passed the filter.

Additional Information

This function is often used as an argument in other functions (mainly in the CALCULATE function) when a table needs to be filtered.

Examples

Suppose we have defined the measure

Ventas = SUM(FactSales[SalesAmount])

...which adds up the sales amounts. Now imagine that we want to add the sales of those products whose price is higher than the average value of the sales prices. To do this, our first attempt would be to define the following measure:

Ventas por encima del promedio = CALCULATE(
    [Ventas],
    DimProduct[UnitPrice]>AVERAGE(DimProduct[UnitPrice])
)

...measure that, in any case, causes an error because CALCULATE does not admit complex expressions like this as an argument:

FILTER function. Example of use

The solution is to add as an argument not the complex expression in question, but the sales table once we have filtered it according to the same criteria:

Ventas por encima del promedio = CALCULATE(
    [Ventas],
    FILTER(
        DimProduct,
        DimProduct[UnitPrice]>AVERAGE(DimProduct[UnitPrice])
    )
)

...which returns the result we were looking for.

Category
Filter
Submitted by admin on Tue, 12/04/2018 - 12:07