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
Anonymous
Not applicable

Filtering a matrix using a measure

Hi,

 

I have a matrix with multiple rows, let's say:

Project

    Sub-project

           Task

                Cost

 

I need to filter the matrix only for project which have a negative balance (I already have a measure which returns 1 if the balance is negative). However if I try to apply this measure as a filter to the matrix and show only where = 1 it obviously returns all the rows where the balance is negative.

The issue is that I want to return only negative project (so overall a project can be positive even if some of the subproject/task/cost might have a negative balance - I'm not interested in these).

Is there a way to write a measure to return up to the lowest level only if all the levels above are negative (starting from the highest level)?

 

As an example, for now it's returning Project A and Project B when applying the filter but I only want to see Project B (overall negative):

JustBI39_0-1627635094725.png

Thanks!

1 ACCEPTED SOLUTION

Hi  @Anonymous ,

Try measure like below:

 

TESS = 
IF (
    HASONEVALUE ( 'Table'[Subprojiect] ),
    IF (
        HASONEVALUE ( 'Table'[Task] ),
        IF (
            SUMX (
                FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
                'Table'[Budget] - 'Table'[Expenditure]
            ) < 0
                && SUMX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[Task] = MAX ( 'Table'[Task] )
                            && 'Table'[Project] = MAX ( 'Table'[Project] )
                            && 'Table'[Subprojiect] = MAX ( 'Table'[Subprojiect] )
                    ),
                    'Table'[Budget] - 'Table'[Expenditure]
                ) < 0,
            1,
            BLANK ()
        ),
        IF (
            SUMX (
                FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
                'Table'[Budget] - 'Table'[Expenditure]
            ) < 0
                && SUMX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[Project] = MAX ( 'Table'[Project] )
                            && 'Table'[Subprojiect] = MAX ( 'Table'[Subprojiect] )
                    ),
                    'Table'[Budget] - 'Table'[Expenditure]
                ) < 0
                && SUMX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[Task] = MAX ( 'Table'[Task] )
                            && 'Table'[Project] = MAX ( 'Table'[Project] )
                            && 'Table'[Subprojiect] = MAX ( 'Table'[Subprojiect] )
                    ),
                    'Table'[Budget] - 'Table'[Expenditure]
                ) < 0,
            1,
            BLANK ()
        )
    ),
    IF (
        SUMX (
            FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
            'Table'[Budget] - 'Table'[Expenditure]
        ) < 0,
        1,
        BLANK ()
    )
)

 

vluwangmsft_0-1628065981426.png

 

And if you want to show only ProjectB since it's the one which is negative on aggregate level and I want to see all the subprojects of ProjectB, not only the negative portion, try the following measure:

TESS1 = 
    IF (
        SUMX (
            FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
            'Table'[Budget] - 'Table'[Expenditure]
        ) < 0,
        1,
        BLANK ()
    )

Final get the below:

vluwangmsft_0-1628497734444.png

 

 

Wish it is helpful for you!

 

 

 

Best Regards

Lucien

View solution in original post

7 REPLIES 7
v-luwang-msft
Community Support
Community Support

Hi @Anonymous ,

Test like below:

base data:

vluwangmsft_0-1627896123848.png

Then use the below measure:

TEST1 = 
VAR TES1 =
    CALCULATE (
        SUM ( 'Table'[Cost] ),
        ALLEXCEPT ( 'Table', 'Table'[Project] ),
        'Table'[Task] = "Balance"
    )
VAR tes2 =
    CALCULATE (
        SUM ( 'Table'[Cost] ),
        ALLEXCEPT ( 'Table', 'Table'[Project], 'Table'[Subprojiect], 'Table'[Task] ),
        'Table'[Task] = "Balance"
    )
var test3=
    IF ( TES1 < 0 && tes2 < 0, 1, BLANK () )  return IF(HASONEVALUE('Table'[Task]),SUM('Table'[Cost]),test3)

 

Create matrix  by use the measure:

vluwangmsft_1-1627896188629.png

 

This is what you want!

vluwangmsft_2-1627896232076.png

 

 

Wish it is helpful for you!

 

Best Regards

Lucien

Anonymous
Not applicable

Hi @v-luwang-msft , thanks a lot! We are moving in the right direction. My data are structured in a different way. I have a measure (Balance) which calculates the difference between budget and expenditure and then a measure (Check Balance) which returns the projects/lines which are in negative balance.

 

I want to use the check balance measure to filter the table:

JustBI39_2-1627904868598.png

 

However I do want to show only ProjectB since it's the one which is negative on aggregate level and I want to see all the subprojects of ProjectB, not only the negative portion:

 

JustBI39_3-1627904917895.png

 

I'm not sure if this can be achieved though, since in the matrix filters are applied on the lowest level of the hierarchy (so showing only 1 on project level will not do the job because it will return an empty matrix).

Hi @Anonymous ,

Could you pls share your pbix file? Need to test it .Rememeber to remove confidential data.

 

Best Regards

Lucien

Anonymous
Not applicable

Hi @v-luwang-msft, can't upload files directly here.

See below a sample I adapted from your pbix file.

I have 2 tables with a 1 to * relationship (filter both direction).

Project:

JustBI39_1-1627977078684.png

Table:

JustBI39_0-1627977009350.png

JustBI39_0-1627979587476.png

 

Then the measures:

 

Balance =
SUM ( 'Table'[Budget] ) - SUM ( 'Table'[Expenditure] )
 
Check Balance =
VAR over =
SUMMARIZE (
'Project';
'Project'[Project];
"Overspend";
CALCULATE (
DISTINCTCOUNT ( 'Project'[Project] );
FILTER ( 'Project'; [Balance] < 0 )
)
)
RETURN
SUMX ( over; [Overspend] )
 
Thank you!

Hi  @Anonymous ,

Try measure like below:

 

TESS = 
IF (
    HASONEVALUE ( 'Table'[Subprojiect] ),
    IF (
        HASONEVALUE ( 'Table'[Task] ),
        IF (
            SUMX (
                FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
                'Table'[Budget] - 'Table'[Expenditure]
            ) < 0
                && SUMX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[Task] = MAX ( 'Table'[Task] )
                            && 'Table'[Project] = MAX ( 'Table'[Project] )
                            && 'Table'[Subprojiect] = MAX ( 'Table'[Subprojiect] )
                    ),
                    'Table'[Budget] - 'Table'[Expenditure]
                ) < 0,
            1,
            BLANK ()
        ),
        IF (
            SUMX (
                FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
                'Table'[Budget] - 'Table'[Expenditure]
            ) < 0
                && SUMX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[Project] = MAX ( 'Table'[Project] )
                            && 'Table'[Subprojiect] = MAX ( 'Table'[Subprojiect] )
                    ),
                    'Table'[Budget] - 'Table'[Expenditure]
                ) < 0
                && SUMX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[Task] = MAX ( 'Table'[Task] )
                            && 'Table'[Project] = MAX ( 'Table'[Project] )
                            && 'Table'[Subprojiect] = MAX ( 'Table'[Subprojiect] )
                    ),
                    'Table'[Budget] - 'Table'[Expenditure]
                ) < 0,
            1,
            BLANK ()
        )
    ),
    IF (
        SUMX (
            FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
            'Table'[Budget] - 'Table'[Expenditure]
        ) < 0,
        1,
        BLANK ()
    )
)

 

vluwangmsft_0-1628065981426.png

 

And if you want to show only ProjectB since it's the one which is negative on aggregate level and I want to see all the subprojects of ProjectB, not only the negative portion, try the following measure:

TESS1 = 
    IF (
        SUMX (
            FILTER ( ALL ( 'Table' ), 'Table'[Project] = MAX ( 'Table'[Project] ) ),
            'Table'[Budget] - 'Table'[Expenditure]
        ) < 0,
        1,
        BLANK ()
    )

Final get the below:

vluwangmsft_0-1628497734444.png

 

 

Wish it is helpful for you!

 

 

 

Best Regards

Lucien

Anonymous
Not applicable

Hi @ebeery , thanks for your reply. I actually already tried this solution. However I need to return "1" for all the lines down in the hierarchy for negative projects since when you apply a filter in a matrix it's being evaluated on the lowest level (i.e. for Project B I would like to get all the subprojects) and not on the project line.

I think should be something related to https://www.kasperonbi.com/use-isinscope-to-get-the-right-hierarchy-level-in-dax/ but I couldn't figure it properly.

ebeery
Solution Sage
Solution Sage

Hi @Anonymous take a look at the ISINSCOPE() function.

I think you should be able to do something like IF(ISINSCOPE([Project Column]), [Measure]) to return your "1" measure only on the Project lines of the hierarchy.

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.