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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
Anonymous
Not applicable

calculate diff between two dates but in a specific month

looking to calculate day diff, but in a specific month. so in the example below, a new column to show days in Jan2012, first record should show 6 and others 0.

basically looking for a visual to show the total number of DAYSdiff in a specific month, there might be an easier way than above.

any help is appreciated!

 

 1.png

1 ACCEPTED SOLUTION

hi, @Anonymous 

If you try to improve the formula as below:

Measure 2 = 
IF (
    SELECTEDVALUE ( 'Table'[DATE1] ) <= MAX ( 'Date'[Date] )
        && SELECTEDVALUE ( 'Table'[DATE1] ) >= MIN ( 'Date'[Date] ),
 
    DATEDIFF ( SELECTEDVALUE ( 'Table'[DATE1] ), MAX ( 'Date'[Date] ) + 1, DAY ),
    IF (
        SELECTEDVALUE ( 'Table'[DATE1] ) <= MIN ( 'Date'[Date] )
            && SELECTEDVALUE ( 'Table'[DATE2] ) >= MAX ( 'Date'[Date] ),

        DATEDIFF ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] ) + 1, DAY ),
        IF (
            SELECTEDVALUE ( 'Table'[DATE1] ) <= MIN ( 'Date'[Date] )
                && SELECTEDVALUE ( 'Table'[DATE2] ) <= MAX ( 'Date'[Date] )&& SELECTEDVALUE ( 'Table'[DATE2] )>=MIN('Date'[Date]),

            DATEDIFF ( MIN ( 'Date'[Date] ), SELECTEDVALUE ( 'Table'[DATE2] ) + 1, DAY ),
            IF (
                SELECTEDVALUE ( 'Table'[DATE1] ) >= MIN ( 'Date'[Date] )
                    && SELECTEDVALUE ( 'Table'[DATE2] ) <= MAX ( 'Date'[Date] ),
                DATEDIFF ( SELECTEDVALUE ( 'Table'[DATE1] ), MAX ( 'Date'[Date] ) + 1, DAY ),
                0
            )
        )
    )
)

 

Best Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-lili6-msft
Community Support
Community Support

hi, @Anonymous 

For your case, you could try this way as below:

Step1:

Create a separate date table for select specific month.

Date =
ADDCOLUMNS (
    CALENDARAUTO (),
    "YearMonth", YEAR ( [Date] ) * 100
        + MONTH ( [Date] )
)

Step2:

Use this logic to create a measure as below:

DAYSdiff = 
IF (
    SELECTEDVALUE ( 'Table'[DATE1] ) <= MAX ( 'Date'[Date] )
        && SELECTEDVALUE ( 'Table'[DATE1] ) <= MIN ( 'Date'[Date] ),
    DATEDIFF ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] ) + 1, DAY ),
    IF (
        SELECTEDVALUE ( 'Table'[DATE1] ) <= MAX ( 'Date'[Date] ),
        DATEDIFF ( SELECTEDVALUE ( 'Table'[DATE1] ), MAX ( 'Date'[Date] ) + 1, DAY ),
        0
    )
)

Result:

10.JPG

here is my sample pbix, please try it.

 

Best Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Although it is informative, it is not the solution.

in the scenario below, only record 3 should have 30 days and all other record should be 0.

 

Capture1.PNG

hi, @Anonymous 

If you try to improve the formula as below:

Measure 2 = 
IF (
    SELECTEDVALUE ( 'Table'[DATE1] ) <= MAX ( 'Date'[Date] )
        && SELECTEDVALUE ( 'Table'[DATE1] ) >= MIN ( 'Date'[Date] ),
 
    DATEDIFF ( SELECTEDVALUE ( 'Table'[DATE1] ), MAX ( 'Date'[Date] ) + 1, DAY ),
    IF (
        SELECTEDVALUE ( 'Table'[DATE1] ) <= MIN ( 'Date'[Date] )
            && SELECTEDVALUE ( 'Table'[DATE2] ) >= MAX ( 'Date'[Date] ),

        DATEDIFF ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] ) + 1, DAY ),
        IF (
            SELECTEDVALUE ( 'Table'[DATE1] ) <= MIN ( 'Date'[Date] )
                && SELECTEDVALUE ( 'Table'[DATE2] ) <= MAX ( 'Date'[Date] )&& SELECTEDVALUE ( 'Table'[DATE2] )>=MIN('Date'[Date]),

            DATEDIFF ( MIN ( 'Date'[Date] ), SELECTEDVALUE ( 'Table'[DATE2] ) + 1, DAY ),
            IF (
                SELECTEDVALUE ( 'Table'[DATE1] ) >= MIN ( 'Date'[Date] )
                    && SELECTEDVALUE ( 'Table'[DATE2] ) <= MAX ( 'Date'[Date] ),
                DATEDIFF ( SELECTEDVALUE ( 'Table'[DATE1] ), MAX ( 'Date'[Date] ) + 1, DAY ),
                0
            )
        )
    )
)

 

Best Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
amitchandak
Super User
Super User

Created as column

Diff from date1 = 
VAR seldate = DATE(2013,05,31)
Return 
if(Sales[Order_Date] < seldate, DATEDIFF(Sales[Order_Date],seldate,DAY),0)
Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

In case you want a measure dependent on time/calendar

Diff from date3 = 
VAR seldate = max('OrderTime'[Order Date])
Return 
CALCULATE(sumx(Sales, if(Sales[Order_Date] < seldate, DATEDIFF(Sales[Order_Date],seldate,DAY),0)))

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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