The syntax of calculated expressions in DataLens is similar to SQL.
Simple expressions can be described using ordinary arithmetic operations:
([Sales] - [Profit]) / 10
[Date] - #2019-01-24#
More complex expressions use multiple functions to implement various calculations, aggregations, and conversions of data from one type to another:
CONCAT(SUM([Category Sales]) / [Total Sales], ' %')
Dataset fields in calculations
The syntax used for accessing dataset fields is similar to Transact-SQL, but in DataLens, the field name must be enclosed in square brackets (
In addition to fields, operators, and functions, expressions can include constants of different data types:
- Fractional number:
- Date and time:
Operators available in expressions:
([Sales per Order] * [OrderCount]) - [Profit]
([Profit] / [Cost Price]) * 100
[City] + " " + "city"
[Mass] * [Speed] ^ 2
Remainder of a division:
[Sales] % 10 + [Cost Price] % 10
Comparison operators let you create logical chains:
1 > x > -4 > y != 8
Full description of all operators.
Any formula can be written in one or several lines:
CONCAT( SUM([Category Sales]) / [Total Sales], ' %' )
Comments are used to add explanations or to ignore parts of formulas:
-- This is a one-line comment
/* This is a block comment */
Logical functions are used for branching calculations in expressions:
CASE [ProductID] WHEN 1 THEN "Bananas" WHEN 2 THEN "Apples" WHEN 3 THEN "Pears" ELSE "Other" END
CASE( [Color], "R", "Red", "G", "Green", "B", "Blue", "Not RGB" )
Full description of the CASE.
IF([MassIndex] BETWEEN 18.5 AND 25, "Normally", "Not normal")
IF [Year] % 400 = 0 OR ([Year] % 4 = 0 AND [Year] % 100 != 0) THEN "Leap year" ELSE "Ordinary year" END
IF [City] = "Moscow" THEN "This is the Capital" ELSEIF [City] = "St. Petersburg" THEN "This is the northern Capital" ELSE "Other city" END
Full description of the IF.
IFNULL([Cost Price], 10) * [OrderCount]
Full description of the IFNULL.
IF(ISNULL([Product Name]) = TRUE, "Unnamed", [Product Name] + " " + [ProductID])
Full description of the ISNULL.
ZN([Total Sales]) - ZN([Total Cost])
Full description of the ZN.
The following string functions:
CONCAT([Total Sales], "$")
IF(CONTAINS([Product Name], "RU"), [Product Cost] + " " + "RUB", [Product Cost] + " " + "USD")
REPLACE([OrderID], "2020", [Month])
IF(STARTSWITH([Region Name], "RU_"), SPLIT([Region Name], "_", 2), [Region Name])
Strings can be enclosed in single or double quotes. In this case, one type of quotation mark can be used inside the other:
FIND([Product Name], 'plus')
CONCAT('"', [Product Name], '"')
You can make different conversions of string data using special characters in formulas:
REPLACE([ShopAddress], "\n", " ")
"File path" + " = " + "\\" + [Folder] + '\\' + [Filename]
Special characters such as
\r do not affect the display of the source data.
Expression values can be converted from one type to another:
DATETIME(STR([Order Date]) + "-" + STR([Order Time]))
Full description of the type conversion.
To calculate the resulting values, use aggregate functions.
AVG([Check Total]) * COUNTD([CustomerID])
SUM_IF([Sales], [Category] = "Fruits")
IF MIN[Date] = #2020-01-01# THEN SUM[Sales] * 1.1 ELSE SUM[Sales] * 1.2 END
Window features let you aggregate values from a group of strings, without combining these strings into one. This distinguishes them from aggregate functions. You can also use window functions to calculate values for one string in the context of values from other strings.
SUM([Sales] WITHIN [PaymentType]) / SUM([Sales] TOTAL)
MSUM([Sales per Order], 1 WITHIN [ProductID] ORDER BY [Price])
RANK_DENSE(AVG([Price]), "desc" WITHIN [ShopID] BEFORE FILTER BY [PaymentType])
To create text with markup, use markup functions:
MARKUP(BOLD("This"), " ", ITALIC("is"), " ", URL("https://example.com/", [LinkName])))
To use numeric constants when marking up URLs, convert them to the
URL("https://example.com/?value=" + STR([Value]), [Value]))