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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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)

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)))

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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