How to Add a Calculated Row in Power BI Matrix
Power BI matrices are fantastic for summarizing data, but sometimes you need to go beyond the standard totals and show a custom calculation directly in a row - like a 'Gross Profit' line below your 'Sales' and 'COGS'. While there isn't a simple "Add Calculated Row" button, you can achieve this with a bit of DAX magic. This tutorial will walk you through a clear, step-by-step process to add custom calculated rows to your Power BI matrix visuals, giving you more powerful and insightful reports.
Why Add a Custom Calculated Row?
Matrices are great at displaying hierarchical data. For instance, you can easily show sales figures broken down by product category. By default, Power BI provides options to show subtotals and a grand total at the bottom. But what if the calculation you need isn’t a simple sum?
Consider a typical financial summary:
You have a row for Total Sales.
You have a row for Cost of Goods Sold (COGS).
You need a third row for Gross Profit, which is Total Sales - COGS.
Or another common scenario:
You have a row for Actual Revenue.
You have a row for Budgeted Revenue.
You want to see a Variance row (Actual - Budget) and maybe even a Variance % row.
Forcing your report viewers to perform this mental math or export the data to Excel defeats the purpose of an interactive dashboard. By adding these calculated rows directly into the matrix, you make your reports more insightful, scannable, and immediately actionable.
The Core Technique: Using a Disconnected Table and DAX
The solution involves a clever pattern in Power BI. Since you can't just inject a new row into an existing data field, the approach is to create a "scaffold" for your new matrix structure. Here’s the high-level concept:
Create a New, Separate Table: We will manually create a small, disconnected table that contains the names of the rows we want to display. This includes our original data rows (like "Sales" and "COGS") and our new calculated rows (like "Gross Profit").
Write a "Master" DAX Measure: We'll write a single DAX measure that powers the matrix's values. This measure will use a
SWITCH()function to check which row name from our new table is currently being evaluated and then perform the correct calculation for that specific row.Build the Matrix Visual: Finally, we'll construct the matrix using our new table for the row headers and our master DAX measure for the values.
This method gives you complete control over the rows, their order, and the calculations they display.
Step-by-Step Guide: Adding a Gross Profit Row
Let’s walk through the most common example: building a simple profit and loss (P&L) summary showing Sales, COGS, and Gross Profit. Imagine your data model includes a financial transactions table named Financials with Amount and Account columns, where accounts are named "Sales" and "COGS".
Step 1: Create Your Base Measures
Before creating the master measure, it's a best practice to create simple, explicit measures for your core calculations. This keeps your DAX clean and reusable.
In the Power BI ribbon, go to the Modeling tab and click New Measure. Create the following two measures:
Step 2: Create a Disconnected Table for Row Headers
Now, let's create the table that will define our matrix rows.
On the Home ribbon, click Enter Data.
A "Create Table" window will pop up. Create two columns:
Row Order: This numeric column will be used to correctly sort our rows later.
Row Name: This text column will contain the display names for our rows.
Enter the data as follows:
Row Order | Row Name |
1 | Sales |
2 | COGS |
3 | Gross Profit |
Name the table
P&L Displayand click Load.
This table is "disconnected" because it has no relationships with any other tables in your data model, which is exactly what we want for this technique.
Step 3: Write the Master DAX Measure
This measure is the brain of our operation. It will dynamically calculate the right value based on the row context provided by our P&L Display table.
Create a new measure called P&L Values with the following DAX code:
Let's break down this formula:
SELECTEDVALUE('P&L Display'[Row Name]): Checks which "Row Name" is currently being rendered in the matrix visual and stores it in theCurrentRowvariable.SWITCH(TRUE(), ...): Evaluates conditions in order. When it finds the firstTRUEcondition, it returns its result:If
CurrentRowis "Sales", returns[Total Sales].If
CurrentRowis "COGS", returns[Total COGS].If
CurrentRowis "Gross Profit", performs[Total Sales] - [Total COGS].
Step 4: Build Your Matrix Visual
With our ingredients ready, it's time to assemble the visual.
Drag a Matrix visual onto your Power BI canvas.
In the Fields pane, configure the fields:
Drag
'P&L Display'[Row Name]into the Rows field well.Drag
[P&L Values]into the Values field well.Optionally, add other dimensions like
DateorRegioninto the Columns field well to slice the data.
You should now see a matrix with your three rows: Sales, COGS, and a fully calculated Gross Profit row!
Fine-Tuning Your New Matrix
You’re almost there! Here are a couple of final touches to make your matrix perfect.
Tip 1: Getting the Sort Order Right
You may notice your rows are sorted alphabetically (COGS, Gross Profit, Sales), which isn't what you want. We created the Row Order column specifically to fix this.
Go to the Data View in Power BI.
Select your
P&L Displaytable.Click to select the Row Name column.
In the top ribbon, click Sort by Column and select Row Order.
Return to your report, and the matrix will now be sorted correctly according to the order you defined.
Tip 2: Applying Different Formatting (e.g., Percentages)
A common follow-up question is, "How do I show Gross Profit Margin as a percentage on a new row?" Since all values in the matrix are driven by a single measure, Power BI applies a single number format. A workaround is to handle formatting directly inside your DAX using the FORMAT() function.
First, update your P&L Display table to include a "Gross Profit Margin" with Row Order 4.
Next, update your master measure. Here's a version that handles formatting:
IMPORTANT CAVEAT: Using the FORMAT() function converts the numerical value into a text string. This means you cannot use the output for further calculations (like grand totals) or in other visuals that require number inputs (like line charts). It’s best used only for display purposes in tables and matrices.
Final Thoughts
Adding calculated rows to a Power BI matrix unlocks a new level of reporting clarity, allowing you to embed key business logic like profit margins or variance analysis directly into your visuals. The technique of using a disconnected table with a master SWITCH measure is a powerful pattern that gives you full control over how you present your final numbers.
While this is a great solution within Power BI, it highlights the kind of multi-step, technical process required by traditional BI tools. Learning DAX and navigating the interface to build these kinds of reports often requires a real investment of time. We believe getting insights from your data shouldn't be that complicated. With Graphed, you can connect your data sources in seconds and create the same dashboards in plain English. Instead of writing complex formulas, you can simply ask, "Show me a report of sales, COGS, and gross profit by month," and get a real-time, shareable dashboard instantly.