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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
ka047
Helper I
Helper I

DATEDIFF DAX measure

I am trying to calculate the difference between 2 date fields. Both are in Fact tables & not created measures. AVERAGEX or SUMX won't work for me. Below is what I have so far, if someone could help me calculate the difference, I would appreciate it. Thanks!

 

Shipping Days =
summarize (Fact_SalesCogs,
Fact_SalesCogs[SalesOrder],
Fact_SalesCogs[DeliveryDate],
Fact_SalesCogs[ShippingDateConfirmed],
calculate(
datediff(Fact_SalesCogs[DeliveryDate], Fact_SalesCogs[ShippingDateConfirmed], day)
 
The above isn't working for me and I know I am missing something. 
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @ka047 ,

 

The SUMMARIZE function returns a summary table. If you want to calculate the difference between 2 date fields, try this:

 

Shipping Days =
DATEDIFF (
    Fact_SalesCogs[DeliveryDate],
    Fact_SalesCogs[ShippingDateConfirmed],
    DAY
)

 

 

If you want to create a calculated table which has Shipping Days column, try this:

 

New Table =
SUMMARIZE (
    Fact_SalesCogs,
    Fact_SalesCogs[SalesOrder],
    Fact_SalesCogs[DeliveryDate],
    Fact_SalesCogs[ShippingDateConfirmed],
    "Shipping Days",
        DATEDIFF (
            'Fact_SalesCogs'[DeliveryDate],
            Fact_SalesCogs[ShippingDateConfirmed],
            DAY
        )
)

 

 

The pbix file I tested is here.

 

 

Best Regards,

Stephen Tao

 

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

8 REPLIES 8
Syndicate_Admin
Administrator
Administrator

Good morning

Is it possible to create a measure with the DATEDIFF form using two date fields within the source table?

I am trying to calculate an indicator for invoices with start and end date with reference to the accumulated of a column (consumption), which adds the previous consumption data and I must divide it by the number of days that arises from the invoice period. I have the column of number of days created with the DATTEDIF forula but I skip the error when creating the indicator.

Igolpe_23_0-1683638464509.png

I do not know if the error is that. that a measure is necessary.

Thank you very much in advance.

Greetings

Anonymous
Not applicable

Hi @ka047 ,

Could you tell me if your problem has been solved?
If it is, kindly Accept it as the solution. More people will benefit from it.
Or you are still confused about it, please provide me with more details about your table and your problem or share me with your pbix file from your Onedrive for Business.


Best Regards,
Stephen Tao

Anonymous
Not applicable

Hi @ka047 ,

 

The SUMMARIZE function returns a summary table. If you want to calculate the difference between 2 date fields, try this:

 

Shipping Days =
DATEDIFF (
    Fact_SalesCogs[DeliveryDate],
    Fact_SalesCogs[ShippingDateConfirmed],
    DAY
)

 

 

If you want to create a calculated table which has Shipping Days column, try this:

 

New Table =
SUMMARIZE (
    Fact_SalesCogs,
    Fact_SalesCogs[SalesOrder],
    Fact_SalesCogs[DeliveryDate],
    Fact_SalesCogs[ShippingDateConfirmed],
    "Shipping Days",
        DATEDIFF (
            'Fact_SalesCogs'[DeliveryDate],
            Fact_SalesCogs[ShippingDateConfirmed],
            DAY
        )
)

 

 

The pbix file I tested is here.

 

 

Best Regards,

Stephen Tao

 

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

@ka047 , Try like

 

Shipping Days =
Sumx(summarize (Fact_SalesCogs,
Fact_SalesCogs[SalesOrder],
Fact_SalesCogs[DeliveryDate],
Fact_SalesCogs[ShippingDateConfirmed],
"_1",
calculate(
datediff(min(Fact_SalesCogs[DeliveryDate]), max(Fact_SalesCogs[ShippingDateConfirmed]), day))), [_1])

 

or

 

Shipping Days =
Sumx(summarize (Fact_SalesCogs,
Fact_SalesCogs[SalesOrder],
Fact_SalesCogs[DeliveryDate],
Fact_SalesCogs[ShippingDateConfirmed],
Fact_SalesCogs[DeliveryDate],
Fact_SalesCogs[ShippingDateConfirmed])
,datediff(min(Fact_SalesCogs[DeliveryDate]), max(Fact_SalesCogs[ShippingDateConfirmed]), day))

 

Also, refer: https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-2-Date-Differ...

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
TomMartens
Super User
Super User

Hey @ka047 ,

 

can you please explain why this won't work

Shipping Days =
SUMX(
Fact_SalesCogs,
Fact_SalesCogs[DeliveryDate] - Fact_SalesCogs[ShippingDateConfirmed]
)

Regards and a safe passing to 2021,

Tom 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

ka047_0-1609450314408.png

 

Hey @ka047 ,

what is your data source?

What is the data type of both columns? Both should be either Date or Datetime?

Did you import the data or are you using DirectQuery or LiveConnection?

 

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
MattAllington
Community Champion
Community Champion

I assume you are writing a measure. Why won't SUMX work?  SUMX works just like a calculated column - I see no reason why it won't work. 

sumx(fact,datediff(Fact_SalesCogs[DeliveryDate], Fact_SalesCogs[ShippingDateConfirmed], day))



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors