Forum Discussion
Power BI matrix table
- 4 years ago
Ok, see if this helps.
1) Create a table which reflects the structure you need for the header. In my example I've merged the columns for quearter from the calendar table with a table created using "enter data" in Power Query with the names of the measures to be included in the headers.
The M code for this is:
let Source = #"Calendar Table", #"Removed Other Columns" = Table.SelectColumns(Source,{"Quarter", "QuarterNum"}), #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns", {"Quarter"}), MeasureHeader = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7MSS1WitWJVlJVyC9LLVIISSxKTy1Rio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MHeader = _t]), #"Changed Type" = Table.TransformColumnTypes(MeasureHeader,{{"MHeader", type text}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type), #"Added Custom" = Table.AddColumn(#"Removed Duplicates", "Custom", each #"Added Index"), #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"MHeader", "Index"}, {"MHeader", "Index"}), #"Sorted Rows" = Table.Sort(#"Expanded Custom",{{"Quarter", Order.Ascending}}) in #"Sorted Rows"To get
Leave this table unrelated in the model
2) Create the measures you wish to display (the measures listed in the header table) using TREATAS following the equivalent to:
Sales MH = CALCULATE ( [Sum of Sales], TREATAS ( VALUES ( 'Matrix Header'[QuarterNum] ), 'Calendar Table'[QuarterNum] ) )3) Create the final measure to use in the matrix with:
Matrix final measure = SWITCH ( SELECTEDVALUE ( 'Matrix Header'[MHeader] ), "Sales", [Sales MH], "% over Target", FORMAT ( [% over Target], "#0,0%" ) )(You will need the FORMAT function if you are mixing number types. If not, you can just format the measure with the regular number type)
4) Create the matrix visual using:
Rows: whatever field from the model is needed
Columns: the fields from the Matrix Header Table you have created
Values: the [Matrix final measure]
To get:
I've attached the sample PBIX file for you
See if this helps:
Here is another example:
Ok, see if this helps.
1) Create a table which reflects the structure you need for the header. In my example I've merged the columns for quearter from the calendar table with a table created using "enter data" in Power Query with the names of the measures to be included in the headers.
The M code for this is:
let
Source = #"Calendar Table",
#"Removed Other Columns" = Table.SelectColumns(Source,{"Quarter", "QuarterNum"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns", {"Quarter"}),
MeasureHeader = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7MSS1WitWJVlJVyC9LLVIISSxKTy1Rio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MHeader = _t]),
#"Changed Type" = Table.TransformColumnTypes(MeasureHeader,{{"MHeader", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Removed Duplicates", "Custom", each #"Added Index"),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"MHeader", "Index"}, {"MHeader", "Index"}),
#"Sorted Rows" = Table.Sort(#"Expanded Custom",{{"Quarter", Order.Ascending}})
in
#"Sorted Rows"
To get
Leave this table unrelated in the model
2) Create the measures you wish to display (the measures listed in the header table) using TREATAS following the equivalent to:
Sales MH =
CALCULATE (
[Sum of Sales],
TREATAS ( VALUES ( 'Matrix Header'[QuarterNum] ), 'Calendar Table'[QuarterNum] )
)
3) Create the final measure to use in the matrix with:
Matrix final measure =
SWITCH (
SELECTEDVALUE ( 'Matrix Header'[MHeader] ),
"Sales", [Sales MH],
"% over Target", FORMAT ( [% over Target], "#0,0%" )
)
(You will need the FORMAT function if you are mixing number types. If not, you can just format the measure with the regular number type)
4) Create the matrix visual using:
Rows: whatever field from the model is needed
Columns: the fields from the Matrix Header Table you have created
Values: the [Matrix final measure]
To get:
I've attached the sample PBIX file for you