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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

Optimizing a query with repetitive conditions in a filter

Hi all,

 

I'm currently trying to optimize a measure that takes a while to load : 

 

 

 

MyMeasure:=
CALCULATE (
    [Submeasure],
    USERELATIONSHIP ( Sales[IdMember], 'Members'[IdMember] ),
    FILTER (
        'Members',
        'Members'[Metier] <> "Chef"
            && 'Members'[Metier] <> "Agriculteur"
            && 'Members'[Metier] <> "Fonctionnaire"
            && 'Members'[Metier] <> "Comptable"
            && 'Members'[Metier] <> BLANK ()
    )
)

 

 

 

I do know that a common good practice is to refactor repeated values in a variable, but in this case, I don't think it would work since variables are a constant and in this case Metier changes for every line we're filtering.

 

When it comes to the submeasure, I just recently optimized it by precalculating some values in the source view.

 

 

SubMeasure :=
VAR lastPeriod = 'Time'[LastPeriod]
RETURN
    CALCULATE (
        [IsOK],
        FILTER (
            ALL ( Managing[PeriodLastSale], Managing[idPeriodEntry] ),
            IF (
                ISBLANK ( Managing[PeriodLastSale] ),
                IF ( lastPeriod - Managing[idPeriodEntry] < 5, TRUE (), FALSE () ),
                IF ( lastPeriod - Managing[PeriodLastSale] < 5, TRUE (), FALSE () )
            )
        )
    )

 

 

Right now this parent measure takes a pretty long time to load, do you have an idea how I could optimize it ?

(The names are just mock names to convey the idea).

 

Kindly,

Nossair

1 ACCEPTED SOLUTION
ppm1
Solution Sage
Solution Sage

It is not good practice to filter a whole table in your CALCULATE, so your first measure could be written like this.

MyMeasure :=
CALCULATE (
    [Submeasure],
    USERELATIONSHIP ( Sales[IdMember], 'Members'[IdMember] ),
    NOT ( 'Members'[Metier] IN { "Chef", "Agriculteur", "Fonctionnaire", "Comptable", "" } )
)

 

Also, it is likely your submeasure may be causing the problem. Having IF (and nested IF, in your case) evaluated on each row of an iterator like FILTER will slow it down. You could likely rewrite it with AND or && instead of doing IFs.

 

Plus, your LastPeriod variable appears to be a column reference. Is that a measure?

 

Pat

Microsoft Employee

View solution in original post

1 REPLY 1
ppm1
Solution Sage
Solution Sage

It is not good practice to filter a whole table in your CALCULATE, so your first measure could be written like this.

MyMeasure :=
CALCULATE (
    [Submeasure],
    USERELATIONSHIP ( Sales[IdMember], 'Members'[IdMember] ),
    NOT ( 'Members'[Metier] IN { "Chef", "Agriculteur", "Fonctionnaire", "Comptable", "" } )
)

 

Also, it is likely your submeasure may be causing the problem. Having IF (and nested IF, in your case) evaluated on each row of an iterator like FILTER will slow it down. You could likely rewrite it with AND or && instead of doing IFs.

 

Plus, your LastPeriod variable appears to be a column reference. Is that a measure?

 

Pat

Microsoft Employee

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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