Last Year, Share of Total, Stock at a Date: One Line Each in an XLTable Cube
Three calculations nearly every cube needs, written as a tag on the measure line. With notes for BigQuery and StarRocks.

Almost every cube has the same three calculations: the measure a year ago, the share of a total, and the stock at the end of the period. They take MDX in Analysis Services, DAX in Power BI, and a second measure group or a Jinja script in a hand-written XLTable cube.
Since version 2.1.4 each of them is a tag on the measure line. The server expands the tag into the cube you would otherwise write by hand, so filters, roles, drillthrough and the cache keep working. The few exceptions are listed under Limits at the end.
Three calculations every cube needs
If you build cubes for Excel, these requests come back on every project.
The same measure a period ago. Sales next to sales last year, month by month, with a growth column.
A share of a total. A model's share of its region's sales. The numerator respects every filter in the pivot. The denominator has to ignore one dimension and respect all the others.
A value at a date. Stock, prices, headcount, account balances. Adding up thirty daily stock levels gives a number that means nothing. A month needs the stock on its last day, and a year needs the stock on December 31.
All three are easy to explain to a business user and awkward to model, because each one breaks the default rule of a pivot table: filter the rows, then aggregate them. Last year needs different rows from the ones in the cell. A share needs fewer filters in the denominator. Stock needs a different aggregation along one dimension only.
Before and after: "last year"
An XLTable cube is SQL with comment tags. Written by hand, "last year" is a second measure group that repeats every join, plus a Jinja script that shifts the date:
--olap_jinja
{{ sql_text | replace("salesly.date_sale",
"(salesly.date_sale::date + INTERVAL `1 year`)::date::text") }}
--olap_source Sales last year
SELECT
--olap_measures
sum(salesly.qty) as salesly_sum_qty --translation=`Sales last year Quantity`
,sum(salesly.sum) as salesly_sum_sum --translation=`Sales last year Amount`
FROM demo.Sales salesly
LEFT JOIN demo.Stores stores ON salesly.store = stores.id
LEFT JOIN demo.Models models ON salesly.model = models.id
LEFT JOIN calendar times ON salesly.date_sale = times.day_str
This form still works, and it is what the tag expands to. With the tag, the same measure is one more line in the existing group:
--olap_source Sales
SELECT
--olap_measures
sum(sales.qty) as sales_qty --translation=`Sales`
,sum(sales.qty) as sales_py_qty --translation=`Sales LY` --offset=`-1 year`
FROM db.sales sales
LEFT JOIN calendar times ON sales.date_sale = times.day_d
Each derived measure is still declared explicitly, on its own line, with its own name and format, the way Databricks metric views and Cube do it. Nothing appears in the field list that you did not write.
The calendar: --date
The server has to know which dimension is the calendar and what grain each level has. Mark the levels with --date (year, quarter, month, week or day):
--olap_source Dates
SELECT
--olap_dimensions
times.year_str as year_str --hierarchy=`Dates` --date=`year`
,times.month_str as month_str --hierarchy=`Dates` --date=`month`
,times.day_str as day_str --hierarchy=`Dates` --date=`day`
FROM calendar times
A cube has one calendar. Mark every level of its hierarchy: the syntax checker warns about the ones you missed.
Previous period: --offset
The interval is <±N> year|quarter|month|week|day. With --offset=`-1 year`:
- with Year = 2025 in the report filter, the measure shows 2024;
- with months on rows, March 2025 shows March 2024;
- a slicer by store applies to both measures.
Growth is an ordinary calculated field:
(sales_qty - sales_py_qty) / nullif(sales_py_qty, 0) as yoy --translation=`YoY %`

An Excel PivotTable on the demo cube: each month of 2025 next to the same month of 2024.
For string dates, 52/53-week retail calendars and fiscal calendars, give the tag a key column of the calendar instead of an interval: --offset=`times.py_day`. It holds "the matching day a year ago", and what counts as matching is your decision as the cube author. The join becomes ON sales.date_sale = times.py_day.
One thing to check when you define that column: no two calendar days should point at the same day. A plain day - 1 year breaks this around leap years. February 28 and 29, 2024 both map to February 28, 2023, so that day's sales count twice in February 2024, and February 29, 2024 never shows up as last year's value for 2025. If you want calendar dates, use the interval form: it shifts the fact date, so every fact row lands on exactly one calendar day. If you want a retail comparison, 364 days back is one-to-one and keeps Monday aligned with Monday.
The same tag on BigQuery and StarRocks
The measure line does not depend on the warehouse. With the interval form, the server shifts the date with the warehouse's own function and your cube contains nothing dialect-specific. With a key column, the only dialect-specific part is one column in the calendar. Here is the 364-day retail comparison on both:
-- BigQuery
SELECT *, DATE_SUB(day_d, INTERVAL 364 DAY) AS py_day FROM db.times
-- StarRocks
SELECT *, CAST(days_sub(day_d, 364) AS DATE) AS py_day FROM db.times
-- the measure line, identical on both
,sum(sales.qty) as sales_py_qty --translation=`Sales LY` --offset=`times.py_day`
BigQuery. For the interval form the fact column must be of type DATE; for TIMESTAMP or DATETIME, use a key column. The choice also affects cost. The interval form joins on an expression, so the fact table gets no partition pruning, while the key-column form keeps a plain equality join. BigQuery's on-demand model bills for the data a query scans, and pruned partitions are not counted, so on a large partitioned fact prefer the key column.
StarRocks. days_sub returns a DATETIME, hence the cast. --semi, described below, runs as a window function inside StarRocks, so no data is copied out of the warehouse.
The tags work the same way on all eight supported databases: ClickHouse, Snowflake, BigQuery, Databricks, StarRocks, Trino, Greenplum and DuckDB. Ready-made sample cubes are in the docs, including BigQuery and StarRocks.
Share without a dimension: --exclude
--exclude=`Models` gives the measure without the dimensions of the named source. That is the denominator of a share:
--olap_source Sales
SELECT
--olap_measures
sum(sales.qty) as sales_qty --translation=`Sales`
,sum(sales.qty) as sales_all --translation=`All models` --exclude=`Models` --hide
FROM db.sales sales
LEFT JOIN db.models models ON sales.model = models.id
LEFT JOIN calendar times ON sales.date_sale = times.day_str
--olap_cube
--olap_calculated_fields Calculated fields
sales_qty / nullif(sales_all, 0) as share --translation=`Share` --format=`0.0%`
With models on rows, every row of the denominator holds the total over all models, under the other filters. With region on rows above a level of the same source (category, brand or model), the denominator is the region subtotal, so the shares inside each region add up to 100%. The denominator is hidden with --hide, and the user sees only the share.

Region and category on rows: the shares inside each region add up to 100%.
Value at a date: --semi
--semi=`last` (or first) makes the measure show its aggregate over the last (first) day with data inside the period of the cell:
--olap_source Stock
SELECT
--olap_measures
sum(stock.qty) as stock_qty --translation=`Stock Quantity` --semi=`last`
FROM db.stock_daily stock
LEFT JOIN db.stores stores ON stock.store = stores.id
LEFT JOIN calendar times ON stock.day_d = times.day_d
| Pivot table | What the measure shows |
|---|---|
| Months on rows | Stock on the last day of each month. The year total is the stock on December 31, not the sum of twelve months |
| Year in the filter, stores on rows | Each store on December 31. The total is the sum of the stores on that date |
| Days on rows | The ordinary sum for the day |
The aggregate can be anything (sum, count, avg, max), and the tag combines with --offset: --semi=`last` --offset=`-1 year` is the stock at the end of the same month a year ago.

Demo data. The same column twice: without the tag the total adds up 365 daily levels; with --semi it is the stock on December 31.
If you are moving from Analysis Services, you know this as LastChild and LastNonEmpty. In multidimensional models, Standard edition supports only LastChild; LastNonEmpty, FirstChild, AverageOfChildren and ByAccount require Enterprise. In Power BI the closest functions are LASTDATE and LASTNONBLANK. Note one difference from LastNonEmpty: --semi finds the last day once for the whole measure group, not separately for each store or product (more under Limits).
What the tag does inside
--offset and --exclude are shorthand for the cube you could write by hand. Before parsing, the server makes a copy of the measure group and rewrites its joins: the calendar join gets the date shift, in each warehouse's own SQL (INTERVAL in DuckDB, addYears in ClickHouse, DATEADD in Snowflake), and joins to excluded sources are dropped. The copy is an ordinary measure group, so the engine treats it like any other. In the field list a tagged measure gets a folder named after the copy (Sales__offset_m1y); add --folder=`Sales` to the line to keep it next to the original. You can read the resulting SQL on the Preview tab of the Cubes page.
--semi makes no copy. The value at a date is computed by a window function inside the same SQL, over the rows left after WHERE, so date filters and slicers are taken into account.

The Preview tab on DuckDB: the second measure group is the copy, joined to the calendar through date_sale_d + INTERVAL '1 year'.
The built-in syntax checker validates the tags, so a typo in a value or a missing calendar can be caught before a user opens the report.
How other tools write the same thing
The idea is common, and the syntax differs:
| Tool | How it is written |
|---|---|
| Analysis Services, AtScale, Mondrian, icCube | MDX calculated members: ParallelPeriod, PrevMember |
| Power BI | DAX: SAMEPERIODLASTYEAR, CALCULATE + ALL, LASTDATE |
| Cube | Multi-stage measures: time_shift, grain: exclude |
| dbt MetricFlow | Derived metric with offset_window |
| Looker | Measure of type: period_over_period |
| Holistics | AQL: relative_period, of_all |
| Databricks metric views | window with offset and semiadditive |
XLTable's tags follow the declarative approach of the modern layers. What differs is where the result lands: an ordinary Excel PivotTable over XMLA. Few declarative layers go there. As of November 2026, Cube's MDX API does give a native PivotTable, but it is in preview, on the Enterprise plan, and only for Excel on Windows. Looker's Excel connector opens a single Explore query as a PivotTable over ODBC, also on Windows. dbt's Excel add-in returns query results into cells.
Limits
- No calendar level in the query. If there is no calendar level on the axes, in filters or in slicers,
--offsetshows 0: there is nothing to compare with. - One equality. The measure group and the calendar must be joined by a single
fact.column = calendar.column. --excludetakes a whole source. To exclude one attribute, move it into its own source.- Access filters and
--exclude. An access filter on a dimension does not restrict a measure that excludes that dimension, because the copy has no join to it. If a role must not see the total across that dimension, grant the rights through a table joined to the measure source. --semifinds the last day per measure group. For complete daily snapshots this makes no difference. For sparse data, such as a price that changes once a quarter, a store without a row on that day shows 0. Keep a daily snapshot or expand the table over the calendar.- Drillthrough. On a
--semimeasure it returns the rows of the whole period of the cell, not only the last day. On a visible--excludemeasure it fails when the excluded dimension is in the cell, so keep the denominator hidden and expose the share. - Large facts. The interval form gets no partition pruning. Prefer a key column, built one-to-one.
One more: many-to-many totals
A classic trap in cube modeling: two managers are assigned to one region, the join produces two copies of each fact row, and a subtotal that simply adds them up counts the region twice.
In the same version, the region subtotal and the grand total count each fact row once, while each manager's row still shows the whole region. No Jinja is needed. For counts, use count(fact.id) rather than count(*).

Every manager row shows the whole region; the region and the grand total count it once.
The tags are available from version 2.1.4. To try them on your own warehouse, install the free XLTable Desktop; the full reference is in the docs under Smart measures and Value at a date.