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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Oros
Post Partisan
Post Partisan

Matrix Table

Hello.

 

I have a main table and a date table.  The main table has sales by 2 companies.  The date table has fiscal months and related to the main table through the DATE column.

 

MAIN TABLE

DATECUSTOMERSALESDEPT.
Nov. 1, 2021ABC25A
Nov. 2, 2021XZY50B
Dec. 5, 2022ABC35A

 

Using Matrix table,  this is the result that I am getting

 

CUSTOMERDEPT. ADEPT. ADEPT. BDEPT. BTOTAL (LY)TOTAL (CY)
 NOV. (LY)NOV. (CY)NOV. (LY)NOV. (CY)  
CUST. ABC200100500300700400

 

 

However, I would like to present the month to month comparison between current year (CY) and last year (LY) so that the matrix table will show like this.

CUSTOMERDEPT.  ADEPT. BTOTAL (LY)DEPT. ADEPT. BTOTAL (CY)DIFFERENCE (CY)-(LY)
 (LY) NOV(LY) NOV (CY) NOV(CY) NOV  
CUST. ABC200

500

700100300400-300

 

Here is the table and matrix illustration:

 

Oros_0-1668747505751.png

 

 

Any help is highly appreciated.  Thanks.

 

 

 

 

3 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@Oros , the Calculation group can help measure needs to be above 

 

Calculation Groups- Measure Slicer, Measure Header Grouping, Measure to dimension conversion. Complex Table display : https://youtu.be/qMNv67P8Go0

View solution in original post

PaulDBrown
Community Champion
Community Champion

Here is one way. First the model:

model.jpgCreate a new table to use for the columns in the matrix following this pattern:

 

Custom Matrix =
VAR _Dept =
    ADDCOLUMNS (
        VALUES ( 'Department Table'[DEPT.] ),
        "Index", RANK.EQ ( 'Department Table'[DEPT.], 'Department Table'[DEPT.], ASC )
    )
VAR _Rows =
    DISTINCTCOUNT ( 'Department Table'[DEPT.] )
VAR _Total = { ( "Total", _Rows + 1 ) }
VAR _DT =
    UNION ( _Dept, _Total )
VAR _Metrics =
    {
        FORMAT ( DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ), "MMM" ) & " (LY)",
        FORMAT ( DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ), "MMM" ) & " (CY)"
    }
VAR _A =
    CROSSJOIN ( _DT, _Metrics )
VAR _Diff =
    { ( "Difference (CY)-(LY)", _Rows + 2, BLANK () ) }
RETURN
    ADDCOLUMNS (
        UNION ( _A, _Diff ),
        "Period",
            SWITCH (
                TRUE (),
                CONTAINSSTRING ( [Value], "LY" ), 1,
                CONTAINSSTRING ( [Value], "CY" ), 2,
                3
            )
    )

 

Custom Matrix.jpg

Next the measures, starting with a simple sum measure for the sales:

 

Sales CY =
CALCULATE (
    [Sum Sales],
    FILTER (
        ALL ( 'Date Table' ),
        'Date Table'[Year] = YEAR ( TODAY () )
            && 'Date Table'[MonthNum] = MONTH ( TODAY () )
    )
)
Sales PY =
CALCULATE (
    [Sum Sales],
    FILTER (
        ALL ( 'Date Table' ),
        'Date Table'[Year]
            = YEAR ( TODAY () ) - 1
            && 'Date Table'[MonthNum] = MONTH ( TODAY () )
    )
)

 

and the final measure to use in the matrix:

 

Measure for Custom Matrix = 
VAR _CY = CALCULATE([Sales CY], TREATAS(VALUES('Custom Matrix'[DEPT.]), 'Department Table'[DEPT.]))
VAR _LY = CALCULATE([Sales PY], TREATAS(VALUES('Custom Matrix'[DEPT.]), 'Department Table'[DEPT.]))
VAR _DIFF = [Sales CY] - [Sales PY]
VAR _Rows = COUNT('Department Table'[DEPT.])
RETURN
SWITCH(TRUE(),
AND(MAX('Custom Matrix'[Index]) = _Rows +1, MAX('Custom Matrix'[Period]) = 1), [Sales PY],
AND(MAX('Custom Matrix'[Index]) = _Rows +1, MAX('Custom Matrix'[Period]) = 2), [Sales CY],
SELECTEDVALUE('Custom Matrix'[Period]) = 1, _LY,
SELECTEDVALUE('Custom Matrix'[Period]) = 2, _CY,
SELECTEDVALUE('Custom Matrix'[Period]) = 3, _DIFF)

 

Now create the matrix with the customer field as rows, the fields from the Custom Matrix table as columns and the [Measure for Custom Matrix] as values. Turn off the column subtotals and you get:

result.jpg

 

Sample PBIX file attached

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

Hello @PaulDBrown ,

 

Thank you so much!

View solution in original post

4 REPLIES 4
PaulDBrown
Community Champion
Community Champion

Here is one way. First the model:

model.jpgCreate a new table to use for the columns in the matrix following this pattern:

 

Custom Matrix =
VAR _Dept =
    ADDCOLUMNS (
        VALUES ( 'Department Table'[DEPT.] ),
        "Index", RANK.EQ ( 'Department Table'[DEPT.], 'Department Table'[DEPT.], ASC )
    )
VAR _Rows =
    DISTINCTCOUNT ( 'Department Table'[DEPT.] )
VAR _Total = { ( "Total", _Rows + 1 ) }
VAR _DT =
    UNION ( _Dept, _Total )
VAR _Metrics =
    {
        FORMAT ( DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ), "MMM" ) & " (LY)",
        FORMAT ( DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ), "MMM" ) & " (CY)"
    }
VAR _A =
    CROSSJOIN ( _DT, _Metrics )
VAR _Diff =
    { ( "Difference (CY)-(LY)", _Rows + 2, BLANK () ) }
RETURN
    ADDCOLUMNS (
        UNION ( _A, _Diff ),
        "Period",
            SWITCH (
                TRUE (),
                CONTAINSSTRING ( [Value], "LY" ), 1,
                CONTAINSSTRING ( [Value], "CY" ), 2,
                3
            )
    )

 

Custom Matrix.jpg

Next the measures, starting with a simple sum measure for the sales:

 

Sales CY =
CALCULATE (
    [Sum Sales],
    FILTER (
        ALL ( 'Date Table' ),
        'Date Table'[Year] = YEAR ( TODAY () )
            && 'Date Table'[MonthNum] = MONTH ( TODAY () )
    )
)
Sales PY =
CALCULATE (
    [Sum Sales],
    FILTER (
        ALL ( 'Date Table' ),
        'Date Table'[Year]
            = YEAR ( TODAY () ) - 1
            && 'Date Table'[MonthNum] = MONTH ( TODAY () )
    )
)

 

and the final measure to use in the matrix:

 

Measure for Custom Matrix = 
VAR _CY = CALCULATE([Sales CY], TREATAS(VALUES('Custom Matrix'[DEPT.]), 'Department Table'[DEPT.]))
VAR _LY = CALCULATE([Sales PY], TREATAS(VALUES('Custom Matrix'[DEPT.]), 'Department Table'[DEPT.]))
VAR _DIFF = [Sales CY] - [Sales PY]
VAR _Rows = COUNT('Department Table'[DEPT.])
RETURN
SWITCH(TRUE(),
AND(MAX('Custom Matrix'[Index]) = _Rows +1, MAX('Custom Matrix'[Period]) = 1), [Sales PY],
AND(MAX('Custom Matrix'[Index]) = _Rows +1, MAX('Custom Matrix'[Period]) = 2), [Sales CY],
SELECTEDVALUE('Custom Matrix'[Period]) = 1, _LY,
SELECTEDVALUE('Custom Matrix'[Period]) = 2, _CY,
SELECTEDVALUE('Custom Matrix'[Period]) = 3, _DIFF)

 

Now create the matrix with the customer field as rows, the fields from the Custom Matrix table as columns and the [Measure for Custom Matrix] as values. Turn off the column subtotals and you get:

result.jpg

 

Sample PBIX file attached

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Hello @PaulDBrown ,

 

Thank you so much!

amitchandak
Super User
Super User

@Oros , the Calculation group can help measure needs to be above 

 

Calculation Groups- Measure Slicer, Measure Header Grouping, Measure to dimension conversion. Complex Table display : https://youtu.be/qMNv67P8Go0

Hello @amitchandak ,

 

Thank you so much!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.