What's new ⬇ Download Reportworq
⬇ Guide PDF

Data model variables#

Early release. This feature is in early release, so its screens and behavior may change. Tell us what you think with Send feedback. See Early release features and Send feedback.

A variable lets a report supply a value to a data model view at run time: the fiscal year of a P&L, the region of a burst, the cost center of a budget submission. You write it as a {{Token}}, for example {{Year}}, and set how it behaves on the view's Variables tab. The values flow downstream to Preview, to reports, to bursting, and to the Excel add-in.

Where variables come from#

A view's Variables tab shows each variable the view uses as a card, headed by its caption (once you set one) and its {{Token}}, in up to three groups:

Group Where the token is Required?
Connection - required In the view's query: its SQL, MDX, or DAX, or a connector's query setting, for example WHERE fiscal_year = {{Year}}. Always. A query cannot run with a hole in it.
Filters In a field filter's value on the Fields tab, for example the Region field filtered to {{Region}}. A joined view's own filters count too. Your choice. An optional filter variable left blank drops its filter, so the view returns every region.
From the joined views In the input views of a joined view. See Variables of a joined view.

The sample value is not a default#

A view whose query has a {{Token}} needs a value to run while you build it, so the Design or Connection tab shows a Connection values sidebar for a sample value. Reportworq uses the sample to run the query, work out the view's fields, and fill in Preview while you author.

The sample is only for authoring. It never becomes the variable's default, and reports never see it. Set the default on the Variables tab. Preview starts from the default, and from the sample value when a view has no default. A joined view never asks for a sample value: see Variables of a joined view.

Set a default and whether a value is required#

  1. Open the data model, select the view in the rail, and select the Variables tab.
  2. In the variable's card, enter its Default: the value Preview and reports use when nothing overrides it, for example 2024. For an optional filter variable the box reads Optional: leave blank to skip the filter; otherwise it reads Default value.
  3. For a filter variable, select or clear Required at the top right of the card. A connection variable always shows Required and cannot be made optional.
  4. Select Save Changes.

A report, a burst, or the Excel add-in can override the default with its own value. In Preview, a required variable left blank brings a warning, "No value for required variable(s)", that names it.

Variables in Preview#

The Variable values pane in Preview starts with each variable's default. For a joined view that is the same value a run would use. Two rules then apply:

A box you have not touched follows the view's defaults as you change them.

Set how a report prompts for a variable#

Select Prompt at the top right of a variable's card to open its prompt settings. They decide what the person running the report sees when asked for the value.

Select Save Changes when you are done.

How a value is filled into a query#

People type values into prompts and Excel cells feed values into variables, so Reportworq makes each value safe where it is pasted into a SQL, MDX, or DAX query. A value can never change what the query does: it can only be a value. Reportworq looks at where each {{Token}} sits in the query text and fills it accordingly.

The rules by position are the following.

Where the token sits What happens to the value
Inside a quoted string, such as '{{Market}}' The quote character is doubled, so O'Brien becomes the text O'Brien, not the end of the string.
Inside a quoted name, such as [{{Column}}], "{{Column}}", or a DAX '{{Table}}' The closing character is doubled in the same way.
Bare, not inside quotes Only a number is accepted.
Inside a comment A value that would end the comment, or start a new one, is refused.

Two kinds of bare value are accepted as well as a number:

A value that breaks a rule fails the run, and nothing is sent to the source. The message names the variable and says what to do. For a bare token in SQL it reads:

The value for “Region” can't be used: it is pasted into the query without quotes, so it only takes a number (or, inside IN ( ), a list such as 1, 2, 3 or 'East', 'West'). For a text value, the data model's author should put quotes around it in the query: '{{Region}}'. Only as a last resort, for query text such as a column name, can the data model's author mark the variable as raw query text, which turns this protection off and pastes readers' values unchecked.

In MDX the message asks for brackets instead ([{{Region}}]), and in DAX for double quotes ("{{Region}}"). On a connection whose query uses syntax that databases read differently (see Database rules), the message says that the variable can only take a plain number unless the author marks it as raw query text, and suggests rewriting that part of the query.

What fails and how to fix it#

What you see Why Fix
The message above, for a text value such as East The token sits bare in the SQL (WHERE region = {{Region}}), so only a number is accepted. Quote the token in the query: WHERE region = '{{Region}}'.
The same message for a date such as 2024-01-01 A bare date is not a number. Quote it, '{{Day}}'.
The message, for a list such as East, West A list is accepted only directly inside parentheses, and each text item needs its own quotes. Write IN ({{Regions}}) and supply 'East', 'West'.
The message, for a column or table name (ORDER BY {{SortColumn}}) A name is query text, not a value. Select Raw query text, pasted as is for that variable (see Use a value as query text), and limit the values to a list.
A message that says the variable can only take a plain number The connection is ODBC, OLE DB, or CData, or the query runs a string as SQL, or has a lone carriage return. Rewrite that part of the query, move to a SQL Server or SQLite connection, or opt in as above.
No error, but no rows (IN ('{{List}}') with East','West) The value is now one text string, not several items. Write IN ({{List}}) with the value 'East', 'West'.

Write tokens so they fill cleanly#

The following patterns cover most queries.

To pass Write Example value
A text value in SQL WHERE market = '{{Market}}' Pacific Northwest
A number in SQL WHERE fiscal_year = {{Year}} 2024
A date in SQL WHERE posted_on >= '{{From}}' 2024-01-01
Several values in SQL WHERE region IN ({{Regions}}) 'East', 'West'
A member in MDX [Region].[{{Region}}] East
Members in MDX { {{Regions}} } [Region].[East], [Region].[West]
Text in DAX "{{Region}}" East

Database rules#

How a database reads quotes and comments differs, so Reportworq applies the rules of the view's connection:

Planning Analytics, Analysis Services, and Power BI#

MDX and DAX follow the same idea:

Field filters#

A value that reaches a field filter, and is pushed down to the source as part of its query, is written by its field's type: a number is checked and written as a number; text, dates, and true/false values are quoted with their quotes doubled; and each item of a list is written the same way. Nothing in the filter needs quoting by hand.

Adaptive Planning names#

A value placed in an Adaptive Planning sheet, version, period, level, or dimension name is not checked by position: it is filled in as given. Give such a variable a list of Choices with Also allow typing a value that isn't on the list turned off.

Use a value as query text#

Sometimes a variable is meant to change the query itself: a column to sort by (ORDER BY {{SortColumn}}), a table or schema name (FROM {{Schema}}.ledger), or an MDX set expression. A value like that is not a number, so it is refused unless you opt in.

  1. On the view's Variables tab, select Prompt on the variable's card.
  2. Under In the query, select Raw query text, pasted as is. A note under it reminds you that readers' values are then pasted into the query unchecked, and that quoting the token is the better choice for text.
  3. Select Save Changes.

The value is then pasted into the query exactly as given, with no checking. The setting is stored on the view's own query, so only the data model's author can turn it on: nothing a report or a caller sends can set it.

Warning: A raw query text variable can change what the query does. Whoever can supply its value can rewrite that part of the query against the source, with the connection's permissions. Turn it on only for a variable that genuinely holds query text, and give it a list of Choices with Also allow typing a value that isn't on the list turned off, so people pick from known values.

If you are upgrading#

Earlier releases pasted a value into the query as raw text. Queries that relied on that can stop working after the upgrade. Check views that match any of these patterns:

The run's error names the variable and the reason, so the failing view is easy to find.

Feedback on this page

Comments, questions, requests, or something missing or unclear? Email us - the page you are on is filled in for you.

Email feedback on this page

Or write to support@reportworq.com directly.