Analytics Sources and Queries
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 returns the analytics of the workbook, and DashboardAnalytics.setSource registers a source. Creating Dashboards 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 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 chooses a source, dimensions, measures, filters, relationships, sorting, a result limit and supported aggregate calculations. DashboardAnalytics.query 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 saves such a relationship, and DashboardAnalytics.suggestRelationships 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. 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 | Analytics | The analytics of a workbook: sources, profiles, queries and relationships |
| AnalyticsSource | AnalyticsSource | A worksheet table, name or range binding, or a logical server connector query |
| AnalyticsQuery | AnalyticsQuery | 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.
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: register a source and build a dashboard on it.
- Filters and Drill-Down: narrow the queries of all widgets with shared filters.
- Generic and HTTP Data: bring data that is not in a worksheet into the workbook.