Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions models/marts/fact_bookings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
with bookings as (

select
Comment thread
EmilyYoung26 marked this conversation as resolved.
booking_id,
member_id,
class_id,
booking_timestamp,
attendance_status,
check_in_timestamp,
cancellation_timestamp
from {{ ref('stg_bookings') }}

),

final as (

select
booking_id,
member_id,
class_id,

booking_timestamp,
date(booking_timestamp) as booking_date,

attendance_status,

case when attendance_status = 'attended' then true else false end as is_attended,
case when attendance_status = 'cancelled' then true else false end as is_cancelled,
case when attendance_status = 'no_show' then true else false end as is_no_show,

check_in_timestamp,
cancellation_timestamp

from bookings

)

select *
from final
95 changes: 95 additions & 0 deletions models/marts/fact_bookings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
version: 2

models:
- name: fact_bookings
description: Final fact table for class bookings with one row per booking.

columns:
- name: booking_id
description: Unique identifier for each booking.
tests:
- not_null
- unique

- name: member_id
description: Identifier for the member who made the booking.
tests:
- not_null
- relationships:
arguments:
to: ref('dim_members')
field: member_id

- name: class_id
description: Identifier for the class that was booked.
tests:
- not_null

- name: booking_timestamp
description: Timestamp of when the booking was made.
tests:
- not_null

- name: booking_date
description: Date when the booking was made.
tests:
- not_null

- name: attendance_status
description: Cleaned attendance status for the booking.
tests:
- not_null
- accepted_values:
arguments:
values: ['attended', 'cancelled', 'no_show']

- name: is_attended
description: Flag indicating whether the booking was attended.
tests:
- not_null
- accepted_values:
arguments:
values: [true, false]
quote: false

- name: is_cancelled
description: Flag indicating whether the booking was cancelled.
tests:
- not_null
- accepted_values:
arguments:
values: [true, false]
quote: false

- name: is_no_show
description: Flag indicating whether the booking was a no-show.
tests:
- not_null
- accepted_values:
arguments:
values: [true, false]
quote: false

- name: check_in_timestamp
description: Timestamp when the member checked in, if they attended.

- name: cancellation_timestamp
description: Timestamp when the booking was cancelled, if cancelled.

unit_tests:
- name: test_fact_bookings_attendance_flags
description: Test that the attendance_status is consistent with the attendance flags.
model: fact_bookings

given:
- input: ref('stg_bookings')
rows:
- {booking_id: 1, member_id: 101, class_id: 1001, booking_timestamp: '2025-01-10 09:00:00', attendance_status: 'attended', check_in_timestamp: '2025-01-10 09:55:00', cancellation_timestamp: null}
- {booking_id: 2, member_id: 102, class_id: 1002, booking_timestamp: '2025-01-11 10:00:00', attendance_status: 'cancelled', check_in_timestamp: null, cancellation_timestamp: '2025-01-10 15:00:00'}
- {booking_id: 3, member_id: 103, class_id: 1003, booking_timestamp: '2025-01-12 11:00:00', attendance_status: 'no_show', check_in_timestamp: null, cancellation_timestamp: null}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add one more test case where attendance_status is just 'null' to begin with, just to check it is handled correctly, otherwise this all looks good :)

expect:
rows:
- {booking_id: 1, member_id: 101, class_id: 1001, booking_timestamp: '2025-01-10 09:00:00', booking_date: '2025-01-10', attendance_status: 'attended', is_attended: true, is_cancelled: false, is_no_show: false, check_in_timestamp: '2025-01-10 09:55:00', cancellation_timestamp: null}
- {booking_id: 2, member_id: 102, class_id: 1002, booking_timestamp: '2025-01-11 10:00:00', booking_date: '2025-01-11', attendance_status: 'cancelled', is_attended: false, is_cancelled: true, is_no_show: false, check_in_timestamp: null, cancellation_timestamp: '2025-01-10 15:00:00'}
- {booking_id: 3, member_id: 103, class_id: 1003, booking_timestamp: '2025-01-12 11:00:00', booking_date: '2025-01-12', attendance_status: 'no_show', is_attended: false, is_cancelled: false, is_no_show: true, check_in_timestamp: null, cancellation_timestamp: null}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
select *
from {{ ref('fact_bookings') }}
where
(attendance_status = 'attended' and (is_attended = false or is_cancelled = true or is_no_show = true))
or
(attendance_status = 'cancelled' and (is_attended = true or is_cancelled = false or is_no_show = true))
or
(attendance_status = 'no_show' and (is_attended = true or is_cancelled = true or is_no_show = false))

Loading