Deep dive into visual calculations: Adding calculations directly to your Power BI visuals (Generally Available)
With visual calculations (Generally Available), you can write DAX directly on a visual to calculate exactly what you need to show. They're useful when a calculation is specific to a single visual, so you can define the logic right where you need it instead of adding a model or report measure.
Figure: Create a visual calculation directly on a Power BI visual.
Custom totals for tables and matrices are also generally available. Custom totals are powered by visual calculations and give you a simple way to adjust the total row to fit your reporting needs.
Figure: Customize totals for table and matrix columns using built-in aggregation options.
How visual calculations work
A visual calculation is a DAX calculation directly on a visual. Because the calculation runs against the aggregated data already on the visual, the DAX is often simpler than the equivalent measure, and you don't need to manage filter context across the whole model.
To add a visual calculation, select a visual and choose New visual calculation in the ribbon. The visual calculations edit screen opens with a preview of your visual, a formula bar, and the visual matrix that shows the underlying data. As you write a calculation, the results appear immediately so you can see exactly what's happening. To edit an existing visual calculation, use the context menu in the Visualizations pane field list and choose Edit calculation to reopen the edit screen.
You can write most DAX functions you're used to from measures, and you also get visual-specific functions like RUNNINGSUM, MOVINGAVERAGE, COLLAPSE, PREVIOUS, NEXT, and LOOKUP. To make common patterns easier, visual calculations include built-in templates such as Running sum, Moving average, Percent of parent, Percent of grand total, Versus previous, and Versus last. Select a template to insert a starting expression, then adjust it for your data.
Custom totals for tables and matrices
By default, the total row in a table or matrix evaluates the field across the full filter context, which gives you a sum in most cases. Sometimes you want something different, such as an average, a count, or no total at all on a column.
Right-click any numerical column in a table or matrix and select Customize total calculation. Choose from Sum, Average, Min, Max, Count, Count (distinct), or None. Power BI handles the total with a visual calculation, so you can adjust totals to match how you want the data to read without writing the DAX yourself.
Example data to try out visual calculations
To follow along, open a blank Power BI Desktop file. On the Modeling ribbon, select New table then paste in this DAX to define a sample table:
Sales =DATATABLE("Year", STRING,"Quarter", STRING,"Sales", INTEGER,"Cost", INTEGER,{{"2024", "Q1", 120000, 75000},{"2024", "Q2", 135000, 82000},{"2024", "Q3", 150000, 90000},{"2024", "Q4", 180000, 105000},{"2025", "Q1", 140000, 86000},{"2025", "Q2", 158000, 94000},{"2025", "Q3", 172000, 102000},{"2025", "Q4", 205000, 118000}})
Add a table visual to the report and drop Year, Quarter, Sales, and Cost into it. You're ready to add your first visual calculation. We are using a table for these examples, but they aren’t limited to tables, you can use them in other visuals such as bar and line charts too!
Example: profit and a running total
Select the table, choose New visual calculation, and enter:
Profit = [Sum of Sales] - [Sum of Cost]
The new column appears immediately on the visual, calculated row by row. Add a second visual calculation using the Running sum template to track profit as it accumulates across quarters:
Running sum of profit = RUNNINGSUM([Profit])
Figure: Add a profit and running sum visual calculation to show accumulation of profit across quarters.
Formatting how visual calculations display is different than other columns or calculations, and straightforward. In the Visualizations pane choose Format visual and in the General tab open Data format. Pick the visual calculation from the Apply settings to dropdown and format it appropriately.
Figure: The formatting pane showing the data format options.
Example: Conditional formatting driven by a visual calculation
Visual calculations are also useful for keeping formatting logic close to the visual it applies to. Add another visual calculation to the same table:
Profit color =IF([Profit] < 50000, "good",IF([Profit] > 75000, "bad", BLANK()))
For this to work, set the new visual calculation’s data format to text. In the Visualizations pane, choose Format visual, open Data format on the General tab, select the visual calculation from the Apply settings to dropdown, and change the Data type to text. Visual calculations usually inherit the data type of the columns they reference.
Now apply it as conditional formatting. Open the Format pane for the table, expand Cell elements, turn on Background color for the Profit column, and select fx. In the Format style dropdown, choose Field value, then pick Profit color from the field list.
Finally, the Profit color visual calculation can be hidden from displaying in the visual.
Cells in the Profit column now color themselves based on the value: the report theme sentiment color for bad for low, good for high, and nothing for everything in between. The rule stays alongside the visual it applies to, which makes it easy to find and update later.
Comments
Post a Comment
Hi User,
Thanks for visiting My Blog and please provide your valuable feedback and subscribe for more updates. Please don't post any spam content or comments.
Thank You