Skip to main content

Multi-statement ATL blocks

A block of ATL can contain multiple statements. For example:

[[Q1data = filterRows(WholeTable, 'Quarter', eq('Q1')); totalVal(columnsInRegion(Q1data, Sales))]]

This block contains two statements: the first creates the Q1data variable, which returns a filtered table region; the second uses Q1data to compute the total sales value for Q1. A semicolon ( ; ) terminates the first statement.

There are two other ways to terminate a statement:

  • with a line break

  • with a paragraph break

Therefore, you could reorganize the code in these ways:

Terminate statement with a line break

[[

Q1data = filterRows(WholeTable, 'Quarter', eq('Q1'))
totalVal(columnsInRegion(Q1data, Sales))

]] 

Terminate statement with a paragraph break

[[

Q1data = filterRows(WholeTable, 'Quarter', eq('Q1'))

totalVal(columnsInRegion(Q1data, Sales))

]] 

Important

An ATL block returns the value of its last statement only. There is no requirement to terminate the last statement.

Organizing a multi-statement ATL block into multiple lines makes your ATL easier to read and follow. Bear in mind that you can add comments inside an ATL block. This approach is great for writing a user-defined function (UDF). For example:

#value salesAnalytics (startDate, endDate)

[[

// filter the whole table using the startDate and endDate values
	
    filteredData = filterRows(WholeTable, 'Date', value -> value >= startDate && value <= endDate)
	
// get aggregated Sales values for period
	
    sumVal = totalVal(columnsInRegion(filteredData, Sales))
	
    avgVal = mean(columnsInRegion(filteredData, Sales))
	
    medVal = median(columnsInRegion(filteredData, Sales))
	
// get group-by-Country values for period (top Country only) 
	
    topCountryData = groupByTable(filteredData, Country, Sales)[ :1]
	
    topCountryName = topCountryData[0][0]
	
    topCountrySales = topCountryData[0][1]
	
// create and populate an results object
	
    keys = makeList('sumVal', 'avgVal', 'medVal', 'topCountryName', 'topCountrySales')
	
    values = makeList(sumVal, avgVal, medVal, topCountryName, topCountrySales)
	
    createAtlObject(keys, values)
	
]]

The example above is a #value script. The script has a single ATL block, containing 10 ATL statements and four explanatory comments. All statements and comments are terminated using a paragraph break.

Tip

If you want to indent lines of code, use the Increase Indent and Decrease Indent buttons in the toolbar.

KC_UDF_indentIcons.png

Writing conditionals as multi-line blocks

The if and ifelse statements in a conditional statement do not require a statement terminator.

Therefore, you can write a conditional like this:

[[number = chooseAtRandom(-3, -8, -15, 0, 8, 44, 99); if(number >= 10) {[[number]] is bigger than 10.} elseif(number < 0) {[[number]] is a negative number.} else{[[number]] is a single-digit number.}]]

Note that no semicolon, line break, or paragraph break terminates the if and elseif statements within the conditional.

However, you can arrange a conditional as a multi-line block to improve readability. For example:

[[

number = chooseAtRandom(-3, -8, -15, 0, 8, 44, 99)

if(number >= 10) {[[number]] is bigger than 10.} 

elseif(number < 0) {[[number]] is a negative number.} 

else{[[number]] is a single-digit number.}

]]