As well as the standard OData functions, OI OData adds functions for the calculations reports need most often: rolling date windows, durations, conditional counts, local time zones and naming conventions.
Every OI function name starts with OI., such as OI.daysago(30), which keeps it apart from OData's own functions. Names are case-sensitive: write OI.daysago, not oi.daysago or OI.DaysAgo.
Where you can use them
Use OI query functions in $filter, $compute, $apply and $orderby on database-backed objects, such as EntryRecords, EntryForms, FormFields, SentinelEvents, TemplateDefinitions, TagDefinitions, Datasets, AnalyticsEvents, AnalyticsEventDetails, WorkbookActivity, EntryChanges, WorkbookReferences, PageActivity and UserGroupActivity.
Every function except OI.normalizesql also works in Union branches and $crossjoin filters.
They don't work on live objects such as SapiCases, Tags or Templates.
Current time and time zones
Dates are stored in UTC. The functions that depend on the current time use the database server's UTC clock, the same clock as OData's own now(), so you can start a window with OI.daysago(30) and end it with now().
Calendar functions such as OI.startofmonth and OI.dbtoday work in UTC days and months. To group by a site's local day or hour, convert with OI.attimezone first.
Conditions, missing values and numbers stored as text
| Function | Returns | Example |
|---|---|---|
| OI.iif(condition, a, b) | a when the condition is true, otherwise b | OI.iif(CreatedDate ge OI.daysago(30),1,0). Summed, it counts the recent rows. |
| OI.ifnull(value, fallback) | value, or fallback when it's null | OI.ifnull(EndTime,now()) |
| OI.tonumber(text) | The text as a number, or null when it isn't one | OI.tonumber(Value) gt 40 |
- In OI.iif, a and b must be the same type, and neither can be a bare null. Use OI.ifnull for that.
- Use OI.tonumber for values stored as text, such as Sentinel Value, attribute values and audit NewValue.
Rolling windows
End a window with OData's own now().
| Function | Returns |
|---|---|
| OI.monthsago(n) | The date and time n months ago |
| OI.weeksago(n) | n weeks ago |
| OI.daysago(n) | n days ago |
| OI.hoursago(n) | n hours ago |
| OI.minutesago(n) | n minutes ago |
| OI.secondsago(n) | n seconds ago |
| OI.dbtoday() | Midnight today, UTC |
EntryRecords?$filter=CreatedDate ge OI.daysago(30) and CreatedDate le now()&$select=Id,CreatedDate,State
Rolling windows are a good alternative to date parameters when a page always shows "the last 30 days" or "since midnight". OData durations can't express months, so OI.monthsago is the easiest way to get "the last 6 months".
Durations and elapsed time
| Function | Returns |
|---|---|
| OI.dayssince(date), OI.hourssince(date), OI.minutessince(date) | Day, hour or minute boundaries crossed (UTC) from the date to now. 23:59 to 00:01 the next day is 1 day. |
| OI.daysbetween(a, b), OI.hoursbetween(a, b), OI.minutesbetween(a, b) | Day, hour or minute boundaries crossed (UTC) from a to b. Null if either is null. |
Unlike OData's date subtraction, which returns a duration, these return whole numbers you can sum and average.
SentinelEvents?$filter=StartTime ge OI.daysago(7)&$compute=OI.minutesbetween(StartTime,OI.ifnull(EndTime,now())) as Minutes&$select=Entity,TestName,StartTime,Minutes
Calendar periods
| Function | Returns |
|---|---|
| OI.startofweek(date) | Midnight on the Monday of the date's week |
| OI.startofmonth(date) | Midnight on the 1st of the date's month |
| OI.weekday(date) | 1 (Monday) to 7 (Sunday) |
| OI.isoweek(date) | ISO 8601 week number, 1 to 53 |
| OI.quarter(date) | 1 to 4 |
| OI.week(date) | SQL Server's week of the year. Matches older Explorer reports. Prefer OI.isoweek for new reports. |
OI.startofweek and OI.startofmonth give one key per period, which is better for charts than separate year and week columns. For one key per day on a database-backed object, group by year(), month() and day(). date() doesn't remove the time there, so every timestamp becomes its own group. Inside an Union branch, date() works.
OI.dbtoday(), OI.startofweek() and OI.startofmonth() return midnight with a +00:00 offset. The other date functions read a date the way it's stored, in its own offset.
Time zones
| Function | Returns |
|---|---|
| OI.attimezone(date, 'zone') | The same moment in another time zone |
Use it to group by a site's local hour or day, for example to line up with shift boundaries:
EntryRecords?$apply=filter(CreatedDate ge OI.daysago(30))/compute(hour(OI.attimezone(CreatedDate,'Australia/Perth')) as LocalHour)/groupby((LocalHour),aggregate($count as Entries))&$orderby=LocalHour
The zone is an IANA name, such as 'Australia/Perth' or 'America/Chicago', or a fixed offset such as 'UTC+08:00'. In a query, write the + as %2B, as in 'UTC%2B08:00', or it's read as a space. See Special characters. Windows IDs such as 'W. Australia Standard Time' also work. An unknown zone is rejected.
Names and SQL text
| Function | Returns | Example |
|---|---|---|
| OI.nameprefix(text) | The name up to and including its first -. If it has none, its first ., then _, then space. Empty if none of these appear. | WELL-A12 → WELL-, ESP_Template → ESP_ |
| OI.normalizesql(text) | The SQL text with formatting differences removed | Matches a query to an existing dataset even if whitespace or capitals differ |
Form fields by naming family:
FormFields?$apply=filter(IsSystem eq false and OI.nameprefix(Name) ne '')/compute(OI.nameprefix(Name) as Family)/groupby((Family),aggregate($count as Fields))&$orderby=Fields desc
Is there already a dataset for this SQL?
Datasets?$filter=contains(toupper(OI.normalizesql(NativeName)),toupper(OI.normalizesql('select well_id, oil_rate from daily_allocation')))&$select=Name,NativeName
Combining text
OI.stringagg is an aggregate, like sum or max. Use it in aggregate(...) to list a group's text values in one column, separated by commas:
| Aggregate | Returns |
|---|---|
| value with OI.stringagg as Alias | The group's values joined with ,, in no particular order. Missing (null) values are skipped, but empty text isn't. A group with no values gives null. |
Every field changed on each entry this week:
EntryAuditLogs?$apply=filter(Timestamp ge OI.daysago(7))/groupby((Entry/Id),aggregate(concat(concat(FieldName,': '),NewValue) with OI.stringagg as Changes))
- Aggregate a field of the object itself, or an expression of its fields as above. It doesn't work on a compute alias or a field of a related object, such as User/DisplayName.
- It also works in Union's apply.
Examples
Sentinel readings above 40, and how long each event lasted
SentinelEvents?$filter=OI.tonumber(Value) gt 40&$compute=OI.minutesbetween(StartTime,OI.ifnull(EndTime,now())) as Minutes&$select=Asset,TestName,Value,Minutes&$top=500
Events per week and weekday, last quarter
SentinelEvents?$apply=filter(StartTime ge OI.monthsago(3))/compute(OI.startofweek(StartTime) as Week,OI.weekday(StartTime) as Day)/groupby((Week,Day),aggregate($count as Events))&$orderby=Week,Day
Entries since midnight
EntryRecords?$apply=filter(CreatedDate ge OI.dbtoday())/aggregate($count as EntriesToday)
Monthly entry counts for the last year
EntryRecords?$apply=filter(CreatedDate ge OI.monthsago(12))/compute(OI.startofmonth(CreatedDate) as Month)/groupby((Month),aggregate($count as Entries))&$orderby=Month