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 Difference Between Dates in Same Column

Hi - 

 

I am new to PowerBI and hoping for some help with finding the number of days between dates in the same column,.  I need to find the number of days between orders by the same customer with a different invoice number.  A sample of how the data is laid out is below, along with the desired result calcuated manually in excel.

 

I have tried combinations of DATEDIFF, EARLIER, TOPN and other functions but have not been able to find a solution.

 

Thanks for your help!

 

 

InvoiceDateInvoiceNoCustomerDays Between Previous Order       
4/4/201934567Henry0<---need to make sure there is no error since this is the first order
4/4/201934567Henry0       
4/4/201934567Henry0       
4/21/201987654Sarah0       
5/2/201950000Jill0       
5/2/201950000Jill0       
5/7/201978906Henry33       
5/7/201978906Henry33<--- need to find the #days since Henry ordered a different invoice #
5/7/201978906Henry33       
5/20/201956789Sarah29       
5/20/201956789Sarah29       
5/20/201956789Sarah29       
6/1/201990000Henry25       
6/1/201990000Henry12       
6/7/201932892Jim0       
6/7/201932892Jim0       
6/7/201932892Jim0       
           
1 ACCEPTED SOLUTION
Stachu
Community Champion
Community Champion

code for a calculated column, it's all based on assumption that an invoice only has a single date (no collective invoicing etc.)

Column =
VAR __CurrentCustomer = 'Table'[Customer]
VAR __CurrentInvoiceDate = 'Table'[InvoiceDate]
VAR __CurrentInvoiceNo = 'Table'[InvoiceNo]
VAR __PreviousInvoiceDate =
    CALCULATE (
        MAX ( 'Table'[InvoiceDate] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Customer] = __CurrentCustomer
                && 'Table'[InvoiceNo] <> __CurrentInvoiceNo
                && 'Table'[InvoiceDate] < __CurrentInvoiceDate
        )
    )
RETURN
    IF (
        ISBLANK ( __PreviousInvoiceDate ),
        BLANK (),
        'Table'[InvoiceDate] - __PreviousInvoiceDate
    )

it may also not give correct results if customer has multiple invoices on the same day (difference will be 0). This can be avoided if the invoice numbers are always increasing and numerical (assumption that previous invoice always has a lower number, but the matter of cancellations comes to mind then)



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

8 REPLIES 8
Stachu
Community Champion
Community Champion

code for a calculated column, it's all based on assumption that an invoice only has a single date (no collective invoicing etc.)

Column =
VAR __CurrentCustomer = 'Table'[Customer]
VAR __CurrentInvoiceDate = 'Table'[InvoiceDate]
VAR __CurrentInvoiceNo = 'Table'[InvoiceNo]
VAR __PreviousInvoiceDate =
    CALCULATE (
        MAX ( 'Table'[InvoiceDate] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Customer] = __CurrentCustomer
                && 'Table'[InvoiceNo] <> __CurrentInvoiceNo
                && 'Table'[InvoiceDate] < __CurrentInvoiceDate
        )
    )
RETURN
    IF (
        ISBLANK ( __PreviousInvoiceDate ),
        BLANK (),
        'Table'[InvoiceDate] - __PreviousInvoiceDate
    )

it may also not give correct results if customer has multiple invoices on the same day (difference will be 0). This can be avoided if the invoice numbers are always increasing and numerical (assumption that previous invoice always has a lower number, but the matter of cancellations comes to mind then)



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Anonymous
Not applicable

Thanks! I tried this but all the rows in the column came back blank. I also tried removing the If blank language (see below), but it still came up blank.

 

Any ideas on what this might be?

 

 

POWER BI.PNG

Stachu
Community Champion
Community Champion

try removing the ".[Day]" after the invoice date



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Anonymous
Not applicable

thanks @Stachu !  this worked! my one addition was to add +1 to the return (SalesExport[InvoiceDate] +1- _PreviousInvoiceDate), otherwise it would give the number of days one day off, and as 31 for some reason if there was an invoice the day before.

 

my only problem now is this only seems to work if stored as a date value and shown visually as a Day within the date hierarchy, which means I can't sum the column or perform other operations like average etc.

Stachu
Community Champion
Community Champion

you need to change the data type of this column to Whole Number in the Modeling ribbon (possibly also format) - that should do the trick, PowerBI sets it up as a date by default



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Anonymous
Not applicable

thanks so much! for some reason didn't work the first go, but tried again and its working!

 

thanks again!!

Anonymous
Not applicable

thanks so much! for some reason didn't work the first go, but tried again and its working!

 

thanks again!!

Anonymous
Not applicable

thanks @Stachu ! this worked now!

the only thing I had to change was adding a +1 in the RETURN section - otherwise for dates that had the prevoius date the day before, it was returning the day difference as 31

 

RETURN
IF(
ISBLANK(_PreviousInvoiceDate),
BLANK(),
SalesExport[InvoiceDate] +1- _PreviousInvoiceDate
)
 

the only problem now is that this only seems to work if stored as a date and show visibly as DAY within the Date hierarch - which means I can't sum the column or use for other calculations such as average etc.

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.