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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
crln-blue
Post Patron
Post Patron

Measure for calculating average of each table column depending on the value from another column

Hello everyone,


For context, I'm using a pie chart visual to show comparison of the statuses on my data but I have the following set of columns on my table:

NameLocationDateStatus 1Status 2Status 3Status 4
AUS11/25/20221206
AUS05/09/2022552-4
ACAN01/18/20226 1 
AUS11/25/2022-14 4
BHK05/09/2022 -26-2
BCH01/18/202223 0
BSIN11/25/20226770
BHK05/09/2022-81 9-8
CUS01/18/2022 -8-280
DHK03/03/2023610 12
EHK08/19/20239 107


Above data is let's say a simplified version of my table (but I got more columns and statuses).

So what I'm aiming is to use the pie chart and use the statuses from the Status columns as my legend. I thought of unpivoting the Status columns in order to get a Status columns and its Values column so I can drag the Status column to the Legend of the pie chart.

crlnblue_0-1697642697255.png

My values would be the average of each of the status column.

However, for scaleability, I think that this strategy is not good as this produces additional rows of data because my initial rows will be multiplied by the number of my status columns.

So I created a separate status table (no relationship to my main table):

crlnblue_1-1697642941525.png

And my measure is this:

 

 

 

Status Average = 
VAR _Max = MAX( 'Status Table'[Status] )
VAR _avg_status1 = AVERAGE('main table'[Status 1])
VAR _avg_status2 = AVERAGE('main table'[Status 2])
VAR _avg_status3 = AVERAGE('main table'[Status 3])
VAR _avg_status4 = AVERAGE('main table'[Status 4])

VAR _Switch = 
    SWITCH(
        _Max,
         "Status 1", _avg_status1,
         "Status 2", _avg_status2,
         "Status 3", _avg_status3,
         "Status 4", _avg_status4,)
Return _Switch

 

 

 

It just gives a blank data. If I return the variable _Max, it displays all the status from the status table I made. If I input a constant value (say, number 1) on their switch statements, it outputs the constant value but if I get the average from its status column (in the main table), its blank.
screenshot below is when I use a constant number in the switch statement (it shows up):

crlnblue_0-1697643565777.png
But when I use my intended calculation (averaging the column), it does not show at all. 

 

What would be the correct way in this measure?

Thank you very much! ♥

 

 

 

 

 

1 REPLY 1
lbendlin
Super User
Super User

Your first action must be to unpivot your data to make it usable.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZDBDoMgDIZfxXCW0II6OKpb4rLEi9nJ+P6vsRZaoh6W8EMJH/9f2HczmtZ8N5oQne+dB+95Q+IVSIM52hMHvYOkXC/i2nYVnMeVSXQYlRxIjTg3V8dLsmWgE7g4TlQtn3s0n1t1pkLJeblHs4LcgMpt7/WezU4PEfyJthHFLuVdRuf6QefspgDcYayuz+oaHA1Cg6Qj6DeVB70qGR0mJZNCkJs9jh8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Location = _t, Date = _t, #"Status 1" = _t, #"Status 2" = _t, #"Status 3" = _t, #"Status 4" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Name", "Location", "Date"}, "Status", "Value")
in
    #"Unpivoted Other Columns"

(How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".)

 

With that all your calculations become super easy.

 

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.