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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Reine
Helper IV
Helper IV

DATEDIFF with IF statement?

Hello 🙂 I have made a column in my report table to calculate the age from the date of birth using DATEDIFF. Upon seeing the data, I realize that I need to add something so that if the age is less than 5 it kicks back "unknown" instead of calculating the age. DOB is a required field when entering someone into our system, so if we didn't get a patients date of birth on initial contact, we put in either that days date or something close. Can I use an IF statement with DATEDIFF? I have tried it a couple of ways but not quite getting it so hoping one of you can help.

 

Capture.PNG

1 ACCEPTED SOLUTION
jdbuchanan71
Super User
Super User

My guess is you are getting converion errors with the IF?  Try something like this:

Age =
VAR AgeCalc =
    DATEDIFF ( [LaddersT.PersonT(PersonID).BirthDate], TODAY (), YEAR )
RETURN
    IF ( AgeCalc < 5, "Unknown", FORMAT ( AgeCalc, "00" ) )

View solution in original post

3 REPLIES 3
jdbuchanan71
Super User
Super User

My guess is you are getting converion errors with the IF?  Try something like this:

Age =
VAR AgeCalc =
    DATEDIFF ( [LaddersT.PersonT(PersonID).BirthDate], TODAY (), YEAR )
RETURN
    IF ( AgeCalc < 5, "Unknown", FORMAT ( AgeCalc, "00" ) )

Hi - now that I have patient ages, I need to be able to pull all patients in a given time period and show how many people there are in each age group - age groups being <18, 18-39, 40-59, 60+.

 

Can you help me figure that out as well?  Sorry - I wasn't thinking about this yesterday or I would have asked for both at the same time.

 

Thank you 🙂

Woohoo!  this works perfectly - thank you so much 🙂  I am new to Power BI as well as DAX so it's all a bit foreign to me still.  I'll study this to better understand what you've done.  I really appreciate your help - I spent hours trying to figure it out.  ugh

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

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

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.

Top Solution Authors
Top Kudoed Authors