{
  "slug": "csv-window-functions",
  "name": "gizza-ai/csv-window-functions",
  "version": "0.1.0",
  "title": "CSV Window Functions — Running Total, Moving Average, Rank & Lag/Lead — gizza.ai",
  "description": "Add SQL window-function columns to CSV rows in your browser: running total, moving average, lag, lead, rank, dense_rank, and row_number, with partition and order support.",
  "tags": [
    "csv window functions",
    "running total csv",
    "moving average csv",
    "csv rank",
    "csv lag lead",
    "cumulative sum csv",
    "sql window function",
    "row number csv",
    "csv analytics"
  ],
  "category": "data",
  "urls": {
    "page": "https://gizza.ai/tools/csv-window-functions/",
    "markdown": "https://gizza.ai/tools/csv-window-functions/index.md",
    "descriptor": "https://gizza.ai/tools/csv-window-functions/tool.json",
    "deep_link_example": "https://gizza.ai/tools/csv-window-functions/?data=region%2Cday%2Csales%0AW%2C1%2C3%0AW%2C2%2C5%0AE%2C1%2C10%0AE%2C2%2C20&function=running_total&column=sales&partition_by=region&order_by=day&window=3&offset=1&output_column=running_total_sales&descending=true&has_header=true&delimiter=%2C"
  },
  "cli": "gizza tool csv-window-functions \"region,day,sales\nW,1,3\nW,2,5\nE,1,10\nE,2,20\"",
  "tool": {
    "description": "Compute SQL-style window functions over CSV rows without collapsing them, appending one result column to every row. Supports running_total (cumulative sum), moving_average (trailing mean), lag and lead, rank, dense_rank, and row_number. Evaluate within partitions (partition_by) and an optional in-partition sort (order_by), choose the value column by header name or 1-based index, set the moving-average window or lag/lead offset, name the output column, rank ascending or descending, and pick comma/tab/semicolon/pipe delimiters. Returns CSV text grouped by partition. Requires a header row.",
    "parameters": {
      "additionalProperties": false,
      "properties": {
        "column": {
          "description": "Value column (header name or 1-based index) the function reads. Required for every function except row_number.",
          "type": "string"
        },
        "data": {
          "description": "Input CSV text. The first row must be a header row; every input row is preserved and one result column is appended.",
          "type": "string"
        },
        "delimiter": {
          "default": ",",
          "description": "CSV field delimiter for both input and output: comma (default), tab, semicolon, or pipe.",
          "enum": [
            ",",
            "tab",
            ";",
            "|"
          ],
          "type": "string"
        },
        "descending": {
          "default": false,
          "description": "Reverse the order_by sort, and rank largest value first. Default false.",
          "type": "boolean"
        },
        "function": {
          "default": "running_total",
          "description": "Window function to apply: running_total (cumulative sum), moving_average (trailing mean over `window` rows), lag/lead (value `offset` rows before/after), rank/dense_rank (position by value, ties share a rank), or row_number (1-based position). Default running_total.",
          "enum": [
            "running_total",
            "moving_average",
            "lag",
            "lead",
            "rank",
            "dense_rank",
            "row_number"
          ],
          "type": "string"
        },
        "has_header": {
          "default": true,
          "description": "Treat the first CSV row as a header row. This tool requires a header row, so leave this on. Default true.",
          "type": "boolean"
        },
        "offset": {
          "default": 1,
          "description": "lag/lead only: how many rows before (lag) or after (lead) the current row to read. Default 1.",
          "maximum": 1000000,
          "minimum": 0,
          "type": "integer"
        },
        "order_by": {
          "description": "Comma-separated columns to sort rows by within each partition before computing. Empty means input order.",
          "type": "string"
        },
        "output_column": {
          "description": "Name of the appended result column. Empty picks a sensible default such as running_total_<column> or rank.",
          "type": "string"
        },
        "partition_by": {
          "description": "Comma-separated columns to partition by. Each distinct combination is an independent window. Empty means one window over all rows.",
          "type": "string"
        },
        "window": {
          "default": 3,
          "description": "moving_average only: number of trailing rows in the frame, including the current row. Default 3.",
          "maximum": 1000000,
          "minimum": 1,
          "type": "integer"
        }
      },
      "required": [
        "data"
      ],
      "type": "object"
    }
  }
}