Counting Functions: COUNT, COUNTA & COUNTBLANK
Overview & Learning Objectives
Division is the heart of KPIs: profit margins, discount percentages, click-through rates, and ratios. However, dividing numbers in analytics carries a lethal risk: Division by Zero. In standard math, dividing by zero crashes reports with "Infinity" or "NaN". The DAX `DIVIDE()` function executes "safe division"—it intercepts division by zero and cleanly returns a blank or an optional fallback value.
- Master the syntax and execution flow of the DAX `DIVIDE()` function.
- Understand why using the forward slash (`/`) operator is an enterprise anti-pattern.
- Supply optional alternative results (e.g. `0` or `"N/A"`) when denominator is zero.
Industry Standard
Standard: Enterprise coding standards at Microsoft, Amazon, and Accenture strictly require all division calculations to use `DIVIDE(Numerator, Denominator)` instead of the `/` operator.
Real-World Example: FreshBake Artisan Patisserie
FreshBake computes Profit Margin % across outlets. Newly opened pop-up kiosks have ₹0 revenue this week.
| Outlet Name | Total Profit (₹) | Total Revenue (₹) | Forward Slash (/) Result | DIVIDE() Result |
|---|---|---|---|---|
| Indiranagar Main | 45,000 | 1,50,000 | 0.30 (30%) | 0.30 (30%) |
| Airport Kiosk | 12,000 | 40,000 | 0.30 (30%) | 0.30 (30%) |
| Metro Pop-Up (New) | -2,500 | 0 | Infinity / Error (Crash!) | BLANK() (Clean visual) |
Syntax & Core Workflow
Profit Margin % = DIVIDE([Total Profit], [Total Revenue], 0)
Parameters Breakdown
Numerator: The dividend value or measure (e.g. `[Total Profit]`).Denominator: The divisor value or measure (e.g. `[Total Revenue]`).AlternateResult (Optional): The fallback value returned if denominator is zero or blank. If omitted, returns `BLANK()`.
Sample Walkthrough (Q&A)
Question: Why is returning BLANK() by default better than returning 0 when calculating Profit Margin % in Power BI?
Formula / Action: DIVIDE([Total Profit], [Total Revenue]) with no 3rd argument returns BLANK().
Result: Visuals automatically hide categories with no activity.
Explanation: If DIVIDE returns 0 for a store that has not launched yet, Power BI treats it as an active store and renders it on charts. Returning BLANK() allows Power BI visuals to automatically suppress empty categories.
Common Mistakes & Limitations
- Using `[Sales] / [Target]` which produces ugly `#ERROR` or `Infinity` cards when targets are zero or unassigned.
- Writing bloated `IF([Revenue] = 0, BLANK(), [Profit] / [Revenue])` statements when `DIVIDE()` is 2x faster and cleaner.
Keyboard Shortcuts
| Action | Windows | Mac |
|---|---|---|
| Format as Percentage | Click Measure -> Measure Tools -> Click "%" icon | Measure Tools -> % |
| Set Decimal Places | Measure Tools -> Decimals box -> Type 1 | Measure Tools -> Decimals |
Pro Tips & Modern Alternatives
- Omit the third parameter in `DIVIDE()` to return `BLANK()` when you want sparse visual tables without ghost zero rows.
- Use the third parameter `0` when calculating metrics for financial balance sheets where explicit zeros are required.