Iterative functions have one special ability, and that is to form a “Virtual” row context for the table supplied as an argument.
Table vs Scalar Iterative Functions
The most used iterative functions are:
- SUMX, MINX, MAXX, AVERAGEX, CONCATENATEX, COUNTX, RANKX
- Pretty much any function ending with X
- All those functions have 2 things in common, they form row context and return a scalar value
- FILTER, ADDCOLUMNS, SUMMARIZE, GENERATE etc.
- These functions create row context, but instead of scalar value, they return modified tables as result.
In this chapter, we will focus on the “X” group of iterators, while table iterators will be covered in the next chapter.
Syntax
Almost all “X” type iterators have the same syntax which we will explain with the [SalesAmountCorrect] measure. We will again focus on the first row in a visual filtered by the Black color.
SalesAmountCorrect = SUMX(Sales,Sales[Price]*Sales[QTY])
- The original filter context is formed with the condition Sales[Color] = “Black”
- Source table gets filtered to contain only sales of color “Black”
- After the table has been filtered by the original filter context, the calculation starts.
SUMX accepts 2 arguments:- First argument (Sales), is the table upon which we wish to create row-by-row calculation (row context)
- Sales table is not the whole table, but the one already filtered by the original filter context!
o We can picture this as a filtered Sales table to which we add a “Virtual” column called X
- Sales table is not the whole table, but the one already filtered by the original filter context!
- The Second argument of SUMX function is the calculation that will be performed in the virtual X column, respecting row-by-row rules of iteration.
- In our example, the calculation that should be performed row-by-row is Sales[Price]*Sales[QTY]
- Notice that we didn’t need to use any aggregation (such as SUM) since the X part of the calculation operates in a row context, meaning Sales[Price] can only reference the current row of the iteration. This applies to all rows of the X column.
- First argument (Sales), is the table upon which we wish to create row-by-row calculation (row context)
- Once the iteration finishes and the virtual X column computes the row-by-row calculation, the SUM part of the SUMX function starts. It sums up all the computed values of the virtual X column.
Explanation of X in Iterative Functions
The X part works the same for all X types of iterative functions. The only part that differs is the function prefix before X.
- SUMX will return the sum of the virtual X column
- MINX will return the min of the virtual X column
- CONCATENATEX will return the string of X column values combined
Iterators that return scalar values are a great replacement for calculated columns with simple logic, such as column1 +-*/ column2, as well as any kind of Calculated Column of Value type. Calculated columns should still be used in case we wish to create Filter types of columns (e.g. the ones we wish to plot on visual as filters, since Measures itself cannot be used to create the original filter context).