Beginning with version 9 in March 2015, Tableau rolled out its then newest calculation feature - Level of detail expressions, an all-new powerful extension set that enables developers to provide an extra-level of detail to their visuaisations that crucially, could be unbound from the framework of the existing view - or rather, could be calculated on an altogether dfferent set of dimensions, yet incorporated into the current visualisation.
Syntax & Scope
Like table calculations, Level-of-detail expressions (LOD Experessions) need to be scoped, but unlike table calcs which can consider cell, panes and windows, lod's need to be scoped to one or more dimensions, furthermore, the scope is defined in the calculation, and so persists for all calculations.
The syntax to initiate an lod remains the same regardless of scope, and, just like a regular table calculation, you are free to pre-determine the aggregation used, or, to allow Tableau to apply the default calculation (typically Sum), as you apply the calculation. The one caveat is that any measures used in the calculation must be aggregated:
- All level-of-detail expressions must begin and end with curly-braces
- Only one scope can be defined per expression
- Multiple scoped dimensions can be included in the expression, separated by a comma
- Scoped dimensions and the measures if used must be separated by a colon
- Just like regular calculations you are free to determine how data is aggregated but unlike regular expressions, lod's must be aggregate
- You can nest multiple expressions into one overall calculation
- lod's can be used inside table calculations
This will calculate the percentage difference between the Sales and Profit for each category member (Superstore: Furniture, Office Supplies, Technology)
Four types of scope exist for level-of-detail expressions:
|Fixed (Fixed grain)||Include (Lower Grain)||Exclude (Higher Grain)||None (Highest Grain)|
The Fixed-type caluclates only on the specified dimension(s)
Include aggregates across all the dimensions of the view and those of the expression, as a result, the effect of the Include-type will only be noticed when not used with sum.
The polar opposite to Include-scope, Exclude removes the named dimensions from the final result so calculates at a higher-grain than may be available in the given view
The highest-grain of all, where no scope (and therefore dimensions) are defined, the result is always the highest value of the resultset
Notice the difference here: Using include with a summation aggregation is functionally the same as a standard sum, only when the aggregation is set as Avg do do we see a difference
This is the tech-part, lets look at how level-of-detail expressions work, behind the scenes.
|Consider this view:||This is the SQL required to generate the view||And this is the plan with some of the key measurements highlighted|
Are LOD Expressions the Only Option?
Let us now consider a second view, this one uses the same Sum calculation, a Fixed LOD calculation and finally, a Window_Sum:
|The View||The SQL||And Finally, the plan|
Its quite big <expand>...
I think we can agree that for such a tiny table, the code is quite extensive; but did you notice that both the Fixed and the WIndow_Sum are calculating the same output.
Lets look at this in some more detail:
Fixed LOD Expression:
Fixed LOD Expression:
Can you guess the code? <click to expand>
Fixed LOD Expression:
And now the plan <click to expand>
Notice that double table scan - yup, regardless as to whether the table is optimised or not, this query generates two scans
Click here to expand...
The Window_Sum Plan <expand>
This plan is a lot smaller, almost 60% smaller than the LOD plan, and with only a single table scan (or index scan / seek if indexed) and crucially, no joins
So, when considering performance and server impact, which calculation would be the better to use here?
Yup, the Window_Sum. Why?
Well, because the window_sum is nothing more than a straightforward aggregation, the windowing function is applied inside Tableau.
But, despite the clear perfromance drawback to Level-of-detail expressions, you must always consider your use case, as Window Calculations still need your data to structured such that the scope can be applied, whereas LOD expressions, particularly Fixed expressions or Min/Avg/Max aggregations, give you power to provide additional information without a supporting structure.
Furthermore, it is possible to include lod expressions inside a window calc if needed, this is particularly useful when performing lookups and ranking operations.
It is plain to see that Level-of-detail expressions are a very valuable tool to include in your development arsenal, that said though, as with all features with Tableau, developers need to consider whether their use is justified in the view they are currently building.
It is clear from the demonstrations above that alternatives do exist, and where possible, these alternatives should be explored, especially when connecting Tableau to large datasets in excess of 1 million rows, as the impact of these overly costly querie will greatly impact time-to-render, and may also impact other resources.
I would also suggest you take time to assess the calculations you are building and see if possible whether the LOD expression can be re-written and included in the underlying set.