Lets Talk: Level-of-Detail Expressions
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 visualisations that crucially, could be unbound from the framework of the existing view - or rather, could be calculated on an altogether different set of dimensions, yet incorporated into the current visualisation.
Syntax & Scope
Like table calculations, Level-of-detail expressions (LOD Expressions) 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.
Syntax:
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:
{<Scope> <Dimension 1>, <Dimension 2> etc : <Aggregation>(<Measure>)}
- 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
{Fixed Category : (Sum(Sales) - Sum(Profit)) / Sum(Profit)}
This will calculate the percentage difference between the Sales and Profit for each category member (Superstore: Furniture, Office Supplies, Technology)
Scope:
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 calculates only on the specified dimension(s) {Fixed Region : Sum(Sales)} | 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. {Include Segment : Sum(Sales)} | 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 {Exclude Category : Sum(Sales)} | The highest-grain of all, where no scope (and therefore dimensions) are defined, the result is always the highest value of the result set {Sum(Sales)} |
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 |
Architecture
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 |
---|---|---|
Select Region ,Segment ,Sum(Sales) As Sales From Superstore Where Category In('Office Supplies', 'Technology') Group By Region, Segment |
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 |
---|---|---|
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:
Visual | SQL | Plan |
---|---|---|
Fixed LOD Expression: | Fixed LOD Expression: | Fixed LOD Expression: |
Window_Sum Calculation: | Window_Sum Calculation: | Window_Sum Calculation: |
So, when considering performance and server impact, which calculation would be the better to use here?
Concluding Remarks
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 query 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.