cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
justlogmein
Helper III
Helper III

How to replicate this macro in Power Query to fill in missing date using nearest dates?

I have some data with missing values and I need to use the other values to fill them in. It if for oil sampling on a mine site. The people taking the oil samples from the equipment (Unit Number) have missed entering some sites they sampled from, so I am guessing the sites based on what was filled out for the other dates.

 

I would like the rules to be, if the sample site is missing, look for the nearest date for that unit number where the sample was filled out and use that site. If there are two nearest dates, i.e. there was one the day before and one the day after, use site from the latter one.

 

I already have a macro which does this, but I would like to do it in Power Query. Does anyone know how I can go about this? Attached is the Excel file with the macro.

 

Interpolate Example 

 

justlogmein_0-1653716410301.png

 

2 ACCEPTED SOLUTIONS

Then you will have to do add a custom column referring to the unit number above. My video here shows how to reference a previous line. https://youtu.be/xN2IRXQ2CvI



* Matt is a Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

View solution in original post

v-stephen-msft
Community Support
Community Support

Hi @justlogmein ,

 

From my understanding, do you want to group by Unit Number and fill down?

Here's my solution.

1.Group by Unit Number, select All Rows in the opreation.

vstephenmsft_2-1653984949097.png

vstephenmsft_1-1653984940035.png

vstephenmsft_3-1653985135750.png

2.Add a custom column to fill down.

vstephenmsft_5-1653985304874.png

vstephenmsft_6-1653985330323.png

3.Remove the unneeded column and expand the Custom column.

vstephenmsft_7-1653985404653.png

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

View solution in original post

5 REPLIES 5
v-stephen-msft
Community Support
Community Support

Hi @justlogmein ,

 

From my understanding, do you want to group by Unit Number and fill down?

Here's my solution.

1.Group by Unit Number, select All Rows in the opreation.

vstephenmsft_2-1653984949097.png

vstephenmsft_1-1653984940035.png

vstephenmsft_3-1653985135750.png

2.Add a custom column to fill down.

vstephenmsft_5-1653985304874.png

vstephenmsft_6-1653985330323.png

3.Remove the unneeded column and expand the Custom column.

vstephenmsft_7-1653985404653.png

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

Vijay_A_Verma
Super User
Super User

Your rule says - if there are two nearest dates, i.e. there was one the day before and one the day after, use site from the latter one.

Now taking Sample 19 as an example which is for date 1/02/2021....One day prior reading is 5 and one day latter reading is 1. But as per your rule, the latter one needs to be chosen. Hence, it should be filled in with 1 not 5. 

But macro doesn't follow this rule. Macro is filling in this with prior date only not latter date. 

 

However, macro is following the first rule i.e. if the sample site is missing, look for the nearest date for that unit number where the sample was filled out and use that site. 

The simplest approach is to fill the value above, down. Make sure the blanks are nulls and not blanks. They should say "null". If they don't say null, select the column and do a replace values and replace an empty string "" with nothing (leave it empty). Then fill down. 



* Matt is a Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

It has to be specific to the Unit Number though, and sometimes there are many missing values in a row and it is more accurate to use the site from the closest date, rather than the last date.

Then you will have to do add a custom column referring to the unit number above. My video here shows how to reference a previous line. https://youtu.be/xN2IRXQ2CvI



* Matt is a Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

Helpful resources

Announcements
September Update

Check it Out!

Click here to learn more about the September 2022 updates!

Power BI Show Episode 10 Recap

The Power BI Community Show

Watch the playback when Amit Chandak, a Power BI Super User, demos how to use Field Parameters to make reports more dynamic.

Power BI Dev Camp Session 26

New Date - Check it Out!

Mark your calendars and join us on Thursday, October 6 at 11a PDT for a great session with Ted Pattison!

Health and Life Sciences Power BI User Group

Health and Life Sciences Power BI User Group

Power BI specialists at Microsoft have created a community user group where customers in the provider, payor, pharma, health solutions, and life science industries can collaborate.

Top Kudoed Authors