Merged PR 4734: Bugfix - Ensures accounts cannot be duplicated in Xero sales monthly trends

# Description

This fixes the duplication error when the same deal has more than one contact. Impact was not massive, it just affected because of the Home to Host account and this has happened during the past week. There were other cases that contained duplicates but these were not invoiced so it didn't affect the total sum.

Changes:
* Contacts now has a boolean field indicating if a deal exists.
* Contacts now has a boolean field indicating if that contact has a deal informed and it's the last updated one. This is opinionated - it could have been done with creation, for instance.
* Sales monthly trends forces the last updated contact to be true.
* Improved robustness by adding tests.

Note that modifying the logic in intermediate on `int_xero__sales_monthly_trends` by changing `id_deal` per `id_contact` would not have worked - we need to do this compute by deal to ensure that any invoicing is reported MoM and YTD.

# Checklist

- [X] The edited models and dependants run properly with production data.
- [X] The edited models are sufficiently documented.
- [X] The edited models contain PK tests, and I've ran and passed them.
- [X] I have checked for DRY opportunities with other models and docs.
- [X] I've picked the right materialization for the affected models.

# Other

- [ ] Check if a full-refresh is required after this PR is merged.

Related work items: #28576
This commit is contained in:
Oriol Roqué Paniagua 2025-03-19 08:19:20 +00:00
parent c00d02685d
commit 5928a198b6
4 changed files with 133 additions and 46 deletions

View file

@ -1460,6 +1460,15 @@ models:
fiscal year is labeled by the year in which it ends (e.g., FY 2025
represents April 2024 March 2025).
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- document_issued_month_utc
- document_status
- document_type
- id_deal
- accounting_financial_l3_aggregation
columns:
- name: document_issued_month_utc
data_type: date
@ -1507,6 +1516,8 @@ models:
- name: id_deal_contact_name
data_type: text
description: "A concatenation of the deal ID and contact name."
data_tests:
- not_null
- name: accounting_financial_l1_aggregation
data_type: text

View file

@ -11,7 +11,11 @@ select
smt.document_type as document_type,
smt.id_deal as id_deal,
c.contact_name as contact_name,
smt.id_deal || '-' || c.contact_name as id_deal_contact_name,
case
when c.contact_name is null
then smt.id_deal
else smt.id_deal || '-' || c.contact_name
end as id_deal_contact_name,
smt.accounting_financial_l1_aggregation as accounting_financial_l1_aggregation,
smt.accounting_financial_l2_aggregation as accounting_financial_l2_aggregation,
smt.accounting_financial_l3_aggregation as accounting_financial_l3_aggregation,
@ -22,4 +26,7 @@ select
smt.previous_year_ytd_amount_wo_taxes_in_gbp
as previous_year_ytd_amount_wo_taxes_in_gbp
from int_xero__sales_monthly_trends smt
left join int_xero__contacts c on smt.id_deal = c.id_deal
left join
int_xero__contacts c
on smt.id_deal = c.id_deal
and c.is_latest_updated_contact_per_deal = true