Power Query to Determine Consecutive and Non-Consecutive Dates

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I'm working with a column called "DateCol Transposed," which has nested table values. The table values contains dates which a process failed for each unique ID (see image below). Note the ID column is not shown in the image.

2.PNG


So as an example, if I expand the "DateCol Transposed" column the table will look like below (I disregard the other columns for now):

IDColumn1Column2Column3Column4Column5Column6
1236/17/20246/19/20246/20/20246/21/20246/24/20246/26/2024
9996/17/2024
8886/18/20246/19/2024
7776/10/20246/12/20246/13/2024

I need to transform the table above (or the nested table values if I do not expand it) to look like the table below using the following rules:

For non-consecutive/single dates (those with no succeeding date), the correction date is the same as the failed date. For consecutive dates (those with succeeding dates), only the first and last dates should be picked. The duration is the difference between the Correction Date and Failed Date.


IDFailed DateCorrection DateDuration (Excluding weekends)
1236/17/20246/17/20241
1236/19/20246/24/20244
1236/26/20246/26/20241
9996/17/20246/17/20241
8886/18/20246/19/20242
7776/10/20246/10/20241
7776/12/20246/13/20242

I cross posted on another forum but didn't get any replies. You can disregard it because the example had the wrong info.


Any help is appreciated. Thanks!
 
What I have done works. There are 2 issues.
1. The first set of data was pivoted but this is not necessary. The sample file you then shared with me without the pivoted data was not the same solution as I previously provided.
2. The second set of data didn't match the first set of data.

I have done it again using your second set of data. Same file
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
What I have done works. There are 2 issues.
1. The first set of data was pivoted but this is not necessary. The sample file you then shared with me without the pivoted data was not the same solution as I previously provided.
2. The second set of data didn't match the first set of data.

I have done it again using your second set of data. Same file
Looks great! Will take a look at your code when I’m in front of my computer. I really appreciate your patience with me. Thank you again
 
Upvote 0
Hi Matt,

I ran into an issue with your solution. Take a look below. ID 777 failed on 6/10, 6/11, and 7/15 (left table) but the output (right table) shows Failed Date 6/10 and Correction Date 7/15. Correction Date should be 6/11 then afterwards a new row should show 7/15 Failed Date and 7/15 Correction Date.

Seems like there is an issue if the ID fails again in different months. Please help.

Example.PNG
 
Upvote 0
Upvote 0
Solution
OK. The logic was a bit hard for me to get my head around late in the day. I had to sleep on it. I think it is correct now, but you know your data better than me.

This is also a good lesson for anyone reading this thread. It is very important to have a set of test data that tests a wide range of scenarios so that you flush out errors early in the process.
Matt,

I was working on it and found another way but your solution is more elegant. I was using your Index trick to get the prior date and use it along with your next date column. Once I had those two dates it could also work with some creative nested IF logic formula. Nonetheless, I like your method with the Date.AddDays function. Thanks a bunch!
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top