Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

How to add variance column to matrix

Hi.  Power BI Gurus. 

 

Would appreciate your advice on how to create a matrix table like the following:

Year to date MayActualBudgetPrior YearActual vs BudgetActual vs Prior Year
Sales10001200800-17%25%
COGS400600300-33%33%
Gross margin6006005000%20%
Operating costs50045040011%25%
Profit before tax100150100-33%0%

 

My dataset captures the P&L data by month and version:

MonthVersionSalesCOGSOperating costs
JanActual20080100
FebActual20080100
MarActual20080100
AprActual20080100
MayActual20080100
JanBudget1005037.5
FebBudget1005037.5
MarBudget1005037.5
AprBudget1005037.5
MayBudget1005037.5
JunBudget1005037.5
JulBudget1005037.5
AugBudget1005037.5
SepBudget1005037.5
OctBudget1005037.5
NovBudget1005037.5
DecBudget1005037.5
JanPrior Year1606080
FebPrior Year1606080
MarPrior Year1606080
AprPrior Year1606080
MayPrior Year1606080
JunPrior Year1707090
JulPrior Year1707090
AugPrior Year1707090
SepPrior Year1707090
OctPrior Year1707090
NovPrior Year1707090
DecPrior Year1707090

 

I managed to create a matrix to show the 1st 4 columns (below), but I am stumbled over how to add the variance columns:

 ActualBudgetPrior Year
Sales10001200800
COGS400600300
Gross margin600600500
Operating costs500450400
Profit before tax100150100

 

Pls advise.

 

 

1 ACCEPTED SOLUTION

Hi @Anonymous 

Create a copy of that query, do the transformation in the copied query.

 

Best Regards
Maggie

 

Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Nars_eci
New Member

Did you find a good solution?

v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

You need to re-create the data model.

Open Edit queries, 

 

1.

create a blank query named "bridge",

enter code in Advanced editor

let
    Source = Table1,
    #"Filtered Rows" = Table.SelectRows(Source, each ([Version] = "Actual"))
in
    #"Filtered Rows"

2. 

create a blank query named "Query1",

enter code in Advanced editor

let
    Source = Table1,
    #"Filtered Rows_budget" = Table.SelectRows(Source, each ([Version] = "Budget")),
    #"Removed Columns_budget" = Table.RemoveColumns(#"Filtered Rows_budget",{"Month"}),
    #"Grouped Rows_budget" = Table.Group(#"Removed Columns_budget", {"Version"}, {{"Sales", each List.Sum([Sales]), type number}, {"COGS", each List.Sum([COGS]), type number}, {"Operating costs", each List.Sum([Operating costs]), type number}}),
    
    #"Filtered Rows_actual" = Table.SelectRows(Source, each ([Version] = "Actual")),
    #"Removed Columns_actual" = Table.RemoveColumns(#"Filtered Rows_actual",{"Month"}),
    #"Grouped Rows_actual" = Table.Group(#"Removed Columns_actual", {"Version"}, {{"Sales", each List.Sum([Sales]), type number}, {"COGS", each List.Sum([COGS]), type number}, {"Operating costs", each List.Sum([Operating costs]), type number}}),

    #"Filtered Rows_prior" = Table.SelectRows(Source, each ([Version] = "Prior Year")),
    #"Merged Queries" = Table.NestedJoin(#"Filtered Rows_prior", {"Month"}, bridge, {"Month"}, "bridge", JoinKind.LeftOuter),
    #"Expanded bridge" = Table.ExpandTableColumn(#"Merged Queries", "bridge", {"Month"}, {"bridge.Month"}),
    #"Filtered Rows_prior1" = Table.SelectRows(#"Expanded bridge", each [bridge.Month] <> null and [bridge.Month] <> ""),
    #"Removed Columns_prior" = Table.RemoveColumns(#"Filtered Rows_prior1",{"Month", "bridge.Month"}),
    #"Grouped Rows_prior" = Table.Group(#"Removed Columns_prior", {"Version"}, {{"Sales", each List.Sum([Sales]), type number}, {"COGS", each List.Sum([COGS]), type number}, {"Operating costs", each List.Sum([Operating costs]), type number}}),
    
    Append_table=Table.Combine({#"Grouped Rows_budget", #"Grouped Rows_actual", #"Grouped Rows_prior"})
in
    Append_table

In this code, i create "budget", "actual", "prior" tables, finally, append all three tables together.

You could open my file to see each table.

 

3.

In Query1

Select "Sales", "COGS","Operating costs", then select "Unpivot columns",

 

Next,  select "Attribute" column, pivot column

2.png

 

add two custom columns

Actual vs Budget=([Actual]-[Budget])/[Budget]

Actual vs Prior Year=([Actual]-[Prior Year])/[Prior Year]

3.png

Close &&apply

 

4.png

 

 

Best Regards
Maggie

 

Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi Maggie.  The solution does not meet my requirement as:

- I need to keep the period & country selection flexible

- there are other charts which use the same dataset

 

Pls see my sample file in this link:

https://www.dropbox.com/s/v1zv1hycllbeqco/test.pbix?dl=0

Hi @Anonymous 

Create a copy of that query, do the transformation in the copied query.

 

Best Regards
Maggie

 

Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.