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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.