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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
dorian_paoli06
Regular Visitor

Time with if statments is too long

Hi every one, i would like to know if it is possible to simplify my code.

The time spent is way too long and i don't know any alternative to do the same thing but way faster with less if statments. 

 

My code : 

Capture.PNG

 
 
var test =
IF(FIRSTDATE('calendar BVE validés'[Date]) <= FIRSTDATE('Calendar enregistrements'[Date]),
IF(FIRSTDATE('calendar BVE uploadés'[Date]) <= FIRSTDATE('Calendar enregistrements'[Date]),
IF(FIRSTDATE('calendar tickets validés'[Date]) <= FIRSTDATE('Calendar enregistrements'[Date]),
IF(FIRSTDATE('calendar tickets uploadés'[Date]) <= FIRSTDATE('Calendar enregistrements'[Date]),
IF(FIRSTDATE('calendar validation'[Date]) <= FIRSTDATE('Calendar enregistrements'[Date]),
IF(FIRSTDATE('calendar 1ere connexion'[Date]) <= FIRSTDATE('calendar email'[Date]),
IF(FIRSTDATE('calendar email'[Date]) <= FIRSTDATE('Calendar enregistrements'[Date]),
CALCULATE(distinctcount('PersonValues'[PersonGuid]), FILTER(PersonValues, PersonValues[Type] = -1732183153)),
'Mesure PV'[Emails confirmés]),'Mesure PV'[1ère connexion]), 'Mesure PV'[Profils validés]), 'Mesure CI'[Tickets uploadés]), 'Mesure CI'[Tickets validés]), 'Mesure SV'[Création de bordereaux]), 'Mesure SV'[Validation de bordereaux])

return
IF( MAXX({FIRSTDATE('calendar email'[Date]), FIRSTDATE('calendar 1ere connexion'[Date]), FIRSTDATE('calendar profils complétés'[Date]),FIRSTDATE('calendar validation'[Date]), FIRSTDATE('calendar tickets uploadés'[Date]), FIRSTDATE('calendar tickets validés'[Date]), FIRSTDATE('calendar BVE uploadés'[Date]), FIRSTDATE('calendar BVE validés'[Date])},[VALUE])>Date(2020,12,19),[Emails confirmés], test).
 
Thank you in advance for the time you will spend on my problem.
1 ACCEPTED SOLUTION
johnt75
Super User
Super User

There are a few things you can do to clean up the code which will make it easier to read and maintain and also improved performance. 

Instead of using FIRSTDATE use MIN. FIRSTDATE returns a table, MIN returns a scalar value and you are using it as a scalar value.

Retrieve all the min dates only once and store them in variables. Currently you have to calculate each minimum date twice, which will impact on performance.

Rather than using the nested IF statements, which are impossible to read, use the pattern

SWITCH( TRUE(),
    Condition 1, Result 1,
    Condition 2, Result 2,
    Default result
)

You could also store the result of the [Email confirmes] measure in a variable, as you are using that twice as well.

View solution in original post

1 REPLY 1
johnt75
Super User
Super User

There are a few things you can do to clean up the code which will make it easier to read and maintain and also improved performance. 

Instead of using FIRSTDATE use MIN. FIRSTDATE returns a table, MIN returns a scalar value and you are using it as a scalar value.

Retrieve all the min dates only once and store them in variables. Currently you have to calculate each minimum date twice, which will impact on performance.

Rather than using the nested IF statements, which are impossible to read, use the pattern

SWITCH( TRUE(),
    Condition 1, Result 1,
    Condition 2, Result 2,
    Default result
)

You could also store the result of the [Email confirmes] measure in a variable, as you are using that twice as well.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.