Forum Discussion

phuang's avatar
phuang
Frequent Visitor
5 years ago
Solved

Help! Calculate average in pivot table

Hi all,

I am trying to get the correct average from a pivot table.

This is a survey to study where people would like to spend $50.

Each person is given $50 to spend and 5 different category.  One person, Sue didn't want to spend on anything.

When I calculate the average in excel, she's not counted.

However, after I unpivot, the total of sum seems to change from 200 to 250.

 

Table one is the original table: 

 ABCDE 
Mark020030050
Tim551552050
Sue000000
Mary10151001550
Alice10100151550
Total2550255050200

In excel, the average is correct, for example column A average is 25/200 = 0.125

However, the average is incorrect in tabel 2, 25/250 = 0.1

The sum of all values should be 200, not 250.

I don't know why it is 250 in the pivot table.  Is there a way to fix it?

 

Table 2: I unpivot table 1 and create table 2

Name       Attribute Value

MarkA0
MarkB20
MarkC0
MarkD30
MarkE0
TimA5
TimB5
TimC15
TimD5
TimE20
SueA0
SueB0
SueC0
SueD0
SueE0
MaryA10
MaryB15
MaryC10
MaryD0
MaryE15
AliceA10
AliceB10
AliceC0
AliceD15
AliceE15

 

Thank you!

Penny 

 

  • phuang - you must have something else going on. Your data totals 200. See this M code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k0sylbSUTIAYiMDKMMYwojViVYKycwFsk2h2BDGMILIBpemQrWgYpAc0OBKkBYDmD5DmDyQB1LgmJOZnApXgSQJURELAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, A = _t, B = _t, C = _t, D = _t, E = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name"}, "Attribute", "Value")
    in
        #"Unpivoted Other Columns"

    When I load that into Power BI and create a quick matrix, I get this:

    Sue is correct at zero.

    Please post more details on what is wrong if this doesn't help. See steps below to provide data in the forums in a usable format. Thanks!

    How to use M code provided in a blank query:
    1) In Power Query, select New Source, then Blank Query
    2) On the Home ribbon, select "Advanced Editor" button
    3) Remove everything you see, then paste the M code I've given you in that box.
    4) Press Done
    5) See this article if you need help using this M code in your model.

     

    How to get good help fast. Help us help you.

    How To Ask A Technical Question If you Really Want An Answer

    How to Get Your Question Answered Quickly - Give us a good and concise explanation
    How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.

     

     

  • Hi phuang ,

     

    In Power BI, the total is not 250. The calculation logic is as below:

     

    Please create a measure like so:

    Avg =
    DIVIDE (
        CALCULATE ( SUM ( 'Table'[Value] ), ALLEXCEPT ( 'Table', 'Table'[Attribute] ) ),
        CALCULATE ( SUM ( 'Table'[Value] ), ALL () )
    )
    

     

    Best Regards,

    Icey

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

6 Replies

  • edhans's avatar
    edhans
    Community Champion

    phuang - you must have something else going on. Your data totals 200. See this M code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k0sylbSUTIAYiMDKMMYwojViVYKycwFsk2h2BDGMILIBpemQrWgYpAc0OBKkBYDmD5DmDyQB1LgmJOZnApXgSQJURELAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, A = _t, B = _t, C = _t, D = _t, E = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name"}, "Attribute", "Value")
    in
        #"Unpivoted Other Columns"

    When I load that into Power BI and create a quick matrix, I get this:

    Sue is correct at zero.

    Please post more details on what is wrong if this doesn't help. See steps below to provide data in the forums in a usable format. Thanks!

    How to use M code provided in a blank query:
    1) In Power Query, select New Source, then Blank Query
    2) On the Home ribbon, select "Advanced Editor" button
    3) Remove everything you see, then paste the M code I've given you in that box.
    4) Press Done
    5) See this article if you need help using this M code in your model.

     

    How to get good help fast. Help us help you.

    How To Ask A Technical Question If you Really Want An Answer

    How to Get Your Question Answered Quickly - Give us a good and concise explanation
    How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.

     

     

    • phuang's avatar
      phuang
      Frequent Visitor

      Hi edhans,

      Thanks for your response.  Yes, the data total is 200 in excel and the average is correct in excel but not in power bi. 

      In power bi, the average is not calculated with the 200, it avg with 250 so the average number is wrong.

      I googled and found that pivot table's average is not calculated by true sum, the total of all numbers.  Is there a way to force it to calculate by the true sum?

       

  • Icey's avatar
    Icey
    Community Support

    Hi phuang ,

     

    In Power BI, the total is not 250. The calculation logic is as below:

     

    Please create a measure like so:

    Avg =
    DIVIDE (
        CALCULATE ( SUM ( 'Table'[Value] ), ALLEXCEPT ( 'Table', 'Table'[Attribute] ) ),
        CALCULATE ( SUM ( 'Table'[Value] ), ALL () )
    )
    

     

    Best Regards,

    Icey

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • phuang's avatar
      phuang
      Frequent Visitor

      Hi Icey,

       

      Yes, the measurement works.  Thank you so much for explaining it to me and share the report.   Really appreciated!

       

      Penny