问题
This is the desired layout of the matrix that I need. I need to have two columns under one column:
However, I ended up with this(I used different values, so the values might be slightly different.):
With the following columns in the matrix fields:
This is a sample of the dataset. Total Units sum up columns A and B. The Order column sorts the Status column:
STATUS A B Total Units Order
ABC 3 0 3 1
DEF 0 6 6 2
ABC 3 2 5 1
ABC 5 6 11 1
GHI 0 4 4 3
ABC 5 3 8 1
DEF 0 9 9 2
How do I get my desired layout? Do I need to pivot the table? Or do I need to group certain values together?
回答1:
First, transform your data to this:
Changes I've made:
- Unpivoted source data for columns A, B, and renamed resulting column as "Unit Type"
- Deleted column "Total Units", it's unnecessary
- Named your table "Data" (you can change it to whatever name you prefer).
Then, write 2 DAX measures.
Measure 1:
Units = SUM(Data[Value])
Measure 2:
% = DIVIDE ( [Units], CALCULATE ( [Units], ALL ( Data[STATUS] ) ) )
Then, create a matrix as follows:
Result:
Explanation:
- You need to unpivot data to put A and B types into the same column. This will allow you to use them to slice data as matrix rows;
- Total Units is a useless column because we can easily reproduce it with DAX measure, and then it will be dynamic (it will respond to matrix rows, columns, slicers and other interactive controls);
- % formula needs to calculate % of units for a given unit type, but across all Status. We accomplish that by first calculating subtotal units for ALL(Data[STATUS]), and then dividing units by them.
来源:https://stackoverflow.com/questions/56626197/power-bi-matrix-two-columns-under-on-column