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
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 an 8 times 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 an 8 times 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 an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

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.

Top Solution Authors
Top Kudoed Authors