Level of Detail (LOD) expressions in Tableau allow you to compute values at the data source level and the visualization level, giving you more control over the granularity of your data. LOD expressions are powerful tools for creating complex calculations and aggregations that are not possible with standard calculated fields.
Key Concepts
- Fixed LOD Expressions: Compute values using the specified dimensions, ignoring the view's dimensions.
- Include LOD Expressions: Compute values using the specified dimensions in addition to the view's dimensions.
- Exclude LOD Expressions: Compute values using the specified dimensions, excluding certain dimensions from the view.
Syntax
The general syntax for LOD expressions is:
{ FIXED [dimension1], [dimension2], ... : [aggregate expression] } { INCLUDE [dimension1], [dimension2], ... : [aggregate expression] } { EXCLUDE [dimension1], [dimension2], ... : [aggregate expression] }
Practical Examples
Example 1: Fixed LOD Expression
Scenario: Calculate the total sales per region, regardless of the dimensions in the view.
Code:
Explanation:
- This expression calculates the sum of sales for each region, ignoring any other dimensions in the view.
Example 2: Include LOD Expression
Scenario: Calculate the average sales per customer within each region.
Code:
Explanation:
- This expression calculates the average sales per customer, including the customer name dimension in addition to the dimensions in the view.
Example 3: Exclude LOD Expression
Scenario: Calculate the total sales for each region, excluding the product category dimension.
Code:
Explanation:
- This expression calculates the sum of sales for each region, excluding the product category dimension from the calculation.
Practical Exercise
Exercise 1: Fixed LOD Expression
Task: Create a fixed LOD expression to calculate the total profit per state, regardless of the dimensions in the view.
Steps:
- Open Tableau and connect to the Sample - Superstore data source.
- Create a new calculated field named "Total Profit per State".
- Enter the following code:
{ FIXED [State] : SUM([Profit]) }
- Drag the "State" dimension to the Rows shelf.
- Drag the "Total Profit per State" calculated field to the Columns shelf.
Solution:
- You should see the total profit for each state, regardless of any other dimensions in the view.
Exercise 2: Include LOD Expression
Task: Create an include LOD expression to calculate the average discount per customer within each region.
Steps:
- Create a new calculated field named "Average Discount per Customer".
- Enter the following code:
{ INCLUDE [Customer Name] : AVG([Discount]) }
- Drag the "Region" dimension to the Rows shelf.
- Drag the "Average Discount per Customer" calculated field to the Columns shelf.
Solution:
- You should see the average discount per customer for each region.
Exercise 3: Exclude LOD Expression
Task: Create an exclude LOD expression to calculate the total sales for each region, excluding the product sub-category dimension.
Steps:
- Create a new calculated field named "Total Sales per Region Excluding Sub-Category".
- Enter the following code:
{ EXCLUDE [Sub-Category] : SUM([Sales]) }
- Drag the "Region" dimension to the Rows shelf.
- Drag the "Total Sales per Region Excluding Sub-Category" calculated field to the Columns shelf.
Solution:
- You should see the total sales for each region, excluding the product sub-category dimension.
Common Mistakes and Tips
-
Common Mistake: Misunderstanding the scope of LOD expressions.
- Tip: Always remember that
FIXED
ignores the view's dimensions,INCLUDE
adds dimensions to the view, andEXCLUDE
removes dimensions from the view.
- Tip: Always remember that
-
Common Mistake: Using LOD expressions when a simple calculated field would suffice.
- Tip: Evaluate whether an LOD expression is necessary for your calculation. Sometimes, a standard calculated field can achieve the same result with less complexity.
Conclusion
Level of Detail (LOD) expressions are a powerful feature in Tableau that allow you to perform complex calculations and aggregations at different levels of granularity. By mastering LOD expressions, you can create more sophisticated and accurate visualizations. In the next section, we will explore creating dashboards, where you can combine multiple visualizations to tell a comprehensive story with your data.
Tableau Course
Module 1: Introduction to Tableau
- What is Tableau?
- Installing Tableau
- Tableau Interface Overview
- Connecting to Data Sources
- Basic Data Types and Structures
Module 2: Basic Visualization Techniques
- Creating Your First Visualization
- Using Marks and Cards
- Building Basic Charts
- Filtering Data
- Sorting and Grouping Data
Module 3: Intermediate Visualization Techniques
- Using Calculated Fields
- Creating Dual-Axis Charts
- Using Parameters
- Creating Maps
- Using Table Calculations
Module 4: Advanced Visualization Techniques
- Advanced Chart Types
- Using LOD Expressions
- Creating Dashboards
- Dashboard Actions
- Storytelling with Data
Module 5: Data Preparation and Transformation
Module 6: Advanced Analytics
Module 7: Performance Optimization
- Optimizing Workbook Performance
- Extracts vs Live Connections
- Reducing Load Times
- Performance Recording
- Best Practices for Performance
Module 8: Tableau Server and Online
- Introduction to Tableau Server
- Publishing Workbooks
- Managing Permissions
- Scheduling Extracts
- Collaborating with Tableau Online