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
NH
Advocate II
Advocate II

Help Need to find Day delta in either Power Query or DAX formuale

Hi

 

I've a shipment data which need to find the day different for a given same shipment ID where  its Shipment date changes.

In Excel I'm using this formaule the get the result but like to find out how do it in Power BI. Can anyone help to advise. Thanks in advance.

 

Shipment IDShipment Date ChangeDay_delta ResultForumale in ExcelPurpose
8539/12/20181IFERROR(DAYS360(B2,VLOOKUP(A2,A3:B49981,2,FALSE)),"NA")Find the Day different for a given Shipmen ID
8539/13/2018NAIFERROR(DAYS360(B3,VLOOKUP(A3,A4:B49982,2,FALSE)),"NA") 
2989/18/2018NAIFERROR(DAYS360(B4,VLOOKUP(A4,A5:B49983,2,FALSE)),"NA") 
4509/18/2018-1IFERROR(DAYS360(B5,VLOOKUP(A5,A6:B49984,2,FALSE)),"NA") 
4509/17/2018NAIFERROR(DAYS360(B6,VLOOKUP(A6,A7:B49985,2,FALSE)),"NA") 
4789/15/2018-1  
4789/14/2018NA  
6669/19/20181  
6669/20/2018NA  
6049/21/20181  
6049/22/2018NA  
5259/20/2018-2IFERROR(DAYS360(B13,VLOOKUP(A13,A14:B49992,2,FALSE)),"NA") 
5259/18/2018NA  
3429/17/20183  
3429/20/2018NA  
3449/17/20183  
3449/20/2018NA  

 

Best Regards,

NH

1 ACCEPTED SOLUTION

Hi @NH,

I didn't realize there could be more than 2 changes.

In this case, this should work (assuming that your data is arranged in order by shipment IDs, like in your example)

 

 

Day_delta = 
IF([Index] <> MAXX(FILTER(Table1, [Shipment ID] = EARLIER([Shipment ID])), [Index]), 
DATEDIFF(
    MAXX(
        FILTER(Table1,  
                [Index] = EARLIER([Index])), 
        [Shipment Date Change]), 
    MAXX(
        FILTER(Table1,  
                [Index] = EARLIER([Index]) + 1), 
        [Shipment Date Change]), 
    DAY))

 

The results look like this:

examp.PNG

Explanation:

For all rows where the Index is not the maximum index for the shipment ID (meaning, not the last index of a shimpent id), I use the DATEDIFF by DAY, with the following variables:

- the date with the current index

- the date with the next index

View solution in original post

7 REPLIES 7
ofirk
Resolver II
Resolver II

Hi,

I'll assume you have some column that contains the original shipment date.

How about creating the following column:

Day Delta Result = DATEDIFF([Original Shipment Date], [Shipment Date Change], DAY)

Hi Ofirk,

 

The original shipment date will be in the same column as shipment date change.  That why need know how to resolve this in Power Bi.  Thanks you.

Hi @NH,

This solution is a bit messy but it works:

 

1. I added to the data a [Line Number] column based on the order the data is inserted to the table (because from your data sample it appears that the first of 2 shipment ids is always the planned date and the second is the actual)

 

2. I created a Day_delta column with the following calculation:

Day_delta = 
IF([Line Number] = MINX(FILTER(Table1, [Shipment ID] = EARLIER([Shipment ID])), [Line Number]), 
DATEDIFF(
    MAXX(
        FILTER(Table1, 
                [Shipment ID] = EARLIER([Shipment ID]) && 
                [Line Number] = MINX(
                                    FILTER(Table1, 
                                            [Shipment ID] = EARLIER([Shipment ID])), 
                                    [Line Number])), 
        [Shipment Date Change]), 
    MAXX(
        FILTER(Table1, 
                [Shipment ID] = EARLIER([Shipment ID]) && 
                [Line Number] = MAXX(
                                    FILTER(Table1, 
                                            [Shipment ID] = EARLIER([Shipment ID])), 
                                    [Line Number])), 
        [Shipment Date Change]), 
    DAY))

For a sample of your data I get the following results:

 

ex.PNG

 

If you don't want to get the result 0 for Shipment ID 298, change the condition to:

 

IF([Line Number] <> MAXX(FILTER(Table1, [Shipment ID] = EARLIER([Shipment ID])), [Line Number]), 

 

Hi Ofirk,

 

I've found out why there was result was not tally. Because some of the shipment ID has more than 2 shipment dates changes.

The DAX code that you provided was work only if the shipment ID has 2 shipemet dates changes or less.

Please refer to table below which shown the issues. Possible to advise how to fix the below issue? Thanks you.

IndexShipment IDShipment Date ChangeDay_delta in DAXCorrect Result should be
085309/12/1861
185309/13/1865
285309/18/18NANA
345009/18/18-4-1
445009/17/18-4-2
545009/15/18-4-1
645009/14/18NANA

Hi @NH,

I didn't realize there could be more than 2 changes.

In this case, this should work (assuming that your data is arranged in order by shipment IDs, like in your example)

 

 

Day_delta = 
IF([Index] <> MAXX(FILTER(Table1, [Shipment ID] = EARLIER([Shipment ID])), [Index]), 
DATEDIFF(
    MAXX(
        FILTER(Table1,  
                [Index] = EARLIER([Index])), 
        [Shipment Date Change]), 
    MAXX(
        FILTER(Table1,  
                [Index] = EARLIER([Index]) + 1), 
        [Shipment Date Change]), 
    DAY))

 

The results look like this:

examp.PNG

Explanation:

For all rows where the Index is not the maximum index for the shipment ID (meaning, not the last index of a shimpent id), I use the DATEDIFF by DAY, with the following variables:

- the date with the current index

- the date with the next index

Hi Ofirk,

 

The updated Dax code is working. Thanks you every much.

 

Best regards,

NH

Hi Ofirk,

 

I've tried your solution and the result was very very close to the result in Excel. I'm still trying to unerstand your DAX code and also trying to find out why some row the Day delta was different from the result in Excel.

 

Thanks for your help. Cheers.

 

Regards,

NH

 

 

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.