Power Query - finding the last non blank value in a row

batdog

New Member
Joined
Oct 14, 2005
Messages
1
I have a table which mainly consists of a series of dates. One of the columns will always have an initial start date. The following 52 columns (nothing to do with the number of weeks in the year) may contain dates subsequent to the initial one. The dates will be populated from left to right (i.e. no gaps between dates). Some rows may not have any further dates after the first one; other rows could have any number of additional dates, but it will vary from row to row.

Example:

Row 1 - Company A | Start Date | Date 1 | Date 2 | Date 3 | Date 4
Row 2 - Company B | Start Date | Date 1
Row 3 - Company B | Start Date
Row 4 - Company B | Start Date | Date 1 | Date 2 | Date 3 | Date 4 | Date 5 | Date 6 | Date 7

What I want to do is find the duration in days between the start date and the most recent date for each row. I know how to find the duration between two dates in Power Query using Duration.Days, but only for fixed columns, so I'm initially trying to add a column at the end which effectively looks to the left in each row to identify the first non-null value (which should be the most recent date entered for that row). I can then perform a duration calculation between this and the initial date.

In the example above I would expect to get back the values Date 4, Date 1, Start Date and Date 7 for rows 1 to 4 respectively.

Any suggestions gratefully received.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You don't need to add a last non-null column, rather just add a duration column like this

Power Query:
= Table.AddColumn(PreviousStepName, "Duration", each let LastDate = List.Last(List.RemoveNulls(Record.ToList(_))) in Duration.Days(LastDate - [Start Date] ))

change PreviousStepName to the correct step name in your query. This assumes the start column is named "Start Date", if not then change [Start Date] to the correct column name.
 
Upvote 0

Forum statistics

Threads
1,223,670
Messages
6,173,722
Members
452,528
Latest member
ThomasE

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