Skip to main content

LAG

Given a level member and a nonnegative integer, this function counts backward in the level and returns a previous member. The details are different for time dimensions and data dimensions.

Returned Type

This function returns a member.

Syntax and Details

member_expression.LAG(optional_integer_expression) 

Where:

  • member_expression is an expression that returns a member.

    This expression cannot refer to a measure.

  • optional_integer_expression is a nonnegative integer literal.

    The default value for this argument is 0; in this case, the function returns the member given by member_expression.

    If integer_expression is 1, this function is equivalent to the PREVMEMBER function.

This function examines the members of the level to which the given member belongs, counts backward from the current member (using integer_expression), and returns the member at that position. For time dimensions, this function ignores any parent level. For data dimensions, this function considers the parent level; it counts backward from the current member within the given parent member. (Note that the terms time dimension and data dimension refer specifically to the dimension type as defined in the cube. See Defining DeepSee Models.)

Within any time dimension, this function is more useful for a timeline-based time level (such as Period, which groups records by year and month) than for a date-part-based time level (such as Month, which groups records only by month). If the level is based on a date part, this function returns null when it refers to a level beyond the end of the set; see an example of a similar scenario in PREVMEMBER. For a fuller discussion, see “Introduction to Time Levels,” in Using MDX with DeepSee.

Example

The first examples use a time dimension. Consider the following query, shown for reference:

SELECT MEASURES.[%COUNT] ON 0, 
{birthd.1948,birthd.1949,birthd.1950,birthd.1951,birthd.1952} ON 1 
FROM patients
 
                             Patient Count
1 1948                                   10
2 1949                                    4
3 1950                                   12
4 1951                                    8
5 1952                                    6

The following query uses LAG:

SELECT MEASURES.[%COUNT] ON 0, birthd.1951.LAG(1) ON 1 FROM patients           
                             Patient Count
1950                                     12

For another example:

SELECT MEASURES.[%COUNT] ON 0, birthd.1951.LAG(2) ON 1 FROM patients           
                             Patient Count
1949                                      4

In this sample, the year level is the child of the decade level, which means that the members 1949 and 1950 belong to different parents. As you can see, the LAG function ignores the parent level when you use the function with a time dimension.

The second examples use a data dimension (the HomeD dimension). To see the hierarchy in this dimension, see the examples in the FIRSTCHILD function. The following query uses LAG with this dimension:

SELECT MEASURES.[%COUNT] ON 0, homed.city.Magnolia.LAG(1) ON 1 FROM patients
 
                             Patient Count
Cypress                                 104

Because this is a data dimension, this query retrieves the previous member of the city level within the parent ZIP code. Within this ZIP code, Cypress is the first city, so the following query returns no results:

SELECT MEASURES.[%COUNT] ON 0, homed.city.Cypress.LAG(1) ON 1 FROM patients
 
                             Patient Count
                                          *

See Also

FeedbackOpens in a new tab