Forum Discussion

Covington's avatar
Covington
Helper I
7 years ago
Solved

Calculate Average, insert into columns conditionally

I have a fairly simple table (I think), but I need to Calculate Averages, then insert them after the table is pivoted:

 

 

ProductP-Codeabcdef
P10055234.746.740.144.552.357.1
P10158234.447.641.041.447.263.8
P10257233.747.841.544.447.555.9
P10357233.547.139.341.452.760.8
P10456233.344.444.841.748.160.4
P10556232.347.541.443.249.755.1
        

 

My data comes in like this. 

 

I need to do the following:

  • Calculate the Averages of columns (a - f)
  • Pivot the Table to look like this (using unpivot):
ProductP-CodeAttributeValueAverage
P10055a234.7233.65
P10055b46.746.85
P10055c40.141.35
P10055d44.542.76667
P10055e52.349.58333
P10055f57.158.85

 

That would just be the first product's pivot.  You can see the Average is the Average of each column overall (regardless of product).  I *Think* I need tothen do something like this (but don't know how):

 

  • After Unpivot, Create New Column "Average"
  • Somehow Insert the average based upon the Attribute value

 

It's confusing.  I've tried so many ways by now I can barely remember them all :(

 

Thanks in advance.

  • Hi Covington ,

     

    Please check the following steps as below.

     

    1. Unpivoted the fact table and make the group by the column Attribute. Here is the M code for your reference.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TZDJDcUwCER78TlC2ICXLv49Sv9tfFYlF48EPM/AfbdfR2xXE9FnEMNS5RmC0E0YrCkDyGRp8bkctK7sBNlGF0yTDhiSxaEyCXaBwz9ykMJqwQ5CwjFBNxY4BdIXlJixFHQ8XDpqVJuZ+Dp6eSZIHw8uY4+x/TcFuUB5wThApqrlyJfj47xG1eM8fw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Product = _t, #"P-Code" = _t, a = _t, b = _t, c = _t, d = _t, e = _t, f = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"P-Code", Int64.Type}, {"a", type number}, {"b", type number}, {"c", type number}, {"d", type number}, {"e", type number}, {"f", type number}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product", "P-Code"}, "Attribute", "Value"),
        #"GroupBy"=Table.Group(#"Unpivoted Columns", {"Attribute"},{{"AveragePrice", each List.Average([Value]), type number}})
    in
        #"GroupBy"

    2. Duplicated the fact table.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TZDJDcUwCER78TlC2ICXLv49Sv9tfFYlF48EPM/AfbdfR2xXE9FnEMNS5RmC0E0YrCkDyGRp8bkctK7sBNlGF0yTDhiSxaEyCXaBwz9ykMJqwQ5CwjFBNxY4BdIXlJixFHQ8XDpqVJuZ+Dp6eSZIHw8uY4+x/TcFuUB5wThApqrlyJfj47xG1eM8fw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Product = _t, #"P-Code" = _t, a = _t, b = _t, c = _t, d = _t, e = _t, f = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"P-Code", Int64.Type}, {"a", type number}, {"b", type number}, {"c", type number}, {"d", type number}, {"e", type number}, {"f", type number}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product", "P-Code"}, "Attribute", "Value")
    in
        #"Unpivoted Columns"

    3. Merge the two tables by the column Attribute.

     

    let
        Source = Table.NestedJoin(Table1,{"Attribute"},#"Table1 (2)",{"Attribute"},"Table1 (2)",JoinKind.LeftOuter),
        #"Expanded Table1 (2)" = Table.ExpandTableColumn(Source, "Table1 (2)", {"Product", "P-Code", "Value"}, {"Table1 (2).Product", "Table1 (2).P-Code", "Table1 (2).Value"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1 (2)",{"Table1 (2).Product", "Table1 (2).P-Code", "Attribute", "Table1 (2).Value", "AveragePrice"})
    in
        #"Reordered Columns"

     

     

    By the way, we can create a calculated column in data pane.

     

    Column = CALCULATE(AVERAGE('Table1 (2)'[Value]),ALLEXCEPT('Table1 (2)','Table1 (2)'[Attribute]))

     

    Please find the pbix as attached.

     

    Regards,

    Frank

5 Replies

  • Hi Covington,

     

    I believe that the best option is to make the unpivot of the columns as you refer then create then create the following measure:

     

     

    Average =
    CALCULATE ( AVERAGE ( Table[Value] ); ALL ( Table[P-Code]; Table[Product] ) )
    

    See result below for the P-Code 55:

     

     

    Regards,

    MFelix

     

    • Covington's avatar
      Covington
      Helper I

      I did give that a try... At first it told me a comma was needed.  Then it told me that the name CALCULATE wasn't recognized.  I copied directly and simply changed to my Tablename where you had  "Table".  I'll keep trying harder.. gotta be something I did wrong.

      • Covington's avatar
        Covington
        Helper I

        ok... I'm starting to think I'm in the wrong product?

         

        I'm using Power Query Editor... I noticed I'm in PowerBI forum.  I bet there is no calculate where I'm at.