Window functions
A window function ranks data or fetches offset values within a specified partition. It assigns a calculated value to each row without changing the number of rows in the query's result set. Window functions are computed in the database and can be combined with the filter pane to perform analyses such as finding the top N items within a group. This topic describes the syntax, usage, and typical use cases of window functions.
Limitations
Window functions are supported on the following data sources. Ensure your data source meets the minimum version requirement.
Data source | Supported | Minimum version |
MySQL | Yes | 8.0 (not supported on versions 5.7 and earlier) |
AnalyticDB for MySQL | Yes | 3.0 (XIHE engine only) |
PostgreSQL | Yes | 9.4 |
ClickHouse | Yes | 21.1 |
MaxCompute | Yes | All versions |
Oracle | Yes | 11g |
Hive | Yes | 2.1.0 |
SQL Server | Yes | 2012 |
Apache Doris | Yes | All versions |
Additionally, window functions have the following limitations:
Data modeling: Window functions cannot be used with a relational model (a dataset with multiple logical tables). To use them, you must perform physical layer modeling.
Charts: Details tables, trend analysis tables, and multidimensional analysis tables do not support window functions.
Function nesting: Window functions cannot be nested with LOD functions or other window functions.
Partition fields: A partition field used in a window function must be added as a dimension to your chart. Otherwise, the database cannot perform the calculation.
Offset: For WINDOW_LAG and WINDOW_LEAD, the offset must be a positive integer and cannot exceed 999,999,999.
Totals and subtotals: Fields that contain window functions do not support totals and subtotals. The corresponding values are displayed as "-".
Background
Common analytical tasks include ranking within groups, making year-over-year and month-over-month comparisons, and analyzing first and last values. Examples include finding the top 5 cities by profit in each region, calculating month-over-month sales, or identifying a category's sales on its first day.
While table calculations can perform these tasks, they run in memory and are constrained by a row retrieval limit, which can lead to incomplete results with large datasets. Window functions, by contrast, perform calculations like ranking and offsets directly in the database. When used with the filter pane, you can filter for top-N results at the source, which reduces data transfer and improves performance for large-scale data analysis.
Use cases
Window functions are ideal for the following analytical scenarios:
Scenario | Example | Functions |
Ranking analysis | Ranking sales by category or performance by region. | WINDOW_ROW_NUMBER, WINDOW_RANK, WINDOW_DENSE_RANK |
Year-over-year and month-over-month | Comparing a month's sales figures with those of the previous or next month. | WINDOW_LAG, WINDOW_LEAD |
First and last value analysis | Finding the sales amount for the first or last day in each category. | WINDOW_FIRST_VALUE, WINDOW_LAST_VALUE |
Top N within a group | Returning only the top N rows from each partition. | Ranking functions + filter pane |
Syntax
Window functions use the same segmented syntax as LOD functions. Parameters are enclosed in curly braces ({}) and segments are separated by colons (:). A window function uses a partition field to divide data into partitions and then uses a sort expression to rank rows or retrieve offset values within each partition. The following table describes the parameters.
Parameter | Description |
Partition field | One or more dimensions that define the partition (group). Separate multiple fields with commas. This parameter can be empty to perform a global ranking. |
Sort expression | The field or aggregate expression used for sorting. You can specify ASC for ascending order or DESC for descending order. The default is ASC. Use commas to separate multiple expressions. |
Value expression | The expression from which to retrieve the value. It must be an aggregated field or an aggregation expression. This parameter is required only for offset functions and first/last value functions. |
Offset | The number of rows to move forward or backward. It must be a positive integer. This parameter is required only for offset functions. |
WINDOW_ROW_NUMBER
WINDOW_ROW_NUMBER assigns a unique, sequential rank to each row within a partition. Rows with identical values receive different ranks.
Syntax | WINDOW_ROW_NUMBER{<partition field> : <sort expression>} |
Definition | Assigns a unique, sequential rank to each row within a partition, with no gaps or ties. |
Returns | Integer |
Example | WINDOW_ROW_NUMBER{[Region]: SUM([Order Amount]) DESC} Ranks sales by region in descending order. For example, values of 6, 9, 9, and 14 would be ranked as 4, 2, 3, and 1, respectively. |
WINDOW_RANK
WINDOW_RANK assigns a rank to each row within a partition. Rows with identical values receive the same rank, and subsequent ranks are skipped.
Syntax | WINDOW_RANK{<partition field> : <sort expression>} |
Definition | Assigns a rank to each row within a partition. Rows with identical values receive the same rank, and subsequent ranks are skipped to reflect the number of tied rows. |
Returns | Integer |
Example | WINDOW_RANK{[Region]: SUM([Order Amount]) DESC} Ranks sales by region in descending order. For example, values of 6, 9, 9, and 14 would be ranked as 4, 2, 2, and 1, respectively. |
WINDOW_DENSE_RANK
WINDOW_DENSE_RANK assigns a rank to each row within a partition. Rows with identical values receive the same rank, and subsequent ranks are consecutive without gaps.
Syntax | WINDOW_DENSE_RANK{<partition field> : <sort expression>} |
Definition | Assigns a rank to each row within a partition. Rows with identical values receive the same rank, and ranks are not skipped. |
Returns | Integer |
Example | WINDOW_DENSE_RANK{[Region]: SUM([Order Amount]) DESC} Ranks sales by region in descending order. For example, values of 6, 9, 9, and 14 would be ranked as 3, 2, 2, and 1, respectively. |
WINDOW_LAG
WINDOW_LAG retrieves the value of an expression from a row that precedes the current row by a specified offset within a partition. It returns NULL if the offset is outside the partition's bounds.
Syntax | WINDOW_LAG{<partition field> : <sort expression> : <value expression> : <offset>} |
Definition | Retrieves a value from a preceding row by a specified offset within the partition. Returns NULL if the offset is outside the partition's bounds. |
Returns | Numeric |
Example | WINDOW_LAG{[Product]: [Month] ASC: SUM([Sales Amount]): 1} Partitions by product and sorts by month in ascending order to retrieve the sales amount from the previous row (the previous month's sales). |
WINDOW_LEAD
WINDOW_LEAD retrieves the value of an expression from a row that follows the current row by a specified offset within a partition. It returns NULL if the offset is outside the partition's bounds.
Syntax | WINDOW_LEAD{<partition field> : <sort expression> : <value expression> : <offset>} |
Definition | Retrieves a value from a following row by a specified offset within the partition. Returns NULL if the offset is outside the partition's bounds. |
Returns | Numeric |
Example | WINDOW_LEAD{[Product]: [Month] ASC: SUM([Sales Amount]): 1} Partitions by product and sorts by month in ascending order to retrieve the sales amount from the next row (the next month's sales). |
WINDOW_FIRST_VALUE
WINDOW_FIRST_VALUE retrieves the value of an expression from the first row of the sorted partition.
Syntax | WINDOW_FIRST_VALUE{<partition field> : <sort expression> : <value expression>} |
Definition | Retrieves a value from the first row of the sorted partition. |
Returns | Numeric |
Example | WINDOW_FIRST_VALUE{[Category]: [Date] ASC: SUM([Sales Amount])} Partitions by category and sorts by date in ascending order to retrieve the sales amount from the first row in each partition (the first day's sales for each category). |
WINDOW_LAST_VALUE
WINDOW_LAST_VALUE retrieves the value of an expression from the last row of the sorted partition.
Syntax | WINDOW_LAST_VALUE{<partition field> : <sort expression> : <value expression>} |
Definition | Retrieves a value from the last row of the sorted partition. |
Returns | Numeric |
Example | WINDOW_LAST_VALUE{[Category]: [Date] ASC: SUM([Sales Amount])} Partitions by category and sorts by date in ascending order to retrieve the sales amount from the last row in each partition (the last day's sales for each category). |
Procedure
On the dataset editing page, click Create Calculated Field.

Enter a Field Name (①), enter the window function expression in the Field Expression box (②), and configure other settings such as the field type (③).
For example, set the field name to "Regional Profit Rank" and enter the following expression: WINDOW_ROW_NUMBER{[Region]: SUM([Profit Amount]) DESC}.

Click OK to create the field.
(Optional) To filter for the top N results within each group, drag the calculated field to the filter pane and set the filter condition.
For example, set the condition to less than or equal to 5 to show only the top 5 rows from each partition.

Use the field to create a chart on your dashboard to view the results of the window function.
Examples
Top 5 cities by profit
To find the top 5 cities with the highest profit in each region, you can use a window function to rank all cities and then use a filter to display only the top results.
Create a calculated field named "Regional Profit Rank" with the following expression: WINDOW_ROW_NUMBER{[Region]: SUM([Profit Amount]) DESC}.

This expression partitions the data by region and assigns a unique rank to each row based on the total profit in descending order.
Create a crosstab. Drag Region and City to the Rows area, and drag Profit Amount and Regional Profit Rank to the Columns area.
Drag Regional Profit Rank to the filter pane and set the filter condition to less than or equal to 5.

Click Update. The crosstab shows only the top 5 cities by profit for each region.

Product type sales ranking (with ties)
To rank product sales within each category and allow for ties, use WINDOW_RANK or WINDOW_DENSE_RANK instead of WINDOW_ROW_NUMBER.
Create a calculated field named "Product Type Sales Rank" with the following expression: WINDOW_RANK{[Product Type]: SUM([Sales Amount]) DESC}.

This expression partitions the data by product type and ranks rows by total sales amount in descending order, skipping ranks after a tie.
Create a crosstab. Drag Product Type and Product Name to the Rows area, and drag Sales Amount and Product Type Sales Rank to the Columns area.
Click Update. Products with the same sales amount receive the same rank.

WINDOW_RANK: Skips ranks after a tie (for example, 1, 2, 2, 4).
WINDOW_DENSE_RANK: Does not skip ranks after a tie (for example, 1, 2, 2, 3).
Last month's sales by city
You can display a city's sales for the previous month alongside the current month's sales. This is useful for calculating the month-over-month growth rate.
Create a calculated field named "Last Month Sales by City" with the following expression: WINDOW_LAG{[City]: [Order Month] ASC: SUM([Order Amount]): 1}.

This expression partitions the data by city, sorts it by month in ascending order, and retrieves the sales amount from the preceding row.
Create a crosstab. Drag City and Order Month to the Rows area, and drag Order Amount and Last Month Sales by City to the Columns area.
Click Update. For each city, the function returns null for the first month because there is no preceding data. All other months display the corresponding sales from the previous month.

To calculate the month-over-month growth rate, create another calculated field with an expression such as: (SUM([Order Amount]) - [Last Month Sales by City]) / [Last Month Sales by City].
FAQ
What is the difference between a window function and a table calculation?
A table calculation is performed in memory after the database returns the full dataset. A window function is executed directly in the database. When used with the filter pane to filter for top N results, a window function returns only the rows that meet the criteria, which provides better performance on large datasets.
Can the partition field be empty?
Yes. An empty partition field indicates a global ranking, which means the function is applied to the entire result set. For example: WINDOW_ROW_NUMBER{: SUM([Sales Amount]) DESC}.
How do I find the top N results within a group?
This is a two-step process. First, create a calculated field for ranking, such as WINDOW_ROW_NUMBER{[Category]: SUM([Sales Amount]) DESC}. Second, drag this field to the filter pane and set a filter condition, such as less than or equal to 5.
Why does my MySQL data source report that it does not support window functions?
MySQL 8.0 and later versions support window functions. Check your data source version. If it is earlier than 8.0, you must upgrade it or use a different data source.
Can I use window functions and LOD functions together?
You can use fields containing LOD functions and fields containing window functions in the same query, where they are calculated independently. However, you cannot nest an LOD function inside a window function expression, or vice versa.




