Sub topic 7
Calculating ratios with DIVIDE
This chapter covers how to build a ratio measure with DIVIDE, why it is safer than the / operator, and the mistakes that most often break ratio measures. It also covers the optional alternate result, which the next chapter treats in more depth.
Prerequisites
This chapter assumes you can do the following:
- Navigate Power BI Desktop: Report view, the Fields pane, and visuals.
- Create a measure from the formula bar.
- Write the
SUMandCOUNTROWSmeasures from the earlier chapters. - Explain filter context at a basic level, meaning that a visual or slicer changes which rows a measure sees.
The examples use a Sales table with a numeric SalesAmount column. Each row is one order line.
Why ratios need their own function
Many useful business numbers are ratios. Average sale per order line is total sales divided by the number of order lines. Profit margin is profit divided by revenue. Return rate is returned items divided by items sold.
You could write a ratio with the division operator /. That works until the denominator is zero. In DAX, 10 / 0 returns an error value, which is infinity in a measure. A filter that leaves no rows can produce the same problem. Imagine a slicer that selects a region with no sales. Then COUNTROWS(Sales) is 0, and any ratio that divides by it fails.
DIVIDE handles this case for you. It checks the denominator before dividing. If the denominator is zero or blank, it returns an alternate result instead of an error. The default alternate result is BLANK.
The DIVIDE syntax
DIVIDE(<numerator>, <denominator> [, <alternateResult>])numeratoris the value to divide.denominatoris the value to divide by.alternateResultis optional. It is returned when the denominator is zero or blank. If you leave it out, DAX returnsBLANK.
Both the numerator and the denominator can be any expression that returns a single number. That includes a column aggregation, a number, or another measure.
Building the first ratio
Start from two measures you already have. The first totals the sales amount in the current filter context.
Total Sales = SUM(Sales[SalesAmount])The second counts the rows of Sales in the current filter context. If each row is one order line, it counts order lines.
Order Lines = COUNTROWS(Sales)Now combine them. Create a new measure and enter this formula:
Average Sales per Order Line = DIVIDE([Total Sales], [Order Lines])This formula refers to the two measures by name, in square brackets. It does not repeat their logic. The measure reads like the sentence it implements: total sales divided by order lines.
Here is how the formula works when you place it in a visual:
- The visual and any slicers set the filter context.
[Total Sales]is evaluated in that context and returns a number.[Order Lines]is evaluated in the same context and returns a count.DIVIDEchecks whether the count is zero or blank. If it is, the result isBLANK. Otherwise it returns the quotient.
Why the measure changes with the filters
The ratio is not a stored number. Power BI recalculates it for every cell of every visual. Consider a table visual with one row per product category:
| Category | Total Sales | Order Lines | Average Sales per Order Line |
|---|---|---|---|
| Bikes | 90,000 | 300 | 300 |
| Accessories | 12,000 | 240 | 50 |
| Clothing | 8,000 | 80 | 100 |
Each row has its own filter context, so each row gets its own numerator and denominator. The total row of the visual has no category filter, so it divides the grand totals. In this example that is 110,000 divided by 620, which is about 177.4.
Notice that 177.4 is not the average of 300, 50, and 100. A ratio of totals is different from an average of ratios. This is the correct behavior for a measure. It is also the reason you should write the ratio as a measure and not average a column of precomputed ratios.
Why DIVIDE is preferred over the / operator
Compare the two forms:
| Aspect | `[Total Sales] / [Order Lines]` | `DIVIDE([Total Sales], [Order Lines])` |
|---|---|---|
| Zero denominator | Returns an error value | Returns `BLANK`, or your alternate result |
| Blank denominator | Treated as zero, so it errors | Returns the alternate result |
| Intent | Division only | Division with a built-in safety check |
| Visual behavior | Error values can show as infinity or break a card | Empty cells are usually hidden or left blank |
Blank results in a visual are often a good thing. When a slicer selects a category with no sales, the visual typically omits the empty row or leaves the cell empty. An error value does not behave this way.
You may see advice to guard / with an IF test. That works, but it is longer and repeats the denominator expression:
Average Sales per Order Line (IF) =
IF(
[Order Lines] = 0,
BLANK(),
[Total Sales] / [Order Lines]
)DIVIDE expresses the same logic in one call. Use it unless you have a specific reason to write the test by hand.
The alternate result
The optional third argument lets you choose what appears when the denominator is zero or blank.
Average Sales per Order Line (0 if none) = DIVIDE([Total Sales], [Order Lines], 0)With this version, a filter that leaves no order lines shows 0 instead of an empty cell. Whether that is better depends on the report:
- Choose
BLANK(the default) when "no data" is different from "zero". An empty cell tells the reader there is nothing to divide. - Choose
0when a number must always appear, for example a card that feeds a chart axis or a downstream calculation.
A subtle effect follows from this choice. A measure that returns 0 for every combination of filters can cause Power BI to display rows that would otherwise be hidden. Power BI hides rows where all measures in a visual are blank. If your measure returns 0, those rows now appear. A report with many categories can become long and cluttered. Keep that in mind before you set the alternate result to 0 on a measure used in a large table.
The alternate result does not have to be a number. It can be any scalar value, but a measure should return a consistent data type. Mixing a number and text in one measure makes formatting hard. Keep the alternate result numeric.
Referencing measures from other measures
The ratio depends on [Total Sales] and [Order Lines]. This pattern is worth adopting for three reasons:
- One definition. If the definition of total sales changes, you edit one measure. Every ratio that refers to it updates.
- Readability. The formula shows what it computes without the reader opening other formulas.
- Testing. You can place
Total SalesandOrder Linesin a table next to the ratio and check each part separately.
You can also write the ratio without helper measures:
Average Sales per Order Line (inline) =
DIVIDE(SUM(Sales[SalesAmount]), COUNTROWS(Sales))This returns the same results. The version with measure references is easier to maintain once the model has more than a few measures.
A ratio using a filtered count
The denominator, or the numerator, can come from COUNTROWS over a filtered table. This counts only the Sales rows where SalesAmount is greater than 1000.
Large Order Lines = COUNTROWS(FILTER(Sales, Sales[SalesAmount] > 1000))You can use this measure as the numerator of a share:
Large Order Line Share = DIVIDE([Large Order Lines], [Order Lines])The result is a fraction between 0 and 1, for example 0.18. Format the measure as a percentage in the Measure tools ribbon so it displays as 18%.
FILTER runs inside the current filter context. If a slicer already limits the data to one region, Large Order Lines counts only the large lines in that region. The denominator is limited to the same region. The ratio is therefore consistent without extra work.
Formatting a ratio measure
A ratio measure returns a number with many decimal places unless you format it. Select the measure in the Fields pane, then use the Measure tools ribbon:
- Set the format to Currency for average sales per order line.
- Set the format to Percentage for shares such as
Large Order Line Share. - Set the decimal places so the number matches how the reader uses it.
Name ratio measures after what they are, such as Average Sales per Order Line and not Measure 3. When the measure returns a share, include the word "Share" or "Rate" or "%" in the name so it is not confused with a count.
Common errors
Wrong order of arguments. DIVIDE([Order Lines], [Total Sales]) runs without an error and returns a plausible number. It is the inverse of the one you wanted. Read the formula aloud as "numerator divided by denominator" before you save it.
Dividing averages. Do not compute a ratio from two averages. Use totals in both places, then divide. Averages hide the row counts that give each group its weight.
Unexpected blanks. If the ratio shows blank everywhere, check that both inputs return values in the same filter context. A denominator that is blank because of a missing relationship produces blank results.
Ratio that does not add up. The total row of a ratio is not the sum of the rows above it. Add ratios and you get a meaningless number. Ratios should not be summed. Let the visual compute the total row from the measure.
Using 0 as the alternate result and hiding real gaps. A report that shows 0 for "no data" can mislead readers into thinking the business ran at zero. Use 0 only when that is the real meaning.
Hardcoding the numerator in a column. Putting a ratio in a calculated column fixes it at the row level. It does not respond to slicers or to the grain of a visual. Use a measure for ratios.
Testing a ratio measure
Follow these steps after you create a ratio.
- Add a table visual to a report page.
- Add a dimension field, such as
Category, as rows. - Add
Total Sales,Order Lines, and the ratio measure. - Pick one row and divide the two numbers by hand. Confirm the ratio matches.
- Add a slicer for a field that can produce an empty selection, such as a year with no sales. Confirm the ratio is blank or your alternate result, and not an error.
- Check the total row. It should divide the grand totals.
This takes a few minutes and catches wrong arguments, wrong filters, and misleading blanks before the report is shared.
Summary of the pattern
- Write the numerator and denominator as separate measures.
- Combine them with
DIVIDE(numerator, denominator). - Add a third argument only when "no data" should display as a specific value.
- Format the result as currency or percentage.
- Test with a slicer that empties the data.
Exercise
A slicer selects the year 2031, and the Sales table has no rows for that year. A card shows [Total Sales] / [Order Lines] and displays an error. How do you fix the measure so the card shows an empty value, and what changes if you pass 0 as the third argument?
Show answer
Rewrite the measure as DIVIDE([Total Sales], [Order Lines]). The denominator [Order Lines] is blank or zero for 2031, so DIVIDE returns BLANK and the card shows no value. With DIVIDE([Total Sales], [Order Lines], 0), the card shows 0 instead. Use 0 only if "no data" should be read as zero.
Exercise
Write a measure named Large Order Line Share that returns the share of order lines with a SalesAmount greater than 1000. Use Large Order Lines and Order Lines. Then say how the total row of a table visual is calculated for this measure.
Show answer
Large Order Line Share = DIVIDE([Large Order Lines], [Order Lines]). The total row is not the sum or average of the rows above it. It evaluates the measure with no category filter, so it divides the total count of large order lines by the total count of order lines.
Exercise
Your teammate computes average sales per order line by averaging the ratio column of each category. The numbers differ from your DIVIDE([Total Sales], [Order Lines]) measure in the total row. Which is correct, and why?
Show answer
The DIVIDE measure is correct. It divides the grand total of sales by the grand total of order lines, so each order line has equal weight. Averaging category ratios gives a small category the same weight as a large one, which misstates the overall average.