Knowledge Base
Open App
KB 102800

Function replace FIRSTDATE/LASTDATE with MIN/MAX


When FIRSTDATE or LASTDATE functions are used to return a scalar value instead of a table, the same result can be obtained in a more efficient way by using MIN or MAX on the column.

Remarks

FIRSTDATE and LASTDATE are table functions that internally use MIN and MAX, respectively.

Whenever the expression requires a scalar value, using the table function is unnecessarily expensive, also because it could involve a context transition in case one or more row contexts are active.

Recommended articles:

Example 1

Replace FIRSTDATE with MIN.

Original code

VAR firstDate = FIRSTDATE( Sales[Order Date] )

Possible optimization

VAR firstDate = MIN( Sales[Order Date] )

Example 2

Replace LASTDATE with MAX.

Original code

CALCULATE (
    LASTDATE( Sales[Order Date] ),
    Customer[Country] = "Canada"
)

Possible optimization

CALCULATE (
    MAX( Sales[Order Date] ),
    Customer[Country] = "Canada"
)