Skip to content

Conversation

@AndyMoreland
Copy link
Contributor

No description provided.

Comment on lines +119 to +136
@offline(tags="batch-most-recent-revenue")
def batch_most_recent_revenue(
records: DataFrame[RApplication.id, RApplication.records, Now]
) -> DataFrame[RApplication.id, RApplication.most_recent_revenue]:
df: pl.LazyFrame = records.to_polars()

exploded = df.explode(pl.col(str(RApplication.records))).unnest(str(RApplication.records))

time_consistent = exploded.filter(pl.col(str(RQuickbooksRecord.created_at)) <= pl.col(str(Now)))

most_recent_rev_output = (
time_consistent.sort(by=str(RQuickbooksRecord.created_at), descending=True)
.groupby(str(RApplication.id), str(Now), maintain_order=True)
.agg(pl.first(str(RQuickbooksRecord.revenue)).alias(str(RApplication.most_recent_revenue)))
.select(str(RApplication.id), str(RApplication.most_recent_revenue))
)

return most_recent_rev_output
Copy link

Choose a reason for hiding this comment

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

The output DataFrame should include all input application IDs, even when no records exist for an application. Consider using a right join with the input IDs and defaulting most_recent_revenue to 0 for applications without records. This ensures the output maintains the same cardinality as the input.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants