Most reports don't need every row. They need counts, totals, latest dates and averages. OI OData can do that work on the server with $apply, so a dataset returns a handful of summary rows instead of thousands of raw ones.
Why aggregate on the server?
- A dataset reads at most 1,000 rows. "Entries per form per week" might cover 50,000 entries, but only a few hundred groups.
- It's faster. On database-backed objects, the grouping runs in SQL.
- Your page stays simple. The table or chart shows the summary directly.
The $apply pipeline
$apply is a series of steps separated by /. Each step works on the output of the one before:
$apply=filter(<condition>)/compute(<expression> as <alias>)/groupby((<keys>),aggregate(<aggregates>))
| Step | What it does |
|---|---|
| filter(...) | Keeps only the matching rows, like $filter |
| compute(... as Alias) | Adds a calculated column |
| groupby((keys),aggregate(...)) | Groups rows and calculates one summary row per group |
| aggregate(...) | Summarises the whole set into a single row |
You can still add $orderby and $top after $apply. They work on the grouped result.
groupby and aggregate
groupby((Key1,Key2),aggregate(<expression> with <method> as <Alias>, ...))
| Aggregate | Example | Result |
|---|---|---|
| Count rows | $count as Events | Number of rows in the group |
| Sum | EntryCount with sum as Entries | Total |
| Average | OI.tonumber(Value) with average as AvgValue | Mean |
| Minimum | StartTime with min as FirstEvent | Smallest or earliest |
| Maximum | Timestamp with max as LastSeen | Largest or latest |
| Count distinct | UserName with countdistinct as Users | Number of different values |
| List text values | Change with OI.stringagg as Changes | The group's values joined with ,. Database-backed objects only. See combining text. |
Events per monitor and severity in the last week, with the most recent:
SentinelEvents?$apply=filter(StartTime ge OI.daysago(7))/groupby((MonitorName,SeverityName),aggregate($count as Events,StartTime with max as Latest))&$orderby=Events desc
| MonitorName | SeverityName | Events | Latest |
|---|---|---|---|
| THP Low | High | 37 | 2026-10-06T22:15:00Z |
| Compressor Trip | Critical | 4 | 2026-10-05T03:40:00Z |
To summarise everything into one row, use aggregate without groupby:
EntryRecords?$apply=filter(CreatedDate ge OI.daysago(30))/aggregate($count as Entries,CreatedDate with max as Latest)
filter
filter(...) inside $apply takes the same conditions as $filter. Put it first, so the server groups as few rows as possible:
TagDefinitions?$apply=filter(Datasource ne null)/compute(Datasource/Name as DatasourceName)/groupby((DatasourceName),aggregate($count as Tags))&$orderby=Tags desc
compute
compute(expression as Alias) adds a column you can group or aggregate on. Use it to:
- Group by a part of a date: compute(year(CreatedDate) as Yr,month(CreatedDate) as Mo)
- Group by a related value, flat enough for a dataset: compute(Workspace/Name as WorkspaceName)
- Group by naming convention: compute(OI.nameprefix(Name) as Family)
- Count conditionally: compute(OI.iif(CreatedDate ge OI.daysago(7),1,0) as ThisWeek), then ThisWeek with sum as RecentEntries
- Convert text to a number: compute(OI.tonumber(Value) as Reading)
Entries created per day in September:
EntryRecords?$apply=filter(CreatedDate ge 2026-09-01T00:00:00Z and CreatedDate lt 2026-10-01T00:00:00Z)/compute(year(CreatedDate) as Year,month(CreatedDate) as Month,day(CreatedDate) as Day)/groupby((Year,Month,Day),aggregate($count as Entries))&$orderby=Year,Month,Day
Entries per form per week, for a logging-compliance chart:
EntryForms?$apply=filter(Entry/CreatedDate ge OI.monthsago(3))/compute(OI.startofweek(Entry/CreatedDate) as Week,FormSchema/Name as Form)/groupby((Week,Form),aggregate($count as Entries))&$orderby=Week,Form
Datasets need flat keys. Grouping directly on a related field, as in groupby((Workspace/Name)), returns a nested object that a dataset can't read. Compute an alias first and group on the alias, as in the examples above.
$compute outside $apply
$compute adds calculated columns to ordinary, ungrouped rows. Select them like any other column:
SentinelEvents?$filter=StartTime ge OI.daysago(7)&$compute=OI.minutesbetween(StartTime,OI.ifnull(EndTime,now())) as Minutes&$select=Entity,TestName,StartTime,Minutes&$orderby=Minutes desc&$top=100
The 100 longest Sentinel events this week. Events that are still open count up to now.
Calculated columns can also be used in $filter and $orderby, as Minutes is used here.
Sorting and limiting grouped results
- $orderby can sort on group keys and aggregate aliases: &$orderby=Events desc.
- $top limits the groups: &$orderby=Events desc&$top=10 gives the top 10.
- $orderby can't sort on a column that isn't in the grouped output.
Worked examples
Top 10 workbook publishers
WorkbookPublishHistory?$apply=groupby((UserDisplayName),aggregate($count as Publishes))&$orderby=Publishes desc&$top=10
Number of assets per template family
TemplateInstances?$apply=filter(TemplateDefinition/IsSystem eq false)/compute(OI.nameprefix(TemplateDefinition/Name) as Family)/groupby((Family),aggregate($count as Assets))&$orderby=Assets desc
Explorer users and when they were last seen
AnalyticsEventDetails?$apply=filter(UserName ne null and Timestamp ge OI.daysago(30))/groupby((UserName),aggregate($count as Events,Timestamp with max as LastSeen))&$orderby=Events desc
Open and recent entries in one pass
EntryRecords?$apply=compute(OI.iif(CreatedDate ge OI.daysago(7),1,0) as ThisWeek)/groupby((State),aggregate($count as Entries,ThisWeek with sum as RecentEntries))
Sentinel events by weekday, last quarter
SentinelEvents?$apply=filter(StartTime ge OI.monthsago(3))/compute(OI.weekday(StartTime) as Day)/groupby((Day),aggregate($count as Events))&$orderby=Day
Things to know
- Aggregate on database-backed objects. On live objects (SapiCases, Tags, Templates and others), $apply is evaluated over at most 5,000 source rows. On database-backed objects (EntryRecords, TagDefinitions, Entities, SentinelEvents and others), it runs in SQL over every row. See Limits and Performance.
- Dynamic entry fields keep their type, so sum and average work on number fields of SapiCases, SapiIssues, SapiSummaries and SapiTimeWindows, over at most 5,000 entries.
- $count is a keyword, written exactly as $count as Alias.
- Aliases are case-sensitive and must be unique within the query.