แผนการเรียนรู้
/

Week 11: The
Semantic Layer

A Shared Language between Business Users, Metrics, and AI

Project Milestone

Develop a Semantic Layer (dbt / Looker)

"Make every metric mean exactly the same thing to every consumer."

Learning Outcomes

1
Define the semantic layer and its role in modern data platforms
2
Model business logic and metrics in a reusable, consistent way
3
Explain how a semantic layer powers both BI dashboards and AI/LLM queries
4
Build a simple dbt Semantic Layer or LookML view for your project

ปัญหา: Metric Chaos

"ยอดขาย" หมายถึงอะไร? ฝ่ายการเงินคิดรวมภาษี ฝ่ายขายไม่รวมค่าส่ง ฝ่ายการตลาดนับเฉพาะช่องทางออนไลน์...

📊 แดชบอร์ดแต่ละทีมแสดง ตัวเลขต่างกัน สำหรับ "ตัวชี้วัดเดียวกัน"
🤖 โมเดล AI ให้คำตอบ ไม่ตรงกับผู้ใช้ธุรกิจ เพราะเข้าใจนิยามต่างกัน
🕒 ใช้เวลานานในการวิเคราะห์ ต้องเขียน SQL ซ้ำๆ เพื่อนิยามเดิม

What is a Semantic Layer?

เลเยอร์ความหมายคือชั้นที่เป็น ตัวกลางระหว่างข้อมูลดิบ (Gold Tables) กับ ผู้ใช้และ AI — แปลงโครงสร้างทางเทคนิคให้เป็นคำศัพท์ทางธุรกิจและเมตริกที่เข้าใจตรงกัน

Gold Tables
(สะอาด, จัดระเบียบ)
→
Semantic Layer
(นิยามเมตริก, มิติ, ดรอปฟิวดาวน์)
→
BI / AI / Apps
(ใช้ภาษาเดียวกัน)

Consistency

เมตริกถูกนิยามครั้งเดียว ใช้ซ้ำได้ทุกที่

Self-Service

ผู้ใช้ธุรกิจสร้างคำตอบเองโดยไม่ต้องรู้ SQL

AI-Ready

ให้บริบทแกโมเดล LLM เพื่อตอบคำถามได้แม่นยำ

Metrics Definition Language (MDL)

หัวใจของ Semantic Layer คือการนิยามเมตริกแบบ Declarative — อธิบาย "อะไร" ไม่ใช่ "อย่างไร"

ตัวอย่าง dbt Semantic Layer YAML:

metrics:
  - name: revenue
    type: sum
    label: "รายได้รวม"
    sql: total_amount
    time_grains: [day, week, month, year]
    filters:
      - field: is_refunded
        operator: '='
        value: 'false'
✅ แปลความหมาย: revenue = ผลรวมของ total_amount โดยไม่รวมเรกคอร์ดที่ถูกคืนเงิน (is_refunded = false) — ใช้คำนิยามนี้ได้ทั้ง BI และ AI

องค์ประกอบของ Semantic Layer

Metrics

ตัวชี้วัดที่คำนวณได้ เช่น ยอดขาย, กำไร, จำนวนคำสั่งซื้อ พร้อมสูตรและการกรอง

Dimensions

แอตทริบิวต์สำหรับแบ่งมุมมอง เช่น หมวดหมู่สินค้า, ภูมิภาค, เวลา

Entities

ตารางหรือ "แนวคิด" (เช่น Customer, Order) ที่เชื่อมโยงมิติและเมตริก

Relationships

ความสัมพันธ์ระหว่างเอนทิตี เช่น Customer 1-N Order

เครื่องมือ: dbt Semantic Layer, Looker LookML, Cube
Workshop (25 นาที)

นิยามเมตริกสำหรับแพลตฟอร์ม E-Commerce

สถานการณ์:

คุณมีตาราง Gold: fct_orders(amount, quantity, is_refunded, order_date, customer_key, product_key)

1. นิยามเมตริก รายได้สุทธิ (Net Revenue) ด้วยภาษา MDL
2. กำหนด Dimensions ที่ควรให้ผู้ใช้กรองหรือจัดกลุ่ม
3. อธิบายว่าทำไมการนิยามครั้งเดียวช่วยให้ AI ตอบได้ถูกต้อง

เฉลย: Net Revenue

1. MDL Definition

metrics:
  - name: net_revenue
    type: sum
    label: "รายได้สุทธิ"
    sql: amount
    filters:
      - field: is_refunded
        operator: '='
        value: 'false'

2 & 3. Dimensions & AI Benefit

  • 📅 Dimensions: order_date, customer (ภูมิภาค), product (หมวด)
  • 🤖 นิยามเดียว = AI แปล "รายได้สุทธิ" เป็น SQL ที่ถูกต้องตามนิยามเดียวกันกับ BI
  • ✅ ลดความขัดแย้งและข้อโต้แย้งของตัวเลขระหว่างแผนก
💡 แนวคิดหลัก: Semantic Layer กลายเป็น "Prompt" ที่ช่วยให้ LLM รู้จักเมตริก — AI จึงสร้างคำตอบที่สอดคล้องกับนโยบายขององค์กร

Semantic Layer + AI / LLM

เหตุผลที่ AI ต้องการ Semantic Layer เพื่อตอบคำถามธุรกิจอย่างน่าเชื่อถือ

Context Injection

ป้อนคำนิยามเมตริกและมิติให้ LLM รู้จักใน System Prompt (คล้าย RAG สำหรับเมตริก)

Schema Leaf-Reduction

แทนที่จะโหลด Schema หลายพันคอลัมน์ ให้ LLM ใช้เฉพาะเอนทิตีและเมตริกที่เกี่ยวข้อง

Verified SQL

Semantic Layer ช่วยให้ Text-to-SQL ตรงกับนิยามองค์กร ลดข้อผิดพลาดของ Join

🧠 คำศัพท์: รูปแบบนี้เรียกว่า Semantic-Enhanced Agentic RAG — ใช้ Semantic Layer เป็น "แหล่งความรู้" ที่ AI อ่านแล้วตอบกลับ

Part 2: Building a Semantic Layer

"Single source of metrics truth"

จากแนวคิดสู่การปฏิบัติ ด้วย dbt, Looker, และ MetricFlow

dbt Semantic Layer Workflow

1
Define Models: สร้าง marts เช่น dim_customer, fct_orders ใน Gold
2
Configure Semantic Models: ประกาศ entities, dimensions, measures
3
Define Metrics: ประกาศสูตรการคำนวณครั้งเดียว (MDL)
4
Query via API: BI (Tableau/Looker) และ AI (GraphQL API) ใช้เมตริกเดียวกัน
อ่านเพิ่มเติม: dbt Semantic Layer Documentation

Looker LookML Basics

LookML เป็นภาษาแบบ Declarative ที่อธิบาย View และ Explore สำหรับ BI

view: order_facts {
  dimension: order_date { type: date }
  dimension: customer_region
  measure: net_revenue {
    type: sum
    sql: ${TABLE}.amount ;;
  }
}

explore: order_facts {
  join: customer { relationship: many_to_one }
}
✅ explore = เรื่องที่ผู้ใช้ธุรกิจเล่าได้, measure = เมตริกที่คำนวณ
กิจกรรมกลุ่ม (20 นาที)

ออกแบบ Semantic Layer สำหรับโครงการ

ภารกิจ:

สำหรับ "Agentic E-Commerce Platform" ของคุณ จงนิยาม:

1. 2–3 เมตริกหลัก (เช่น รายได้, กำไร, จำนวนคำสั่งซื้อ)
2. Dimensions ที่สำคัญและลำดับชั้น
3. คำถามธรรมชาติ 2 ข้อที่ AI ควรตอบได้จาก Semantic Layer นี้

ตัวอย่างคำตอบ

เมตริก

  • • gross_revenue = SUM(amount)
  • • net_revenue = ไม่รวม refunded
  • • order_count = COUNT(order_id)
  • • aov = net_revenue / order_count

Dimensions & AI Questions

  • • เวลา: ปี > ไตรมาส > เดือน
  • • ลูกค้า: ภูมิภาค, ระดับสมาชิก
  • • สินค้า: หมวดหมู่ > ยี่ห้อ
  • • ❓ "ยอดขายรายเดือนของภาคใต้คือ?"
  • • ❓ "สินค้าหมวดไหนกำไรสูงสุดใน Q3?"
🚀 โครงการของคุณในสัปดาห์นี้: สร้าง Semantic Layer (dbt หรือ Looker) พร้อมให้ AI ตอบโดยใช้คำนิยามเดียวกัน

Week 11 Summary

1. Semantic Layer แก้ปัญหา "Metric Chaos" โดยนิยามเมตริกครั้งเดียว
2. ใช้ภาษา Declarative (dbt MDL / LookML) อธิบายสิ่งที่เป็นธุรกิจ
3. สำคัญสำหรับ AI — ให้บริบทและนิยามที่ทำให้ LLM ตอบได้ตรงจริง
4. BI และ AI ใช้เลเยอร์เดียวกัน = "ทุกคนเห็นตัวเลขเดียวกัน"
Next Week: OLAP — slicing, dicing & drilling into your data!