Skip to main content

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.

 

DataZoe_0-1782797798010.png

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.

 

DataZoe_1-1782797815796.png

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])

That's it. The calculation lives on the visual and reflects exactly what you see, without adding anything to the semantic model.

DataZoe_2-1782797833339.png

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.

DataZoe_3-1782797833342.png

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

Popular posts from this blog

Exception deserializing the package "The process cannot access the file because it is being used by another process."

TITLE: Microsoft Visual Studio ------------------------------ Failed to start project ------------------------------ ADDITIONAL INFORMATION: Exception deserializing the package "The process cannot access the file 'E:\SSASCube\HistoricalDataLoad\HistoricalDataLoad\bin\Development\HistoricalDataLoad.ispac' because it is being used by another process.". (Microsoft.DataTransformationServices.VsIntegration) ------------------------------ The process cannot access the file 'E:\SSASCube\HistoricalDataLoad\HistoricalDataLoad\bin\Development\HistoricalDataLoad.ispac' because it is being used by another process. (mscorlib) ------------------------------ BUTTONS: OK ------------------------------ While running SSIS package i got the error “The process cannot access the file ‘*.ispac’ because it is being used by another process”. I tried to close SSDT and run it again but, I still got the same error while compiling. Then, after searching over internet, I got...

SSRS INTERVIEW QUESTIONS

Q: What is SSRS? Ø   SSRS or SQL Server Reporting Service is a server-based report generation software systems from Microsoft and is part of Microsoft BI. Ø   It is used for preparing and delivering interactive and variety of reports. Ø   It is administered through an web based interface. Ø   Reporting services utilizes a web service interface for supporting and developing of customized reporting applications. Ø   SSRS lets you create very rich reports (Tabular/Graphical/Interactive) from various datasources with rich data visualization (Charts, Maps, sparklines) Ø   SSRS allows are reports to be exported in various formats (Excel, PDF, word etc) Q: Explain SSRS Architecture? Reporting services architecture comprises of integrated components. It is a multi-tiered, included with application, server and data layers. This architecture is scalable and modular. A single installation can be used across multiple computers. It includes the fo...

Failed to execute the package or element. Build errors were encountered

Error: TITLE: Microsoft Visual Studio ------------------------------ Failed to execute the package or element.   Build errors were encountered. For more information, see the Output window. ------------------------------ BUTTONS: OK ------------------------------   Solution: We tried to close SSDT and run it again but, we still got the same error while running SSIS package. Then, we need to follow bellow solution: Step 1: Go to Task Manager–> Details Tab. Step 2: Locate the process “ DtsDebugHost.exe “. Kill this process. There might be multiple instances of this process. Kill all of them. Step 3: Rerun SSIS package