Sub topic 13
Setting Data Type and Format for Results
This chapter assumes you can already write basic visual calculations such as RUNNINGSUM, PREVIOUS, and COLLAPSE. It covers how Power BI picks the data type and display format of a calculation's result, how to change both, and the mistakes that make results look wrong even when the DAX is correct.
Data type and format are separate settings
A visual calculation returns a value. Two settings control how that value is treated.
- The data type is what kind of value the result is, such as whole number, decimal number, text, or true/false. It affects how the value is stored, sorted, and combined with other values.
- The format is how a number is displayed. A decimal value of
0.2537can appear as0.25,25.37%, or$0.25. The format changes the display only. It does not change the stored value.
Keep the two apart. Setting a percentage format on a result does not multiply it by 100 in the DAX. The value stays 0.2537, and the format makes it show as 25.37%.
What Power BI chooses by default
When you create a visual calculation, Power BI infers a data type from the expression. In many cases it also carries over the format of the field you used. For example, a running sum of a currency-formatted measure usually keeps the currency format.
This inheritance breaks down when the expression changes what the number means. These are the common cases:
| Expression | What it produces | Typical default problem |
|---|---|---|
| `RUNNINGSUM([Sales Amount])` | Same unit as the input | Usually fine |
| `MOVINGAVERAGE([Sales Amount], 3)` | Same unit, often fractional | May show more or fewer decimals than you want |
| `[Sales Amount] - PREVIOUS([Sales Amount])` | Same unit as the input | Usually fine |
| `DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))` | A ratio between 0 and 1 | Shows as `0.25` instead of `25%` |
The last row is the one you will fix most often. A ratio inherits nothing sensible from a currency field, so you set the format yourself.
Changing the data type and format
You change both settings in the visual calculations edit mode.
- Select the visual, then choose New calculation on the ribbon, or open an existing calculation to enter edit mode.
- Select the calculation field in the visual preview or in the field list of the edit mode.
- Open the Format pane of the edit mode and find the data format options for the calculation. Menu labels can differ slightly between Power BI Desktop versions.
- Choose a data type if the automatic choice is wrong.
- Choose a format, such as percentage, currency, or decimal, and set the number of decimal places.
The setting belongs to that calculation on that visual. If you copy the visual, the calculation and its format come along. If you build the same calculation on another visual, you set the format again there.
Worked example: percent of parent
Start with a matrix that has Category and Product on rows and Sales Amount as a value. Add this visual calculation:
Percent of parent = DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS))Without changes, a product that makes up a quarter of its category shows as 0.25. Readers may not realize this is a share, and values such as 0.0731 are hard to scan.
Select the calculation and apply these settings:
- Data type: Decimal number
- Format: Percentage
- Decimal places: 1
The same cell now reads 25.0%, and 0.0731 reads 7.3%. The stored value is still 0.25 and 0.0731. If you later sort the visual by this calculation, the sort uses the stored values, so the order is unchanged.
The same fix applies to the grand total version:
Percent of grand total = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS))Set it to a percentage with the same number of decimal places as the parent calculation. If both columns appear in one matrix, matching decimals make them easier to compare.
Choosing decimal places
Decimal places are a display choice. They are not rounding. A result of 1234.5678 shown with zero decimals reads 1,235, but the underlying value keeps its precision.
Use these guidelines:
- Currency running sums: zero or two decimals, to match the underlying measure.
- Moving averages: one more decimal than the source, because averages of whole numbers are often fractional.
- Differences from
PREVIOUS: the same decimals as the source, so the change lines up with the values it compares. - Percentages: one decimal is a good default. Use zero when the shares are large and few, and two when you compare small shares.
If the visual's total row mixes calculations with different decimals, the table looks uneven. Settle on one convention per visual.
Gotchas
Do not use FORMAT to build the display
You can write a visual calculation that returns formatted text:
Percent label = FORMAT(DIVIDE([Sales Amount], COLLAPSE([Sales Amount], ROWS)), "0.0%")This returns text, so the data type becomes text. The result sorts alphabetically ("10.0%" before "9.0%"), and you cannot use it in further numeric calculations or as a numeric axis. Return a number and set the format in the Format pane instead. Use FORMAT only when you want a text label on purpose, for example inside a longer string.
A wrong data type can hide the problem
If a calculation mixes types, for example a numeric result in one branch and text in another, the type may come out as text. Numbers then left-align and sort incorrectly. Check the data type setting first when a numeric column looks like labels.
Blank results are not zeros
PREVIOUS returns blank on the first row of its axis, and DIVIDE returns blank when the denominator is zero or blank. Formatting does not change this. A blank cell stays blank rather than showing 0%. If you need a zero, handle it in the expression with COALESCE or by supplying an alternate result to DIVIDE. Do not expect a number format to do it.
Change vs previous = [Sales Amount] - COALESCE(PREVIOUS([Sales Amount]), [Sales Amount])This version returns a change of zero on the first row instead of subtracting a blank. Decide whether that is the meaning you want. In some reports a blank is more honest than a zero.
Formatting does not follow the model
A format you set on a visual calculation does not become a property of any model field. Other visuals, and other report authors using the dataset, will not see it. If the same percentage or currency format is needed across many visuals, a measure with a format string set in the model is easier to keep consistent. This is one of the trade-offs covered in the limitations chapter.
Totals and subtotals use the same format
The format applies to every cell of the calculation, including subtotal and grand total rows. For a percent of parent, the grand total row shows its own computed value, which is often 100% or blank, in the same format as the rest. You cannot give subtotals a different format from detail rows through this setting alone.
Checking your result
After you set a format, verify it:
- Compare a few displayed cells with a hand calculation, for example
25%against250 / 1000. - Sort by the calculation and confirm the order is numeric, not alphabetical.
- Hover over a cell in a chart that uses the calculation and check that the tooltip uses the same format.
- Exit the edit mode and confirm the matrix, not just the preview, shows the format.
Exercise
A visual calculation Share = DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS)) displays 0.18 in a matrix cell. A colleague suggests rewriting it as FORMAT(DIVIDE([Sales Amount], COLLAPSEALL([Sales Amount], ROWS)), "0%") so it shows 18%. What problem would that cause, and what is a better approach?
Show answer
FORMAT returns text, so the calculation's data type becomes text. The column then sorts alphabetically and cannot be used in numeric calculations. Keep the original numeric expression and set the calculation's format to Percentage (with the number of decimal places you want) in the Format pane. The cell then shows 18% while the stored value remains 0.18.
Exercise
You add MOVINGAVERAGE([Sales Amount], 3) to a table and it shows values such as 1,204.3333333. How do you get a cleaner display without changing the stored values, and does the displayed rounding affect a later sort by this column?
Show answer
Set the calculation's format to a decimal or currency format with a fixed number of decimal places (for example 0 or 2) in the Format pane. This changes only the display. Sorting uses the stored full-precision values, so the order is not affected by the displayed rounding.