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

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

Reply
MIrlam
Frequent Visitor

How to factor year in to my expression

Hi all

 

I'm hoping someone can help with this. My dax expression is:

 

LOS Days (AdmissionMonth) = IF(MONTH('Admissions and Discharges'[DateAdmitted])>=MONTH('Admissions and Discharges'[DCDateForOBD]),DATEDIFF('Admissions and Discharges'[DateAdmitted],'Admissions and Discharges'[DCDateForOBD],DAY),DATEDIFF('Admissions and Discharges'[DateAdmitted],ENDOFMONTH('Admissions and Discharges'[DateAdmitted].[Date]),DAY))
 
which basically counts the length of time someone has stayed if they are admitted one month and discharged the next. This works fine except for January as it currently does not consider the year - is there a way i can easily edit this to make that work?
 
thanks
2 ACCEPTED SOLUTIONS
AlexisOlson
Super User
Super User

There might be a way to make this quite a bit simpler.

 

Is this logic correct?

Column =
VAR StartDate = 'Admissions and Discharges'[DateAdmitted]
VAR EndDate =
    MIN ( 'Admissions and Discharges'[DCDateForOBD], EOMONTH ( StartDate, 0 ) )
RETURN
    DATEDIFF ( StartDate, EndDate, DAY )



View solution in original post

Hi @MIrlam 

 

Try this:

Column = 
IF(
    YEAR( 'Admissions and Discharges'[DateAdmitted] )
        & MONTH( 'Admissions and Discharges'[DateAdmitted] )
            >= YEAR( 'Admissions and Discharges'[DCDateForOBD] )
        & MONTH( 'Admissions and Discharges'[DCDateForOBD] ),
    DATEDIFF(
        'Admissions and Discharges'[DateAdmitted],
        'Admissions and Discharges'[DCDateForOBD],
        DAY
    ),
    DATEDIFF(
        'Admissions and Discharges'[DateAdmitted],
        ENDOFMONTH( 'Admissions and Discharges'[DateAdmitted].[Date] ),
        DAY
    )
)

 

Output:

VahidDM_0-1640214182113.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

View solution in original post

6 REPLIES 6
AlexisOlson
Super User
Super User

There might be a way to make this quite a bit simpler.

 

Is this logic correct?

Column =
VAR StartDate = 'Admissions and Discharges'[DateAdmitted]
VAR EndDate =
    MIN ( 'Admissions and Discharges'[DCDateForOBD], EOMONTH ( StartDate, 0 ) )
RETURN
    DATEDIFF ( StartDate, EndDate, DAY )



That is brilliant, thank you!

VahidDM
Super User
Super User

Hi @MIrlam 

 

Try this:

LOS Days (AdmissionMonth) =
IF(
    YEAR( 'Admissions and Discharges'[DateAdmitted] )
        && MONTH( 'Admissions and Discharges'[DateAdmitted] )
            >= YEAR( 'Admissions and Discharges'[DCDateForOBD] )
        && MONTH( 'Admissions and Discharges'[DCDateForOBD] ),
    DATEDIFF(
        'Admissions and Discharges'[DateAdmitted],
        'Admissions and Discharges'[DCDateForOBD],
        DAY
    ),
    DATEDIFF(
        'Admissions and Discharges'[DateAdmitted],
        ENDOFMONTH( 'Admissions and Discharges'[DateAdmitted].[Date] ),
        DAY
    )
)

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

Thank you, that has helped with the Jan / Dec overlap as highlighted below. However I now have an issue where the datediff isn't calculating as I think it should? As far as I can see the dax is correct but i get this return when the months for dateadmitted and d/cdateforOBD are the same

Column = IF( YEAR( 'Admissions and Discharges'[DateAdmitted] ) && MONTH( 'Admissions and Discharges'[DateAdmitted] ) >= YEAR( 'Admissions and Discharges'[DCDateForOBD] ) && MONTH( 'Admissions and Discharges'[DCDateForOBD] ), DATEDIFF( 'Admissions and Discharges'[DateAdmitted], 'Admissions and Discharges'[DCDateForOBD], DAY ), DATEDIFF( 'Admissions and Discharges'[DateAdmitted], ENDOFMONTH( 'Admissions and Discharges'[DateAdmitted].[Date] ), DAY ) ) 

MIrlam_0-1640182273292.png

Thanks

Hi @MIrlam 

 

Try this:

Column = 
IF(
    YEAR( 'Admissions and Discharges'[DateAdmitted] )
        & MONTH( 'Admissions and Discharges'[DateAdmitted] )
            >= YEAR( 'Admissions and Discharges'[DCDateForOBD] )
        & MONTH( 'Admissions and Discharges'[DCDateForOBD] ),
    DATEDIFF(
        'Admissions and Discharges'[DateAdmitted],
        'Admissions and Discharges'[DCDateForOBD],
        DAY
    ),
    DATEDIFF(
        'Admissions and Discharges'[DateAdmitted],
        ENDOFMONTH( 'Admissions and Discharges'[DateAdmitted].[Date] ),
        DAY
    )
)

 

Output:

VahidDM_0-1640214182113.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

perfect, works now thanks!

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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.