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
andytmcc
Helper I
Helper I

Why is this filter not working properly?

Hi, 

I am creating a calculated column that pulls in a figure from the previous years row. I only want this figure to be pulled in when my [VARIABLE] column is equal to "Parliamentary Constituency". However, one value ('Mid Ulster') in my [Category] column appears in "Parliamentary Constituency" as well as in "LGD" which are both in my [VARIABLE] column. I only want the value to be created for "Parliamentary Constituency" not for LGD. It seems that my filter is not quite set up correctly: no values are being created for other "LGD's" (which is good), it is just being created for the one "LGD" that has the same name (Mid Ulster) that appears in the "Parliamentary Constituency".  I don't understand why && 'Loneliness All Data'[VARIABLE] == "Parliamentary Constituency" isn't restricting it to just "Parliamentary Constituencies". Sorry if this sounds confusing!

 

 

 

LOC Previous Year =

VAR PreviousRow =

    TOPN (

        1,

        FILTER (

            'Loneliness All Data',

            'Loneliness All Data'[YEAR] < EARLIER ( 'Loneliness All Data'[YEAR] )

                && 'Loneliness All Data'[CATEGORY] = EARLIER ( 'Loneliness All Data'[CATEGORY] ) && 'Loneliness All Data'[VARIABLE] == "Parliamentary Constituency"

        ),

        [YEAR], DESC

    )

VAR PreviousValue =

    MINX ( PreviousRow, [Locus Of Control Mean] )

RETURN

    PreviousValue

1 ACCEPTED SOLUTION

LOC Previous Year =
IF (
    'Loneliness All Data'[VARIABLE] = "Parliamentary Constituency",
    VAR PreviousRow =
        TOPN (
            1,
            FILTER (
                'Loneliness All Data',
                'Loneliness All Data'[YEAR] < EARLIER ( 'Loneliness All Data'[YEAR] )
                    && 'Loneliness All Data'[CATEGORY] = EARLIER ( 'Loneliness All Data'[CATEGORY] )
                    && 'Loneliness All Data'[VARIABLE] == "Parliamentary Constituency"
            ),
            [YEAR], DESC
        )
    VAR PreviousValue =
        MINX ( PreviousRow, [Locus Of Control Mean] )
    RETURN
        PreviousValue
)

View solution in original post

5 REPLIES 5
andytmcc
Helper I
Helper I

That's fantastic @johnt75  It seems to have done the trick. Thank you. Am I correct in saying I could remove the && 'Loneliness All Data'[VARIABLE] == "Parliamentary Constituency"  from within the filter on line 11 as it's not needed there anymore?

No, its still needed there to make sure you get the correct rows when Mid Ulster is in CATEGORY

johnt75
Super User
Super User

Wrap the whole calculation inside IF('Loneliness All Data'[VARIABLE] = "Parliamentary Constituency").

Your filter is restricting the rows returned by TOPN, but it is not doing a check to see if it should perform the calculation at all for the current row.

Ah ok. Thanks @johnt75  Could you show me how to do that? I'm pretty much a newbie!

LOC Previous Year =
IF (
    'Loneliness All Data'[VARIABLE] = "Parliamentary Constituency",
    VAR PreviousRow =
        TOPN (
            1,
            FILTER (
                'Loneliness All Data',
                'Loneliness All Data'[YEAR] < EARLIER ( 'Loneliness All Data'[YEAR] )
                    && 'Loneliness All Data'[CATEGORY] = EARLIER ( 'Loneliness All Data'[CATEGORY] )
                    && 'Loneliness All Data'[VARIABLE] == "Parliamentary Constituency"
            ),
            [YEAR], DESC
        )
    VAR PreviousValue =
        MINX ( PreviousRow, [Locus Of Control Mean] )
    RETURN
        PreviousValue
)

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.