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
kohlivinayak
Resolver I
Resolver I

no of days in between two dates month wise

Hi I have a table like below 

 

Type  |  Start date    |  end date  

A       | 07-feb-2014 | 24-apr-2014

B       | 12-mar-2014 | 30-oct-2015

 

and i want output like 

 

type   |  Feb-2014  | March-2014 | April-2014 | 

A        |       22        |       31           |      24         |

B        |         0        |       20           |       30        |

 

its calculation of no of days type A was used in a particular month.

 

Thanking you in advance.

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @kohlivinayak,

 

Suppose the original is called 'Source table'. 

 

Create a calendar table first:

Calendar =
CALENDAR (
    DATE ( YEAR ( MIN ( 'Source table'[Start date] ) ), MONTH ( MIN ( 'Source table'[Start date] ) ), 1 ),
    DATE ( YEAR ( MAX ( 'Source table'[end date] ) ), MONTH ( MAX ( 'Source table'[end date] ) ), 31 )
)

Filter above 'Calendar' table:

Calendar 2 =
FILTER (
    'Calendar',
    DAY ( [Date] )
        = CALCULATE (
            MAX ( 'Calendar'[Date].[Day] ),
            ALLEXCEPT ( 'Calendar', 'Calendar'[Date].[Month] )
        )
)

1.PNG

 

Cross Join 'Source table' and 'Calendar2'. And add a calculated column in new table.

Cross Join =
CROSSJOIN ( 'Source table', 'Calendar 2' )

Column1 =
IF (
    'Cross Join'[Date].[MonthNo] = 'Cross Join'[Start date].[MonthNo],
    DATEDIFF ( 'Cross Join'[Start date], 'Cross Join'[Date], DAY ) + 1,
    IF (
        'Cross Join'[Date].[MonthNo] = 'Cross Join'[end date].[MonthNo],
        'Cross Join'[end date].[Day],
        IF (
            'Cross Join'[Date].[MonthNo] > 'Cross Join'[Start date].[MonthNo]
                && 'Cross Join'[Date].[MonthNo] < 'Cross Join'[end date].[MonthNo],
            'Cross Join'[Date].[Day],
            0
        )
    )
)

2.PNG

 

Use a Matrix to host data from 'Cross Join' table.

3.PNG

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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-yulgu-msft
Employee
Employee

Hi @kohlivinayak,

 

Suppose the original is called 'Source table'. 

 

Create a calendar table first:

Calendar =
CALENDAR (
    DATE ( YEAR ( MIN ( 'Source table'[Start date] ) ), MONTH ( MIN ( 'Source table'[Start date] ) ), 1 ),
    DATE ( YEAR ( MAX ( 'Source table'[end date] ) ), MONTH ( MAX ( 'Source table'[end date] ) ), 31 )
)

Filter above 'Calendar' table:

Calendar 2 =
FILTER (
    'Calendar',
    DAY ( [Date] )
        = CALCULATE (
            MAX ( 'Calendar'[Date].[Day] ),
            ALLEXCEPT ( 'Calendar', 'Calendar'[Date].[Month] )
        )
)

1.PNG

 

Cross Join 'Source table' and 'Calendar2'. And add a calculated column in new table.

Cross Join =
CROSSJOIN ( 'Source table', 'Calendar 2' )

Column1 =
IF (
    'Cross Join'[Date].[MonthNo] = 'Cross Join'[Start date].[MonthNo],
    DATEDIFF ( 'Cross Join'[Start date], 'Cross Join'[Date], DAY ) + 1,
    IF (
        'Cross Join'[Date].[MonthNo] = 'Cross Join'[end date].[MonthNo],
        'Cross Join'[end date].[Day],
        IF (
            'Cross Join'[Date].[MonthNo] > 'Cross Join'[Start date].[MonthNo]
                && 'Cross Join'[Date].[MonthNo] < 'Cross Join'[end date].[MonthNo],
            'Cross Join'[Date].[Day],
            0
        )
    )
)

2.PNG

 

Use a Matrix to host data from 'Cross Join' table.

3.PNG

 

Best regards,

Yuliana Gu

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

Hi,

 

I've followed the steps, but my output in most cases for column 1 is a negative, any idea why this maybe?

 

Thanks in Advance

Did you resolve the negatives?

I think I have worked it out!! This code is aimed at dates that are all in the same year!! so there needs to be an additional bit of code to check if not only it is the same month, it must be the same year. 

Thanks a lot, i feel there is a lot to learn.

where can i find such things to learn more in powerbi ?

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.