<!-- https://learnsql.cpetoday.com/functions/dax/rownumber -->
# ROWNUMBER

Language: DAX
Category: Ranking and windows
Syntax: ROWNUMBER([<relation>], [<orderBy>], [<blanks>])
Summary: Gives each row of an ordered list a sequence number, starting from 1.
Returns: A whole number for the current row.
Level: Advanced
Related: WINDOW, OFFSET, INDEX, RANK, ALLSELECTED, TOPN

## What it does
ROWNUMBER numbers the rows of an ordered list, so the first row is 1, the second is 2, and so on. The numbers come from the sort order you choose, not from the order rows sit in the data. Use it to number items in a visual, or as a step before a filter that keeps the first few rows. Check that your version of Power BI supports it, and confirm the arguments in the Microsoft reference, because this is one of the newer functions.

## Syntax and arguments
- **relation** (optional): the rows to number, 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
A sequence number for each date in the current selection, shown in a table next to the dates.

```dax
Day Number =
ROWNUMBER (
    ALLSELECTED ( 'Date'[Date] ),
    ORDERBY ( 'Date'[Date] )
)
```

The earliest date in the selection gets 1, the next gets 2, and so on. The number is based on the date order, so it stays the same when the report is sorted another way.

## Things to watch
- Confirm the argument list and that your version supports the function. Numbering by the wrong column gives numbers that look right but mean something else.
- Ties in the sort column can give rows different numbers in an order that is not stable. Sort by a unique column when the numbers must be repeatable.
- Numbering very large tables is slow. Use a dimension list where you can.
- The number belongs to the row in the list. It does not mean the row number in the source file.

## In business terms
Numbering invoices or journal lines in a review sample, giving each period in a selection a sequence for a chart, and marking the first few rows of a sorted list for a top-N style review.
