Forum Discussion

justlogmein's avatar
justlogmein
Helper III
4 years ago
Solved

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 

 

 

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    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.

    2.Add a custom column to fill down.

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

     

     

    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.

     

     

     

     

  • MattAllington's avatar
    MattAllington
    Community Champion

    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. 

    • justlogmein's avatar
      justlogmein
      Helper III

      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.

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    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.