dbt hub

dbt test: expect_column_pair_values_A_to_be_greater_than_B

USE CASE

Columns relationship
Values range

APPLIES TO

Model
Seed
Source

This page discusses the expect_column_pair_values_A_to_be_greater_than_B test from the dbt-expectations package. This test verifies that values in one column (Column A) are greater than or equal to the values in another column (Column B). It's particularly useful for ensuring data consistency and logical relationships between two numeric columns.

How it Works

The expect_column_pair_values_A_to_be_greater_than_B test compares two columns within a model, seed, or source to confirm that each entry in Column A is greater than or equal to the corresponding entry in Column B.

Steps and Conditions:

  1. Column Selection: Select Column A and Column B for comparison.
  2. Comparison Rule Setup: Define whether the values in Column A should be strictly greater than (or optionally equal to) the values in Column B using the or_equal option.
  3. Optional Configuration:
    • Row Condition: Specify a condition under row_condition to filter which rows are included in the test. This helps in focusing the comparison on relevant data subsets.
  4. Execution: The test performs the comparison for each row where the conditions are met. If Column A's value in a row is not greater than (or equal, if specified) Column B's value, the test flags this row.
  5. Outcome:
    • Pass: If all compared values from Column A are greater than or equal to those from Column B, the test passes, validating the logical consistency between the columns.
    • Fail: If any value in Column A is less than the corresponding value in Column B, the test fails. This indicates a potential issue or anomaly where the expected relationship between the columns does not hold.

Example Usage: E-commerce

In an E-commerce platform, maintaining accuracy in pricing data, such as comparing discounted prices against original prices, is crucial.

Consider a scenario where the product_pricing table contains two columns: original_price and discounted_price. It's essential that the discounted price should never exceed the original price.


tests:
  - dbt_expectations.expect_column_pair_values_A_to_be_greater_than_B:
      column_A: original_price
      column_B: discounted_price
      or_equal: True

In this example, the test checks that for every product listing, the original_price is not lower than the discounted_price, ensuring pricing logic is correctly applied across the product catalog. This test helps prevent pricing errors that could negatively impact sales and customer trust.

The only data observability platform built into your dbt code

  • Get monitors on your production tables out-of-the-box with zero configuration
  • Add tests to your code in bulk with a simple UI
  • Track test results over time
  • Set owners and create meaningful alerts
  • Triage incidents faster using our end-to-end column-level lineage graph