<!-- https://learnsql.cpetoday.com/functions/dax/index -->
# INDEX

Language: DAX
Category: Ranking and windows
Syntax: INDEX(<position>, [<relation>], [<orderBy>], [<blanks>])
Summary: Returns the row at a given position in an ordered list. Position 1 is the first row, and a negative number counts from the end.
Returns: A table holding that row. Used inside CALCULATE or another function that takes a table.
Level: Advanced
Related: OFFSET, WINDOW, ROWNUMBER, RANK, CALCULATE, TOPN

## What it does
INDEX picks one row from an ordered list by its position. Position 1 is the first row, which is useful for the first period in a selection. A negative position counts from the end, so -1 is the last row, which is useful for the latest date with data. Use it when a figure must come from a fixed place in the sorted list rather than from the current row. Check that your version supports it.

## Syntax and arguments
- **position**: the row to pick. 1 is the first, and -1 is the last, as counted in the ordered list.
- **relation** (optional): the rows to consider, usually `ALLSELECTED` of a column the visual uses.
- **orderBy** (optional): `ORDERBY` of the column that sets the sequence.
- **blanks** (optional): how blank rows are treated. See the Microsoft reference.

## Example
Sales on the latest day in the current selection, shown as a card.

```dax
Latest Day Sales =
CALCULATE (
    [Total Sales],
    INDEX (
        -1,
        ALLSELECTED ( 'Date'[Date] ),
        ORDERBY ( 'Date'[Date] )
    )
)
```

The list of days in the selection is sorted by date, the last day is picked, and its sales are returned. The card then shows the most recent day with the selected filters.

## Example: a second use
Sales on the first day in the selection, to compare the opening day with the close.

```dax
First Day Sales =
CALCULATE (
    [Total Sales],
    INDEX (
        1,
        ALLSELECTED ( 'Date'[Date] ),
        ORDERBY ( 'Date'[Date] )
    )
)
```

The same pattern with 1 in place of -1 picks the earliest day. Put both figures on one page to show the change across the selection.

## Things to watch
- The result depends on the sort order. Check the `ORDERBY` column before you trust the position.
- A position outside the list returns no row. Test what the report shows in that case.
- Confirm the negative position works as described in your version, with a small test table.
- Compare with `LASTDATE` or `FIRSTDATE` style functions for dates, which are often clearer in time intelligence work.

## In business terms
Opening and closing balances for a selected period, the latest posting date in a ledger extract, and the first and last values of a series in a close review.
