Forum Discussion
Need help calculating weighted average
- 6 years ago
I manually put your sample data in a model, here is the PQ code for that:
// StateAverage let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s5LTVXSUTJVitWJVvLILACyDQ2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Procedure = _t, StateAverageLos = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Procedure", type text}, {"StateAverageLos", Int64.Type}}) in #"Changed Type" // Data let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMU9JR8s5LTQVSZkqxOtFKbqlJCCFjsJBvYhFCyAIs5FiAJGQJVVWJEDICC3mVIhlvCBEC2+iRWQA3HWIhRMQMyT6IiDmSdRARUyTbICJGxki2QYRMlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Procedure = _t, Procedures = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}, {"Procedure", type text}, {"Procedures", Int64.Type}}) in #"Changed Type"After that, create the necessary relationship between the 2 tables:
Then add this measure to the model:
WeightedStateAverageLos = VAR vWeightedSum = SUMX ( Data, Data[Procedures] * RELATED ( StateAverage[StateAverageLos] ) ) VAR vCountProcedures = SUM ( Data[Procedures] ) VAR vRetval = DIVIDE ( vWeightedSum, vCountProcedures ) RETURN vRetvalThis should give you the expected result:
Obviously, to see the months in the correct order, there are more transformation needed, but that's not the scope of your question.
I manually put your sample data in a model, here is the PQ code for that:
// StateAverage
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s5LTVXSUTJVitWJVvLILACyDQ2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Procedure = _t, StateAverageLos = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Procedure", type text}, {"StateAverageLos", Int64.Type}})
in
#"Changed Type"
// Data
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMU9JR8s5LTQVSZkqxOtFKbqlJCCFjsJBvYhFCyAIs5FiAJGQJVVWJEDICC3mVIhlvCBEC2+iRWQA3HWIhRMQMyT6IiDmSdRARUyTbICJGxki2QYRMlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Procedure = _t, Procedures = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}, {"Procedure", type text}, {"Procedures", Int64.Type}})
in
#"Changed Type"
After that, create the necessary relationship between the 2 tables:
Then add this measure to the model:
WeightedStateAverageLos =
VAR vWeightedSum =
SUMX (
Data,
Data[Procedures] * RELATED ( StateAverage[StateAverageLos] )
)
VAR vCountProcedures =
SUM ( Data[Procedures] )
VAR vRetval =
DIVIDE ( vWeightedSum, vCountProcedures )
RETURN
vRetval
This should give you the expected result:
Obviously, to see the months in the correct order, there are more transformation needed, but that's not the scope of your question.
- Anonymous6 years agoNot applicable
Hi Arklur
Thank you so much, this is exactly what I needed. I am still new in Power BI , your explanation was clear and simple to understand.
Thanks again.
Regards
Ilky