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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Calculating the number of days between two date columns

Need help in DAX (Power Query). I've tried a few options but keep on getting an error.  

 

1. Calculate the number of days between 2 dates in 2 date columns (I used DATESBETWEEN and DATEDIFF but getting errors in my DAX syntax)

2. Except for calculating the days between the two date columns per row, I want to calculate the number of days to month-end. Say the total number of days between the 2 dates  (date columns) are 30, then I want to calculate the number of days that can be allocated to the current month ( Month that transaction was initiated) and the number of the days that can be allocated to next month 

Column 1: 2021/06/06

Column 2: 2021/07/06

Total number of days: 30 days 

Number of days allocated to Jun: 24 days

Number of days allocated to Jul: 6 days

 

I was able to solve the problem with Excel but I am looking for a solution in Power Query as I think it will be more efficient than using formulas.

 

DATA
1stDate2ndDateTransaction Amount
2021/06/062021/07/0630000
2021/07/232021/07/296000
2021/07/252021/08/039000
2021/08/042021/09/0330000
2021/08/222021/09/1019000
2021/09/052021/09/1611000
2021/09/282021/10/035000
2021/10/012021/10/2019000

 

1 ACCEPTED SOLUTION
smpa01
Community Champion
Community Champion

@Anonymous  Power Query and Dax are two different programming and I am guessing that you need a DAX solution, not that it can't be done by Power Query.

 

If you need a PQ solution give a shout.

 

The DAX solution is following

 

Measure =
COUNTX (
    ADDCOLUMNS (
        GENERATE (
            'Table',
            DATESBETWEEN ( 'Calendar'[Calendar_Date], 'Table'[1stDate], 'Table'[2ndDate] )
        ),
        "period",
            MAXX (
                FILTER (
                    'Calendar',
                    'Calendar'[Calendar_Date] = EARLIER ( 'Calendar'[Calendar_Date] )
                ),
                'Calendar'[Year_Month]
            )
    ),
    [period]
)

 

You need a Calendar table for this  with minimum columns like following

| Calendar_Date | Year_Month |
|---------------|------------|
| 2000-01-01    | 200001     |

 

Both the tables are disconnected tables with no relationship. The measure will give you following

 

smpa01_0-1634836725340.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

5 REPLIES 5
smpa01
Community Champion
Community Champion

@Anonymous  attached pbix

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
smpa01
Community Champion
Community Champion

@Anonymous  Power Query and Dax are two different programming and I am guessing that you need a DAX solution, not that it can't be done by Power Query.

 

If you need a PQ solution give a shout.

 

The DAX solution is following

 

Measure =
COUNTX (
    ADDCOLUMNS (
        GENERATE (
            'Table',
            DATESBETWEEN ( 'Calendar'[Calendar_Date], 'Table'[1stDate], 'Table'[2ndDate] )
        ),
        "period",
            MAXX (
                FILTER (
                    'Calendar',
                    'Calendar'[Calendar_Date] = EARLIER ( 'Calendar'[Calendar_Date] )
                ),
                'Calendar'[Year_Month]
            )
    ),
    [period]
)

 

You need a Calendar table for this  with minimum columns like following

| Calendar_Date | Year_Month |
|---------------|------------|
| 2000-01-01    | 200001     |

 

Both the tables are disconnected tables with no relationship. The measure will give you following

 

smpa01_0-1634836725340.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Hey @smpa01 , thanks so much for your awnser!

 

Is there any way to transform this visualization table into a data table? Cause this solution worked very well, but I need to use the result to create charts and it give me wrong results as I cant use 1st date and 2nd date.

 

Or maybe, a measure that gives me the awsnser by just giving Year_month and the measure to a bar chart for example.

 

Thanks in advance!

Anonymous
Not applicable

Is it possible to get the PQ solution for this?  The DAX solution works well but is a little slow with my data.

Apologies - I can see this was asked over a year ago and this might not be the cleverest solution, but this is how I would calculate days;

In Power Query it should just be as easy as 

 

Duration.Days([2nd Date]-[1st Date])

 

to give you the number of days between 2 columns.
To calculate Month End, I would duplicate the column that you want to count from month end - so of it's 1st Date's month end, duplicate 1st Date. Or if thats the current month end, create a Todays date column using 

 

DateTime.Date(DateTime.LocalNow())

 

 Then, from the duplicated column Extract the Month End from the Transform Ribbon > Date > Month > End of Month.

 

Create a custom column again using the same code as the first column;

Duration.Days([Month End Date]-[1st Date])

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.