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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jasonjklim
Frequent Visitor

Calculate rows in a matrix and Sorting

I created the Sales and Revenue sheet for my report. 
I figured out the rows calculation of matrix from this link. 

https://community.powerbi.com/t5/Desktop/calculate-difference-between-two-rows-in-a-matrix/m-p/82832...

So, my DAX is like this

 

Measuretest =
VAR result =
CALCULATE (
SUM ( Waterfall[Value] ),
Distinct(VALUES ( 'Table'[Category]))
)
VAR list =
SUMMARIZE ( ALLSELECTED ( Waterfall ), 'Table'[Category]"Result"result )


RETURN
IF (
result <> BLANK (),
result,
VAR WF =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "Waterfall" )
VAR Loss =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "Losses" )
VAR MG =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "IG" )
VAR concession =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "Concessions" )
VAR FY21 =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "FY21 Recurring Fee" )
 
VAR FY21TR =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "FY21 Total Revenue")
 
VAR ED =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "Event-driven")
VAR Distribution =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "Distribution")
VAR FY22_total_rev =
CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "FY22 Total Revenue")
RETURN
SWITCH (
SELECTEDVALUE ( 'Table'[Category] ),
"NNB"WF + Loss,
--"Total Market Growth", MG,
"Internal Growth"MG+concession,
"Organic Growth"WF + LossMG + concession,
"Recurring Fee Growth"WF + LossMG + concession,
"FY22 Recurring Fee"FY21 + WF + LossMG + concession,
"Total Revenue Growth"WF + LossMG + concession+EDDistribution,
"FY22 Total Revenue"WF + LossMG + concession+EDDistribution +FY21TR,
BLANK ()
)
)

 

Yet, When I tried to sort them in the matrix table, it only sorts only alphabectically. 

jasonjklim_0-1651701379559.png

For custom sorting, I created a new excel file like below and create its relationship + change my DAX to point this table. 
Yet, all my variables became 0, and not appear in the matrix. 

 

jasonjklim_1-1651701406241.png

 

 

After I created the Measure, I want to sort them with custom order. 

Like belows,

jasonjklim_3-1651701510480.png

 

 

Yet, PBI only allows me to sort by alphabetically of Category column.

 

Please help

2 REPLIES 2
v-yanjiang-msft
Community Support
Community Support

Hi @jasonjklim ,

I found your solution in another thread.

1. Create "Table" with Category and sort index

2. Add Sort Index filter in Calculate Command like below. After this, the matrix is working.

Measuretest =
VAR result =
    CALCULATE (
        SUM ( Waterfall[Value] ),
        VALUES ( 'Table'[Category] ),
        VALUES ( 'Table'[SortIndex] )
    )
VAR list =
    SUMMARIZE ( ALLSELECTED ( Waterfall ), 'Table'[Category], "Result", result )
RETURN
    IF (
        result <> BLANK (),
        result,
        VAR WF =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "Waterfall",
                'Table'[SortIndex] = 2
            )
        VAR Loss =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "Losses",
                'Table'[SortIndex] = 3
            )
        VAR MG =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "IG",
                'Table'[SortIndex] = 5
            )
        VAR concession =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "Concessions",
                'Table'[SortIndex] = 5
            )
        VAR FY21 =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "FY21 Recurring Fee",
                'Table'[SortIndex] = 1
            )
        VAR revenue_growth =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "Total Revenue Growth"
            )
        VAR FY21TR =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "FY21 Total Revenue",
                'Table'[SortIndex] = 12
            )
        VAR rf =
            CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "Recurring Fee" )
        VAR ED =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "Event-driven",
                'Table'[SortIndex] = 14
            )
        VAR Distribution =
            CALCULATE (
                SUM ( Waterfall[Value] ),
                'Table'[Category] = "Distribution",
                'Table'[SortIndex] = 15
            )
        VAR FY22_total_rev =
            CALCULATE ( SUM ( Waterfall[Value] ), 'Table'[Category] = "FY22 Total Revenue" )
        RETURN
            SWITCH (
                SELECTEDVALUE ( 'Table'[Category] ),
                "Net New Business", WF + Loss,
                "Market Driven", MG,
                "Internal Growth", MG + concession,
                "Organic Growth",
                    WF + Loss + MG + concession,
                "Recurring Fee Growth",
                    WF + Loss + MG + concession,
                "Recurring Fee",
                    WF + Loss + MG + concession,
                "FY22 Recurring Fee",
                    FY21 + WF + Loss + MG + concession,
                "Total Revenue Growth",
                    WF + Loss + MG + concession + ED + Distribution,
                "FY22 Total Revenue",
                    WF + Loss + MG + concession + ED + Distribution + FY21TR,
                " ", " ",
                BLANK ()
            )
    )

If you have no other questions, would you mind accept your method as solutions? Then we are able to close the thread. More people who have the same requirement will find the solution quickly and benefit here. Thank you.

 

Best Regards,
Community Support Team _ kalyj

amitchandak
Super User
Super User

@jasonjklim , You marked index as sort column for category and sorted on category in visual

 

How to Create Sort Column and Solve Related Errors:
https://www.youtube.com/watch?v=KK1zu4MBb-c

 

Sort Month Year.png

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.