MicroStrategy ONE
OLAPAvg (OLAP Average)
Returns the average of the current value, preceding values, and succeeding values as defined in the function arguments. Unlike RunningAvg
and MovingAvg
, which can only include values above the current row in the calculation, you can use OLAPAvg
to include values both above and below the current row in the calculation.
The calculations can be restarted based on attributes defined in the function parameters.
Syntax
OLAPAvg<Distinct, OLAPWinStType, OLAPWinStOffset, OLAPWinEndType, OLAPWinEndOffset, BreakBy, SortBy>(
Argument
)
Where:
Distinct
is aTRUE
/FALSE
parameter that allows you to use all values in the calculation or to calculate using only the unique values. If you defineDistinct
to be true, then the parametersOLAPWinStType
,OLAPWinStOffset
,OLAPWinEndType
,OLAPWinEndOffset
, andSortBy
are ignored.BreakBy
is the attribute indicating where the calculation restarts.SortBy
is the attribute or metric by which the data is sorted.OLAPWinStType
defines the window type for the starting location of the calculation. Select one of the following options, listed by the name of the setting and its correspondingOLAPWinStType
value in parentheses:- Top of data set (
0
): The calculation starts at the top value as determined by theBreakBy
andSortBy
values. - Current row (
2
): The calculation starts at the current row. N
rows before current row (3
): The calculation starts a number of rows before the current row. You define this offset of rows with theOLAPWinStOffset
parameter described below.N
rows after current row (4
): The calculation starts a number of rows after the current row. You define this offset of rows with theOLAPWinStOffset
parameter described below.
- Top of data set (
OLAPWinStOffset
defines the offset of rows from the current row to start the calculation. You can define this offset if theOLAPWinStType
parameter is defined asN
rows before current row (3
) orN
rows after current row (4
).OLAPWinEndType
defines the window type for the ending location of the calculation. Select one of the following options, listed by the name of the setting and its correspondingOLAPWinEndType
value in parentheses:- Bottom of data set (
1
): The calculation stops at the bottom value as determined by theBreakBy
andSortBy
values. - Current row (
2
): The calculation stops at the current row. N
rows before current row (3
): The calculation stops a number of rows before the current row. You define this offset of rows with theOLAPWinEndOffset
parameter described below.N
rows after current row (4
): The calculation stops a number of rows after the current row. You define this offset of rows with theOLAPWinEndOffset
parameter described below.
- Bottom of data set (
OLAPWinEndOffset
defines the offset of rows from the current row to stop the calculation. You can define this offset if theOLAPWinEndType
parameter is defined asN
rows before current row (3
) orN
rows after current row (4
).Argument
is a metric representing a list of numbers.
The function is not valid if your starting point is at a lower point than your ending point.
Example
An OLAPAvg Unit Cost metric is created using the OLAPAvg
function based on the Unit Cost metric, as defined below:
OLAPAvg<OLAPWinStType=3, OLAPWinStOffset=3, OLAPWinEndType=4, OLAPWinEndOffset=2, BreakBy={Category} SortBy={Item}>([Unit Cost])
The starting point for the average is defined as three rows before the current row (OLAPWinStType=3, OLAPWinStOffset=3
). The stopping point for the average is defined as two rows after the current row (OLAPWinEndType=4, OLAPWinEndOffset=2
).
This metric is displayed on a report along with Category, Item, and Unit Cost, as shown below.
There are a few facts about this data to take note of.
The first value of OLAPAvg Unit Cost is $15. This is calculated by adding $32, $8, and $5 to get a total of $45. This total of $45 is then divided by three (the number of rows included in the calculation) to get the final result of $15. These rows are included because the calculation ends two rows after the current row. Even though the calculation starts three rows before the current row, there is no data above the current row to include in the calculation.
The fourth value for OLAPAvg Unit Cost is the first value that can include data from all three rows above the current row to two rows below the current row in the calculation (($32 + $8 + $5 + $25 + $19 + $20) / 6 = $18.17
). Notice that with this calculation a value of six is used to calculate the average because six rows were included in the average.
While it is not shown on this report, the calculation would restart for the first Item of the next Category because the function is defined to break by the Category attribute.