
Chapter 6: Fundamental Functions 211
Analysis
The two examples are subtly different. The first one has a filter, with an equality test on
the ProductID column. It will iterate through all the entries for a particular product,
and return the maximum quantity ordered for a particular product. It will start on the
first row and find all other rows with the same product. It will then move on to the
second row and repeat the process. In this manner, it will traverse every single row
in the table. The second example does not distinguish between different products.
If it finds a row that has a larger quantity ordered than any other row, then the
COUNTROWS() function will return zero. If we add 1 to this, that row will be ranked in
first place. FILTER() is covered later in this chapter. MAXX() and COUNTROWS() are
discussed in the next chapter. There is an optional second parameter to EARLIER(). If
omitted, it defaults to 1, which means “compare to the immediate previous pass only.”
Theoretically, the maximum number of scans of all the table rows is n
2
where n is the
number of rows. You may need to be aware of possible performance implications, when
using EARLIER() and EARLIEST(). Here is another example; it shows how many
employees report to a particular employee:
=COUNTROWS(FILTER(Employees,[ReportsTo]=EARLIER([EmployeeID],1)))
If you try this on the Employees table, you should find that only Fuller and
Buchanan are managers.
EARLIEST()
EARLIEST() is very similar to EARLIER(). The same two examples are shown here,
and the result is the same. EARLIEST() allows for more recursion than EARLIER().
Recursion levels are beyond the scope of this introductory book. However, you might
like to know that EARLIEST() fetches the outermost row context where there are
nested contexts. This can occur where you have multiple, nested aggregation functions.
EARLIEST() is a Filter function. The examples are calculated columns on the Order
Details table.
Syntax
=MAXX(FILTER('Order Details','Order Details'[ProductID] =
EARLIEST('Order Details'[ProductID])),'Order Details'[Quantity])
=COUNTROWS(FILTER('Order Details','Order Details'[Quantity] >
EARLIEST('Order Details'[Quantity]))) + 1