DAX Formula to get number of days between two dates?

Brass Contributor

Hi, all. I have developed a power pivot and I am seeking to find the number of days between two dates. I have tried DATEDIFF unsuccessfully. Any hints/tips on how to calculate this? On the screenshot below I am seeking to get the number of days between the "BeginHarvestDate" and the "PlantDate". Thanks for any help!Screenshot (2).png

5 Replies

@slohombre 

You may simply try this...

Number of Days:=DATEDIFF(DaysToHarvest[plantdate],[beginharvestdate],DAY)

@slohombre 

As variant in DAX that could be as well

=INT(Table1[End]-Table1[Start])

but why don't you do this transformation in Power Query since the table generated in it. That's like

..., each Duration.Days(Duration.From([End]-[Start]))

In general it's better to avoid calculated columns generated by DAX in data model if you can do them in Power Query.

@Sergei Baklan Thank you. I will execute this in Power Query - thanks for that advice. I am having trouble with your formula in the power query editor. I used:  Duration.Days(#duration[beginharvestdate]-[plantdate]) 

 

That formula is yielding an expression error: We cannot apply field access to the type function. Details:

Value=[Function]

Key=beginharvestdate

 

Any thoughts on what I am doing wrong?

@slohombre 

If you wish to do it in Power Query, refer to the following screenshot.

 

Number of days.jpg

@Subodh_Tiwari_sktneer Thank you, Sir. Very helpful.