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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Covington
Helper I
Helper I

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.

1 ACCEPTED SOLUTION

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"

Capture.PNG

 

 

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

 

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

2.PNG

 

Please find the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

5 REPLIES 5
MFelix
Super User
Super User

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:

 

average.png

 

Regards,

MFelix

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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.

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.

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"

Capture.PNG

 

 

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

 

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

2.PNG

 

Please find the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Hi @Covington ,

 

Does that make sense? If so, kindly mark my answer as the solution to close the case please. Thanks in advance.

 

Regards,
Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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