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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
emf_efab
Frequent Visitor

Getting the date of following ID to calculate (datediff) between initial date and final date

I need to get the date of the next ID in the same Order (Pedido), to make afterwards a DateDiff and have as a result the days spended in that state (Estado). For example I need to fill up the blue column (Dia_Sig) with the "correct" date. In ID "31" I should have data as shown, plus "Dia_Sig" 20/02/2021, to then calculate the days and have a next column with "18" as a result.

I need to know how many days I spend in each "Estado", having as final result that the Order (Pedido) lasted 38 days, between 01/02/2021 (Initial Date) and the (Last Date) 11/03/2021. I have the PBIX, and Excel file to attach and send, for better understanding.

 

emf_efab_0-1627999193457.png

 

1 ACCEPTED SOLUTION

Since you want to create a calculated column then you can write it as follows:

Dia_Sig = 
Var SelectedID = 'Control Emails'[ID]
RETURN
CALCULATE (
    Min ( 'Control Emails'[Fecha] ),
    FILTER (
        ALLEXCEPT ( 'Control Emails', 'Control Emails'[Pedido#] ),
        'Control Emails'[ID] > SelectedID
    )
)

The measure to calculate he duration could be:

Duration = 
IF (
    HASONEVALUE ( 'Control Emails'[Pedido#] ),
    DATEDIFF (
        MIN ( 'Control Emails'[Fecha] ),
        MAX ( 'Control Emails'[Dia_Sig] ),
        DAY
    )
)

 

m_refaei_0-1628127639706.png

(last column)

 

m_refaei_1-1628127677954.png

Please check the adjusted file.

View solution in original post

7 REPLIES 7
emf_efab
Frequent Visitor

The pbix Order Ctrl and the Excel Testing 

Since you want to create a calculated column then you can write it as follows:

Dia_Sig = 
Var SelectedID = 'Control Emails'[ID]
RETURN
CALCULATE (
    Min ( 'Control Emails'[Fecha] ),
    FILTER (
        ALLEXCEPT ( 'Control Emails', 'Control Emails'[Pedido#] ),
        'Control Emails'[ID] > SelectedID
    )
)

The measure to calculate he duration could be:

Duration = 
IF (
    HASONEVALUE ( 'Control Emails'[Pedido#] ),
    DATEDIFF (
        MIN ( 'Control Emails'[Fecha] ),
        MAX ( 'Control Emails'[Dia_Sig] ),
        DAY
    )
)

 

m_refaei_0-1628127639706.png

(last column)

 

m_refaei_1-1628127677954.png

Please check the adjusted file.

Thank you very much, I can now go forward, with this help. Regards!!!

emf_efab
Frequent Visitor

 I need to calculate the Fecha_Modif as it is shown in the picture...that should be Dia_Sig, not the one I have now in blue. That should be the next (Initial) Date, for the next ID of the same Order (Pedido#). I shouldn't have Blanks!! >> Id 11 should be 01/02/2021, and Id 31 20/02/2021  

 

emf_efab_0-1628094231386.png

 

emf_efab
Frequent Visitor

It doesn't work or I don't understand what you want to do...Which should be the way to attach the report, and excel file. Sorry I am new in the Forum, and don't know to work with it.

 

You may upload the file to your OneDrive or GoogleDrive and share the link here

Mohammad_Refaei
Solution Specialist
Solution Specialist

You can try this calculated column:

Dia_Sig =
CALCULATE (
    MAX ( [Fecha] ),
    FILTER (
        ALL ( MyTable ),
        MyTable[Pedido] = MyTable[Pedido]
            && MyTable[Fecha] > MyTable[Fecha]
    )
)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 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.