🎯 Mục tiêu bài học
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
📖 Bảng Thuật Ngữ Quan Trọng
| Thuật ngữ | Tiếng Việt | Mô tả |
|---|---|---|
| Calculated Field | Trường tính toán | Custom formula tạo bởi user |
| Parameter | Tham số | Dynamic input control cho user |
| Table Calculation | Tính toán bảng | Running total, rank, YoY — tính trên result set |
| LOD Expression | Biểu thức Level of Detail | FIXED/INCLUDE/EXCLUDE — tính ở level khác viz |
| FIXED | Cố định | Tính ở level cố định bất kể filters |
| INCLUDE | Bao gồm | Thêm dimension vào calculation |
| EXCLUDE | Loại trừ | Bỏ dimension khỏi calculation |
| IIF | Inline If | IF/ELSE ngắn gọn 1 dòng |
| RUNNING_SUM | Tổng tích lũy | Cumulative total từ đầu đến row hiện tại |
| RANK | Xếp hạng | Sắ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!
🔢 1. Basic Calculations
1.1 Tạo Calculated Field
1Right-click data pane → Create Calculated Field2HOẶC Analysis menu → Create Calculated Field1.2 Arithmetic
1// Profit Margin2[Profit] / [Sales]3 4// Revenue per Unit5[Sales] / [Quantity]6 7// Discount Impact8[Sales] * [Discount]9 10// Tax Amount (10%)11[Sales] * 0.11.3 String Functions
1// Full Name2[First Name] + " " + [Last Name]3 4// Uppercase5UPPER([Category])6 7// Extract first 3 characters8LEFT([Product ID], 3)9 10// Replace text11REPLACE([Status], "Pending", "Chờ xử lý")12 13// Contains check14CONTAINS([Product Name], "Premium")1.4 Date Functions
1// Year extract2YEAR([Order Date])3 4// Days between dates5DATEDIFF('day', [Order Date], [Ship Date])6 7// Add 30 days8DATEADD('day', 30, [Order Date])9 10// Current date11TODAY()12 13// Quarter14DATEPART('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!
🔀 2. Logic & Conditions
2.1 IF-THEN-ELSE
1IF [Sales] > 10000000 THEN "VIP"2ELSEIF [Sales] > 5000000 THEN "Premium"3ELSEIF [Sales] > 1000000 THEN "Standard"4ELSE "Basic"5END2.2 CASE Statement
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"6END2.3 IIF (Inline If)
1// Profit Flag2IIF([Profit] >= 0, "Có lãi", "Lỗ")3 4// On-time delivery5IIF([Ship Date] <= [Expected Date], "On Time", "Late")2.4 Logical Operators
1// AND + OR2IF [Region] = "South" AND [Sales] > 5000000 3 AND YEAR([Order Date]) = 20254THEN "Target Customer"5ELSE "Other"6ENDCheckpoint
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!
📊 3. Aggregates & Ratios
3.1 Common Aggregates
1SUM([Sales]) -- Total2AVG([Sales]) -- Average3COUNTD([Customer ID]) -- Count distinct4MAX([Sales]) -- Maximum5MIN([Sales]) -- Minimum3.2 Ratios & Percentages
1// Profit Margin %2SUM([Profit]) / SUM([Sales])3 4// Category Share of Total5SUM([Sales]) / TOTAL(SUM([Sales]))6 7// Running Total8RUNNING_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!
🎛️ 4. Parameters
4.1 Create Parameter
1Right-click → Create Parameter2- Name: "Select Metric"3- Data type: String4- Allowable values: List (Sales, Profit, Quantity)5- Current value: Sales4.2 Dynamic Metric Selector
1CASE [Select Metric]2 WHEN "Sales" THEN SUM([Sales])3 WHEN "Profit" THEN SUM([Profit])4 WHEN "Quantity" THEN SUM([Quantity])5END4.3 Top N Filter
1// Parameter: "Top N" (Integer, 1-20, default 10)2// Calculated Field:3INDEX() <= [Top N]4 5// Usage: Drag to Filters → True4.4 Date Range Parameter
1// Parameters: Start Date, End Date2// 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!
📈 5. Table Calculations
5.1 Quick Table Calculations
1Right-click measure → Quick Table Calculation:2- Running Total3- Difference4- Percent Difference5- Percent of Total6- Rank7- Moving Average5.2 Year-over-Year Growth
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
1// 3-Month Moving Average2WINDOW_AVG(SUM([Sales]), -2, 0)3 4// 7-Day Moving Average5WINDOW_AVG(SUM([Sales]), -6, 0)5.4 Rank
1RANK(SUM([Sales])) -- Standard rank2RANK_DENSE(SUM([Sales])) -- Dense rank (no gaps)3RANK(SUM([Sales]), 'desc') -- DescendingTable 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!
🎯 6. LOD Expressions
LOD expressions tính toán ở mức granularity khác với visualization.
6.1 FIXED
1// Customer's Total Sales (bất kể filter nào)2{ FIXED [Customer ID] : SUM([Sales]) }3 4// Category Average5{ FIXED [Category] : AVG([Profit]) }6.2 INCLUDE
1// Average of per-customer sales (more granular than viz)2AVG({ INCLUDE [Customer ID] : SUM([Sales]) })6.3 EXCLUDE
1// Sales regardless of sub-category2{ EXCLUDE [Sub-Category] : SUM([Sales]) }6.4 Common LOD Use Cases
| Use Case | LOD Expression |
|---|---|
| Customer first purchase | { FIXED [Customer] : MIN([Order Date]) } |
| Customer lifetime value | { FIXED [Customer] : SUM([Sales]) } |
| % of parent category | SUM([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!
🏋️ 7. Thực hành
Á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.
📋 Tổng kết
Kiến thức đã học
| Chủ đề | Nội dung chính | Tầm quan trọng |
|---|---|---|
| Basic Calcs | Arithmetic, String, Date functions | Foundation |
| Logic | IF/CASE/IIF, AND/OR | Segmentation |
| Aggregates | SUM, AVG, COUNTD, ratios | Metrics |
| Parameters | Dynamic controls, Top N, Date range | Interactivity |
| Table Calcs | Running total, YoY, Rank, Moving avg | Time analysis |
| LOD | FIXED, INCLUDE, EXCLUDE | Advanced analysis |
Câu hỏi tự kiểm tra
- Table Calculations khác Regular Calculations thế nào?
- LOD Expression: FIXED vs INCLUDE vs EXCLUDE?
- Parameters trong Tableau dùng để làm gì?
- 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ó!
