---
schema_version: "1"
id: 4f95ea826eb8a1dd3246065885c5fbe54dd8d90b5a44ff2722b67b0908f24a3e
title: Sorting, Aggregation, and Limiting
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/data-cloud/semantic-layer/guides/metric-query-api-in-depth-sorting.md
canonical_url: https://developer.salesforce.com/docs/data/semantic-layer/guide/metric-query-api-in-depth-sorting.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 50784b38f58e3be7981c4f845324452fd5656c8eb51133af4771786c902c2439
products:
  - Tableau Semantics Layer APIs
  - Data 360 Developer Documentation
---
# Sorting, Aggregation, and Limiting

This document describes how to sort, aggregate, and limit results in Semantic Metric Queries. These operations allow you to refine and customize your metric query results to meet specific business requirements.

## Sorting

You can sort the results using any of your selected dimensions. Sorting helps organize your metric query results in a meaningful order.

- The following example shows how to apply simple sorting in a metric query:
```json
{
  "structuredMetricQuery": {
    "sort_orders": [
      {
        "simple_sort_order": {
          "sort_by_field": {
            "expression": {
              "table_field": {
                "name": "Name",
                "table_name": "SemanticAccount_SDO"
              }
            }
          },
          "sorting_order": "ASC",
          "null_values_position": "NULLS_LAST"
        }
      }
    ],
    "submetric_definition": {
      "metric_api_name": "AnnualRevenueAmountPerName_md"
    },
    "model_api_name": "AccountSDM",
    "semantic_context": { // context
    }
  },
  "semanticModelId": "2SMxx0000004CAeGAM"
}
```

## Aggregation

You can aggregate the results of metric queries using time grains. Time grains allow you to group your metric data by specific time periods.

- Each metric query defines a list of supported time grains in the model, and the metric query selects one of them as shown in the following example:
```json
{
  "structuredMetricQuery": {
    "time_grain": "Week",
    "submetric_definition": {
      "metric_api_name": "AnnualRevenueAmountPerName_md"
    },
    "model_api_name": "AccountSDM",
    "semantic_context": { // context
    }
  },
  "semanticModelId": "2SMxx0000004CAeGAM"
}
```

## Limiting

You can use limiting to constrain the number of result rows returned in the metric query results. This is useful for performance optimization and pagination.

- The limit value is set as part of the SQL "LIMIT" clause. You can customize the default limit value. The Semantic Query supports limits in Run Mode - limits are applied after metric query result grouping. For example:

```json
{
  "structuredMetricQuery": {
    "limit_options": {
      "limit": 10
    },
    "submetric_definition": {
      "metric_api_name": "AnnualRevenueAmountPerName_md"
    },
    "model_api_name": "AccountSDM",
    "semantic_context": { // context
    }
  },
  "semanticModelId": "2SMxx0000004CAeGAM"
}
```

> **Performance:** A `limit` caps the number of result rows after grouping — it constrains the groups returned, not the rows scanned. Use filters to reduce the scan.

> **Performance:** Result size is capped at 5,000 rows for interactive queries and 50,000 rows for non-interactive queries; a query that would exceed the cap fails. Narrow the result with filters or a `limit`.
