Skip to content

Guard monthly cost change against a zero prior month - #2324

Closed
Roland Krummenacher (RolandKrummenacher) wants to merge 1 commit into
microsoft:devfrom
RolandKrummenacher:fix/monthly-cost-change-divide-by-zero
Closed

Roland Krummenacher (RolandKrummenacher) wants to merge 1 commit into
microsoft:devfrom
RolandKrummenacher:fix/monthly-cost-change-divide-by-zero

Conversation

@RolandKrummenacher

Copy link
Copy Markdown
Collaborator

Fixes #2323.

🛠️ Changes

monthly-cost-change-percentage.kql guards its division with isempty(PreviousBilledCost). That covers the null prev() returns on the first row, but isempty() is false for 0.0, so a prior month of exactly zero falls through — and KQL evaluates x / 0.0 as inf rather than raising, so the infinity is returned as the percentage.

Both guards now also test for zero:

-    BilledCostChangePct = iff(isempty(PreviousBilledCost), 0.0, ...),
+    BilledCostChangePct = iff(isempty(PreviousBilledCost) or PreviousBilledCost == 0.0, 0.0, ...),

The == 0.0 form matches the convention already used in service-price-benchmarking.kql.

A zero prior month is reachable rather than theoretical: BilledCost is zero on usage rows covered by a commitment discount, since the charge was billed at purchase time and the amortized amount lands in EffectiveCost. A scope whose month is entirely commitment-covered therefore sums BilledCost to exactly zero.

✅ Verification

Run against the pipeline's shape, so no hub or ingestion is involved:

datatable(ChargePeriodStart:datetime, BilledCost:real)
[
    datetime(2025-01-01), 100.0,
    datetime(2025-02-01),   0.0,
    datetime(2025-03-01),  50.0,
]
| order by ChargePeriodStart asc
| extend PreviousBilledCost = prev(BilledCost)
| project ChargePeriodStart,
    Before = tostring(iff(isempty(PreviousBilledCost), 0.0, toreal((BilledCost - PreviousBilledCost) * 100.0 / PreviousBilledCost))),
    After  = tostring(iff(isempty(PreviousBilledCost) or PreviousBilledCost == 0.0, 0.0, toreal((BilledCost - PreviousBilledCost) * 100.0 / PreviousBilledCost)))
ChargePeriodStart Before After
2025-01-01 0 0
2025-02-01 -100 -100
2025-03-01 inf 0

Rows with a non-zero prior month are unaffected, so the change is limited to the case that was previously returning an infinity.

📝 Notes

Deliberately out of scope: returning 0.0 when there is no prior month conflates "no change" with "nothing to compare against". real(null) would separate them, but that is a behaviour change for existing consumers, so this PR only closes the divide-by-zero hole. Happy to follow up if you would prefer the stricter version.

🤖 Generated with Claude Code

isempty() is false for 0.0, so the existing guard only covers the first row,
where prev() returns null. A month whose prior month summed to exactly zero
falls through and KQL evaluates x / 0.0 to inf rather than raising an error,
so the result reaches the caller as an infinity.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The zero-denominator cases are addressed without affecting existing behavior.

Pull request overview

Fixes infinite monthly cost-change percentages when the previous month’s cost is zero.

Changes:

  • Adds zero-value guards for billed and effective cost calculations.
  • Preserves existing behavior for null and nonzero prior values.
File summaries
File Description
src/queries/catalog/monthly-cost-change-percentage.kql Prevents division by zero in monthly cost percentage calculations.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@RolandKrummenacher

Copy link
Copy Markdown
Collaborator Author

Superseded by #2327 — same commit, but on a branch in this repo rather than my fork, to match the convention used by the other PRs here and so CI and maintainer pushes work normally. Closing this one to avoid a duplicate review.

@microsoft-github-policy-service

Copy link
Copy Markdown

@allcontributors

Copy link
Copy Markdown
Contributor

@microsoft-github-policy-service[bot]

Roland Krummenacher (@RolandKrummenacher) already contributed before to code

This branch is waiting to be deployed

1 waiting deployment
ftk-fork — 8f05194f Waiting Sep 16, 2026 by RolandKrummenacher via Clean up PR resources #60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs: Review 👀 PR that is ready to be reviewed Skill: KQL KQL queries and Data Explorer integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

monthly-cost-change-percentage returns inf when the prior month is zero

5 participants