Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

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.

 

 

  • 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.

5 Replies

  • v-juanli-msft's avatar
    v-juanli-msft
    Icon for Community Support rankCommunity 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

     

    add two custom columns

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

    Close &&apply

     

     

     

    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.

      • v-juanli-msft's avatar
        v-juanli-msft
        Icon for Community Support rankCommunity Support

        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.