# Analytics Sources and Queries

> Register analytics sources, profile field roles, run AnalyticsQuery queries with measures and dimensions and join sources with many-to-one relationships.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Dashboards > Analytics Sources and Queries
- **2266 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/dashboards/analytics/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/dashboards/analytics/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/dashboards/analytics/llms-full.txt

---

Workbook analytics is the data layer of a workbook: it profiles the registered sources and returns grouped results, for dashboards or for your own application code. Read on for how sources and field roles work, what a query chooses, how relationships join two sources and which data access TX Sheet Control supports.

### Register Sources

Register worksheet tables, names or ranges as sources with stable IDs. In the browser editor, [Workbook.analytics](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/analytics-property/llms-full.txt) returns the analytics of the workbook, and [DashboardAnalytics.setSource](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/dashboardanalytics/setsource-method/llms-full.txt) registers a source. [Creating Dashboards](https://docs.textcontrol.com/tx-sheet-control/dashboards/creating/llms-full.txt) shows the code.

Clear headers and typed values produce better field roles.

### How Field Roles Work

Sources identify each field as a measure, a dimension, an identifier, a date, a boolean or mixed data. [DashboardAnalytics.profile](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/dashboardanalytics/profile-method/llms-full.txt) returns this metadata with diagnostics.

A field's role affects generated defaults. It is not permission to reinterpret an identifier as a monetary amount. Inspect the diagnostics, and override roles only when business semantics justify the choice.

### Query a Source

An [AnalyticsQuery](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/analyticsquery/llms-full.txt) chooses a source, dimensions, measures, filters, relationships, sorting, a result limit and supported aggregate calculations. [DashboardAnalytics.query](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/dashboardanalytics/query-method/llms-full.txt) runs a query. Queries are asynchronous and support cancellation where documented. Results are detached values: editing a result does not mutate worksheet cells.

Before you rely on the results:

- **Field IDs.** Use field IDs from the actual profiled source.
- **Blanks and duplicates.** COUNT, distinct counts and sums have different meanings for blanks and duplicates.
- **Calculated comparisons.** Test empty groups and missing operands before you use calculated comparisons as KPIs.

### Join Sources With Relationships

Explicit many-to-one left joins connect the fields of a source to the fields of a target. [DashboardAnalytics.setRelationship](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/dashboardanalytics/setrelationship-method/llms-full.txt) saves such a relationship, and [DashboardAnalytics.suggestRelationships](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/dashboardanalytics/suggestrelationships-method/llms-full.txt) suggests joins.

- **Duplicate target keys** reject, rather than silently multiplying totals.
- **Blank keys** do not match blank keys.
- **Suggestions** are never applied automatically. Review the compatibility of types and names, and the uniqueness of the keys.
- **Joined fields** use the documented qualified source and field IDs, `targetSourceId.fieldId`.
- **Chained or many-to-many relationships** are outside the supported relationship model.

Changing lookup data can invalidate uniqueness later, so validation at query time still matters.

### Worksheet and Application Data

Worksheet-based dashboards use registered tables, names and ranges. Generic and HTTP data functionality is exposed through the main packages where needed, as described in [Generic and HTTP Data](https://docs.textcontrol.com/tx-sheet-control/dashboards/data-connections/llms-full.txt). Application authorization, credentials, network access and refresh schedules remain responsibilities of the host.

### Key Members

The browser editor API names the analytics types `DashboardAnalytics`, `AnalyticsSource` and `AnalyticsQuery`. The .NET API has equivalents:

| Browser editor | .NET | Purpose |
|---|---|---|
| [DashboardAnalytics](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/dashboardanalytics/llms-full.txt) | [Analytics](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/analytics/llms-full.txt) | The analytics of a workbook: sources, profiles, queries and relationships |
| [AnalyticsSource](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/analyticssource/llms-full.txt) | [AnalyticsSource](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/analyticssource/llms-full.txt) | A worksheet table, name or range binding, or a logical server connector query |
| [AnalyticsQuery](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/analyticsquery/llms-full.txt) | [AnalyticsQuery](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/analyticsquery/llms-full.txt) | A bounded, deterministic query |

### Add Calculated and Ordered Metrics

Calculations operate on grouped results, using previously defined measure/calculation aliases. `add`, `subtract`, `multiply` and `divide` combine numeric values. `percent-of-total`, `running-total`, `moving-average`, `rank`, `difference` and `percent-change` calculate across result rows in the explicit sort order. A moving average uses a trailing window, defaulting to three rows. Division by zero and missing numeric operands produce `null`.

```javascript
const result = await book.analytics.query({
    sourceId: 'sales', dimensions: ['Date'],
    dateGrouping: [{ field: 'Date', unit: 'month' }],
    measures: [
        { field: 'Revenue', aggregation: 'SUM', alias: 'Revenue' },
        { field: 'Profit', aggregation: 'SUM', alias: 'Profit' }
    ],
    sort: { field: 'Date' },
    calculations: [
        { alias: 'Margin', operation: 'divide', left: 'Profit', right: 'Revenue' },
        { alias: 'Running Revenue', operation: 'running-total', left: 'Revenue' },
        { alias: 'Three-Month Average', operation: 'moving-average', left: 'Revenue', window: 3 },
        { alias: 'Monthly Change', operation: 'percent-change', left: 'Revenue' }
    ]
});
dashboard.addWidget({
    id: 'trend', type: 'line', title: 'Monthly Revenue and Trailing Average',
    placement: { x: 0, y: 0, width: 8, height: 6 },
    query: { sourceId: 'sales', dimensions: ['Date'],
        dateGrouping: [{ field: 'Date', unit: 'month' }],
        measures: [{ field: 'Revenue', aggregation: 'SUM', alias: 'Revenue' }],
        calculations: [{ alias: 'Average', operation: 'moving-average', left: 'Revenue', window: 3 }],
        sort: { field: 'Date' } },
    chart: { yField: 'Average' }
});
```

The standalone query returns all calculated columns. The chart selects the calculated `Average` alias explicitly through `yField`. Sorting windows by a field or aggregate gives a stable interpretation; a running total is chronological only when the underlying sort is chronological. Calendar grouping follows the workbook's 1900/1904 date system and does not rewrite source cells.

### Rank Top Groups and Retain Other

`topN: { count: 5, field: 'Revenue', includeOther: true, label: 'Other' }` retains the five highest groups and combines the remaining groups. `descending: false` selects the lowest groups instead. Top N ranks aggregate or arithmetic aliases, not window calculations. The Other row recomputes SUM, COUNT, AVG, MIN, MAX and COUNT DISTINCT from the underlying group accumulators: averages remain weighted and distinct values are unioned.

Other is a summary of several categories, so the browser does not offer it as one filter/drill mark. Bounds keep queries and rendering predictable: at most 16 calculations, 32 numeric parameters and 1,000 Top N groups. Heatmaps support up to 50 rows and 50 columns; expanded hierarchies stop at 1,000 displayed rows.

### Next Steps

- [Creating Dashboards](https://docs.textcontrol.com/tx-sheet-control/dashboards/creating/llms-full.txt): register a source and build a dashboard on it.
- [Filters and Drill-Down](https://docs.textcontrol.com/tx-sheet-control/dashboards/filters/llms-full.txt): narrow the queries of all widgets with shared filters.
- [Generic and HTTP Data](https://docs.textcontrol.com/tx-sheet-control/dashboards/data-connections/llms-full.txt): bring data that is not in a worksheet into the workbook.

---

## Nearby

- **Previous:** [Dashboard Viewer](https://docs.textcontrol.com/tx-sheet-control/dashboards/viewer/llms-full.txt)
- **Next:** [Generic and HTTP Data](https://docs.textcontrol.com/tx-sheet-control/dashboards/data-connections/llms-full.txt)
