MinAI - Về trang chủ
Hướng dẫn
6/1340 phút
Đang tải...

Calculated Fields in Tableau

Tạo calculated fields, parameters, sets, và LOD expressions trong Tableau

0

🎯 Mục tiêu bài học

TB5 min

Sau bài học này, bạn sẽ:

✅ Tạo calculated fields: arithmetic, string, date functions

✅ Logic: IF/ELSEIF, CASE, IIF, AND/OR

✅ Sử dụng Parameters cho dynamic controls

✅ Hiểu Table Calculations: Running Total, YoY, Rank

✅ Viết LOD Expressions: FIXED, INCLUDE, EXCLUDE

Thời gian: 40 phút | Độ khó: Intermediate | Tool: Tableau Desktop / Public

1

📖 Bảng Thuật Ngữ Quan Trọng

TB5 min
Thuật ngữTiếng ViệtMô tả
Calculated FieldTrường tính toánCustom formula tạo bởi user
ParameterTham sốDynamic input control cho user
Table CalculationTính toán bảngRunning total, rank, YoY — tính trên result set
LOD ExpressionBiểu thức Level of DetailFIXED/INCLUDE/EXCLUDE — tính ở level khác viz
FIXEDCố địnhTính ở level cố định bất kể filters
INCLUDEBao gồmThêm dimension vào calculation
EXCLUDELoại trừBỏ dimension khỏi calculation
IIFInline IfIF/ELSE ngắn gọn 1 dòng
RUNNING_SUMTổng tích lũyCumulative total từ đầu đến row hiện tại
RANKXếp hạngSắp xếp theo giá trị measure

Checkpoint

Calculated Field = custom formula. Parameter = dynamic user input. Table Calc = chạy trên result set (running total, rank). LOD = tính ở level khác viz!

2

🔢 1. Basic Calculations

TB5 min

1.1 Tạo Calculated Field

Ví dụ
1Right-click data pane → Create Calculated Field
2HOẶC Analysis menu → Create Calculated Field

1.2 Arithmetic

Ví dụ
1// Profit Margin
2[Profit] / [Sales]
3
4// Revenue per Unit
5[Sales] / [Quantity]
6
7// Discount Impact
8[Sales] * [Discount]
9
10// Tax Amount (10%)
11[Sales] * 0.1

1.3 String Functions

Ví dụ
1// Full Name
2[First Name] + " " + [Last Name]
3
4// Uppercase
5UPPER([Category])
6
7// Extract first 3 characters
8LEFT([Product ID], 3)
9
10// Replace text
11REPLACE([Status], "Pending", "Chờ xử lý")
12
13// Contains check
14CONTAINS([Product Name], "Premium")

1.4 Date Functions

Ví dụ
1// Year extract
2YEAR([Order Date])
3
4// Days between dates
5DATEDIFF('day', [Order Date], [Ship Date])
6
7// Add 30 days
8DATEADD('day', 30, [Order Date])
9
10// Current date
11TODAY()
12
13// Quarter
14DATEPART('quarter', [Order Date])

Khi tạo Calculated Field, Tableau hiện validation ngay — nếu formula sai sẽ báo lỗi. Dùng search trong editor để tìm functions nhanh!

Checkpoint

Arithmetic: [Profit]/[Sales]. String: UPPER, LEFT, REPLACE, CONTAINS. Date: YEAR, DATEDIFF, DATEADD, TODAY. Tableau validates formula ngay khi viết!

3

🔀 2. Logic & Conditions

TB5 min

2.1 IF-THEN-ELSE

Ví dụ
1IF [Sales] > 10000000 THEN "VIP"
2ELSEIF [Sales] > 5000000 THEN "Premium"
3ELSEIF [Sales] > 1000000 THEN "Standard"
4ELSE "Basic"
5END

2.2 CASE Statement

Ví dụ
1CASE [Region]
2 WHEN "North" THEN "Miền Bắc"
3 WHEN "Central" THEN "Miền Trung"
4 WHEN "South" THEN "Miền Nam"
5 ELSE "Khác"
6END

2.3 IIF (Inline If)

Ví dụ
1// Profit Flag
2IIF([Profit] >= 0, "Có lãi", "Lỗ")
3
4// On-time delivery
5IIF([Ship Date] <= [Expected Date], "On Time", "Late")

2.4 Logical Operators

Ví dụ
1// AND + OR
2IF [Region] = "South" AND [Sales] > 5000000
3 AND YEAR([Order Date]) = 2025
4THEN "Target Customer"
5ELSE "Other"
6END

Checkpoint

IF/ELSEIF/ELSE/END cho multi-condition. CASE/WHEN cho value matching. IIF cho inline if/else ngắn gọn. AND/OR combine conditions!

4

📊 3. Aggregates & Ratios

TB5 min

3.1 Common Aggregates

Ví dụ
1SUM([Sales]) -- Total
2AVG([Sales]) -- Average
3COUNTD([Customer ID]) -- Count distinct
4MAX([Sales]) -- Maximum
5MIN([Sales]) -- Minimum

3.2 Ratios & Percentages

Ví dụ
1// Profit Margin %
2SUM([Profit]) / SUM([Sales])
3
4// Category Share of Total
5SUM([Sales]) / TOTAL(SUM([Sales]))
6
7// Running Total
8RUNNING_SUM(SUM([Sales]))
9
10// Cumulative %
11RUNNING_SUM(SUM([Sales])) / TOTAL(SUM([Sales]))

Checkpoint

SUM, AVG, COUNTD, MAX, MIN — common aggregates. SUM/TOTAL = % of total. RUNNING_SUM = cumulative. Ratios luôn dùng SUM wrapping cả tử và mẫu!

5

🎛️ 4. Parameters

TB5 min

4.1 Create Parameter

Ví dụ
1Right-click → Create Parameter
2- Name: "Select Metric"
3- Data type: String
4- Allowable values: List (Sales, Profit, Quantity)
5- Current value: Sales

4.2 Dynamic Metric Selector

Ví dụ
1CASE [Select Metric]
2 WHEN "Sales" THEN SUM([Sales])
3 WHEN "Profit" THEN SUM([Profit])
4 WHEN "Quantity" THEN SUM([Quantity])
5END

4.3 Top N Filter

Ví dụ
1// Parameter: "Top N" (Integer, 1-20, default 10)
2// Calculated Field:
3INDEX() <= [Top N]
4
5// Usage: Drag to Filters → True

4.4 Date Range Parameter

Ví dụ
1// Parameters: Start Date, End Date
2// Filter calculation:
3[Order Date] >= [Start Date]
4AND [Order Date] <= [End Date]

Parameters biến dashboard thành dynamic — users chọn metric, Top N, date range mà không cần edit. Right-click Parameter → Show Parameter Control để hiện trên dashboard!

Checkpoint

Parameters = dynamic user controls. CASE + Parameter = metric selector. INDEX + Parameter = Top N. Date Parameters = custom date range. Show Parameter Control trên dashboard!

6

📈 5. Table Calculations

TB5 min

5.1 Quick Table Calculations

Ví dụ
1Right-click measure → Quick Table Calculation:
2- Running Total
3- Difference
4- Percent Difference
5- Percent of Total
6- Rank
7- Moving Average

5.2 Year-over-Year Growth

Ví dụ
1(SUM([Sales]) - LOOKUP(SUM([Sales]), -1))
2/ ABS(LOOKUP(SUM([Sales]), -1))
3
4// Compute using: Table (across months/quarters)

5.3 Moving Average

Ví dụ
1// 3-Month Moving Average
2WINDOW_AVG(SUM([Sales]), -2, 0)
3
4// 7-Day Moving Average
5WINDOW_AVG(SUM([Sales]), -6, 0)

5.4 Rank

Ví dụ
1RANK(SUM([Sales])) -- Standard rank
2RANK_DENSE(SUM([Sales])) -- Dense rank (no gaps)
3RANK(SUM([Sales]), 'desc') -- Descending

Table Calculations chạy trên result set — nghĩa là phụ thuộc vào context. Luôn check: Right-click → Compute Using → chọn đúng direction (Table across, Table down, Specific dimension)!

Checkpoint

Quick Table Calc: right-click → Running Total, Rank, YoY. LOOKUP = access other rows. WINDOW_AVG = moving average. Luôn check Compute Using direction!

7

🎯 6. LOD Expressions

TB5 min

LOD expressions tính toán ở mức granularity khác với visualization.

6.1 FIXED

Ví dụ
1// Customer's Total Sales (bất kể filter nào)
2{ FIXED [Customer ID] : SUM([Sales]) }
3
4// Category Average
5{ FIXED [Category] : AVG([Profit]) }

6.2 INCLUDE

Ví dụ
1// Average of per-customer sales (more granular than viz)
2AVG({ INCLUDE [Customer ID] : SUM([Sales]) })

6.3 EXCLUDE

Ví dụ
1// Sales regardless of sub-category
2{ EXCLUDE [Sub-Category] : SUM([Sales]) }

6.4 Common LOD Use Cases

Use CaseLOD Expression
Customer first purchase{ FIXED [Customer] : MIN([Order Date]) }
Customer lifetime value{ FIXED [Customer] : SUM([Sales]) }
% of parent categorySUM([Sales]) / { FIXED [Category] : SUM([Sales]) }
Cohort analysis{ FIXED [Customer] : MIN(YEAR([Order Date])) }

LOD = advanced feature! FIXED bỏ qua filters (trừ Context Filters). INCLUDE thêm granularity. EXCLUDE bớt granularity. Luôn test trước khi dùng trong production!

Checkpoint

FIXED = tính ở level cố định, bỏ qua filters. INCLUDE = thêm dimension (more granular). EXCLUDE = bỏ dimension (less granular). Common: Customer LTV, Cohort, % of Parent!

8

🏋️ 7. Thực hành

TB5 min
Exercises

Áp dụng calculated fields vào scenarios thực tế!

Exercise 1: Customer Segmentation

Tạo calculated field phân loại khách hàng:

  • VIP: Total spend > 10M VND
  • Premium: 5M - 10M VND
  • Standard: 1M - 5M VND
  • Basic: Under 1M VND

Exercise 2: Dynamic Dashboard

Tạo parameter cho phép user chọn metric (Sales/Profit/Quantity) và Top N items.

Exercise 3: YoY Comparison

Tạo chart showing monthly sales with YoY growth percentage.

9

📋 Tổng kết

TB5 min

Kiến thức đã học

Chủ đềNội dung chínhTầm quan trọng
Basic CalcsArithmetic, String, Date functionsFoundation
LogicIF/CASE/IIF, AND/ORSegmentation
AggregatesSUM, AVG, COUNTD, ratiosMetrics
ParametersDynamic controls, Top N, Date rangeInteractivity
Table CalcsRunning total, YoY, Rank, Moving avgTime analysis
LODFIXED, INCLUDE, EXCLUDEAdvanced analysis

Câu hỏi tự kiểm tra

  1. Table Calculations khác Regular Calculations thế nào?
  2. LOD Expression: FIXED vs INCLUDE vs EXCLUDE?
  3. Parameters trong Tableau dùng để làm gì?
  4. Khi nào dùng IF, khi nào dùng CASE?

Bài tiếp theo: Tableau Dashboards — Combine charts thành interactive dashboards!

🎉 Tuyệt vời! Bạn đã thành thạo Calculated Fields trong Tableau!

Nhớ: LOD là killer feature của Tableau — nắm vững FIXED và bạn giải được 90% bài toán khó!