Database queries are used to define which data is pulled in from your BI/Database system into Lucanet xP&A (Extended Planning & Analysis). They are entered during the set-up process of the integration (see individual set-up instructions for each system) and need to be structured in a certain way.

In this chapter we explain how a database query is defined, how to import dimension hierarchies and what needs to be considered in regards to linked dimensions that are queried.

Rules:

  • A query starts with select.
  • A query must return a date column.
  • A query must return the variables in one of the following ways:
    • At least one value column, with a separate column for each variable that contains numbers or dates
    • One column with the variable names and one column with their numeric values
  • A query can return optional columns for dimensions, dimension mappings, or cohorts.
  • Linked dimensions must be included in the following form `from dimension > to dimension` (using backticks).
  • A query must contain from followed by the name of the table the data is to be extracted from.

Example query:

select date, users, country, region as "country > region" from users_table

Explanation:

  • The data is queried from the users_table.
  • The first column (date) has the dates of the transactions (timestamp or date type).
  • The second column (users) contains the values of the "users" variable (number type).
  • The third column (country) is a dimension column, the users are broken down by country (string).
  • The fourth column ( region as "country > region") is a dimension mapping. The region is not an independent dimension, but the countries are assigned to regions.

Example query result:

Shows the result of the above query example. A table with 4 columns: date, users, country, country > region
Example query result

Querying Constant Values

If you only want to import constant values (no time series), the date column can be null. For example:

select null as date, price, plan_name from price_table

When you create or edit a database data source in xP&A, choose the option that matches your query results:

  • Variable names as column names: Each variable has its own numeric column, e.g. Revenue and Costs. Enter these columns under Variable columns.
  • Variables as values in a single column: One column contains the variable names and another column contains their numeric values. Enter the column with the variable names under What column is the variable name in? and the column with the numeric values under Where is the value?.

You configure the date column separately. xP&A treats the remaining columns as dimensions. You do not need to pivot the query results into separate variable columns.

Example query with variables in a single column:

sql
    
  

The following table shows the result of the example query:

datemetricamountcountry
2026-01-01Revenue1000Germany
2026-01-01Costs600Germany
2026-02-01Revenue1200Germany
2026-02-01Costs700Germany

For this query result, configure the data source in xP&A as follows:

  • Enter date under Date column.
  • Select Variables as values in a single column.
  • Enter metric under What column is the variable name in?.
  • Enter amount under Where is the value?.

The query form with this configuration is displayed as follows, for example:

Shows the 'Complete the query form' step with the example configuration. The option 'Variables as values in a single column' is selected, 'metric' is entered under 'What column is the variable name in?', and 'amount' is entered under 'Where is the value?'.

As a result, Revenue and Costs become separate data items in xP&A that you can link to model variables. The country column becomes a dimension. The metric column identifies the variables and does not become a dimension.

You can import a dimension hierarchy in xP&A directly from the values that a SQL query returns. To do so, activate the Import dimension hierarchies check box when you create or edit the SQL data source. The check box is available for all SQL data sources in xP&A. It is located below the Variable columns field in the data source dialog:

Shows the 'New data source: Data Hub' dialog in xP&A with the 'Query' and 'Date column' fields, the 'Variable names as column names' and 'Variables as values in a single column' options, the 'Variable columns' field and the 'Import dimension hierarchies' check box with the help text 'Interpret values such as "Parent > Child" as a parent and child dimension items. Leave off to keep > as part of item names.' Highlighted in red is the deactivated 'Import dimension hierarchies' check box.
'Import dimension hierarchies' check box in the data source dialog

The Import dimension hierarchies check box is deactivated by default. When the check box is deactivated, > remains part of the dimension item name. For example, xP&A imports Retail > Customer A as one flat item. Activate the check box only when your query returns hierarchy paths.

When the check box is activated, separate the hierarchy levels in the values of a dimension column with >. For example, Retail > Customer A creates Retail as a parent item and Customer A as its child item. xP&A ignores spaces before and after >. A path can include more than two levels. xP&A creates missing parent items automatically.

The position of > determines its meaning. The following table compares > in a dimension column's value with > in a column header:

Where it appearsExampleMeaning
In a dimension column's value, with the Import dimension hierarchies check box activatedRetail > Customer AA parent and child within the Customer dimension
In a column headerCustomer > CountryA mapping from the Customer dimension to the Country dimension

For example, a query can return the following result:

dateRevenueCustomerCustomer > Country
2026-01-01100Retail > Customer AGermany
2026-01-01200Retail > Customer BFrance
2026-01-01300Wholesale > Customer CGermany

When the Import dimension hierarchies check box is activated, the Customer dimension contains Retail with Customer A and Customer B beneath it, and Wholesale with Customer C beneath it. xP&A assigns the revenue values to the customer items. The Customer > Country column maps Customer A to Germany, Customer B to France, and Customer C to Germany. The automatically created parent items Retail and Wholesale do not receive these customer-to-country mappings.

Refreshing a hierarchy: xP&A identifies each imported item by its full path. When you refresh the data source and a path is unchanged, the item remains the same. If a path changes, for example from Retail > Customer A to Wholesale > Customer A, xP&A creates a new linked item at the new path and detaches the old item from the data source. xP&A does not move existing manual inputs on the old item to the new item.

Limits and input quality: Automatically created parent items count toward the dimension item limit. Use named items at every hierarchy level. xP&A skips empty segments in malformed paths. If a value contains no named segments, xP&A leaves the value unassigned and the import reports a warning.

Hierarchy levels within one dimension are different from chained mappings between dimensions. Activating the Import dimension hierarchies check box does not remove the limitations for querying linked dimensions (see Limitations and Workarounds for Querying Linked Dimensions in xP&A).

When working with queries, you can only create one level of linked dimensions (mappings) per query. This means you can establish multiple direct relationships from a single source dimension, but you cannot create chained relationships within the same query.

Allowed in one query:

  • A → B
  • A → C
  • A → D

Not allowed in one query:

  • A → B and B → C simultaneously

Workaround Solution

To handle multi-level relationships, you need to create two separate queries:

Query 1: Define the initial mapping relationship

Query 2: Define the secondary mapping relationship using a fictional value

Consider a scenario where:

  • Lease contracts are tied to specific vehicles/license plate (A → B)
  • Vehicles are linked to vehicle types (B → C)
  • The relationship isn't always 1:1 due to vehicles appearing in multiple contracts

The following queries would be used:

Query 1: Lease Contract to License Plate Mapping

Query 1
Query 1

Query 2: License Plate to Vehicle Type Mapping

Query 2
Query 2

Explanation:

  • The second query uses a fictional value (in this case, 1) to satisfy query requirements which will not be used in any model.
  • Both queries use null as date as a placeholder.
  • The mapping syntax uses > to indicate the relationship direction.
  • This approach maintains data integrity while working within the system's constraints.

This content was generated using AI and reviewed by Lucanet subject matter experts before publication.