Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
rlu
Frequent Visitor

zoo's na.locf function not filling fields with R script in Power Query Editor

I'm trying to use the zoo package's na.locf() function to fill missing values in adjacent rows. It works when I run it in RStudio, but it does not work in PowerBI. Here's an abridged version of the R code:

 

library(dplyr)
library(zoo)
library(lubridate)

fillGuids <- clean %>%
    arrange(User, Serial, desc(ClientTime_clean), MetaGuid, desc(Action)) %>%
    group_by(User,Serial,date(ClientTime_clean)) %>%
    mutate(MetaGuid_filled = zoo::na.locf(MetaGuid, na.rm=FALSE)) %>% 
    ungroup() %>% #fill all missing MetaGuids that come after first log by User/Serial/date

 

This is the desired output for the MetaGuid_filled field, derived from the MetaGuid field. However, the output from PowerBI is just what's in MetaGuid. No errors are thrown, but this is not the expected behavior of na.locf. Not to mention the exact same code works when using my RStudio IDE.

 

MetaGuid_filledMetaGuidUserSerialActionClientTime_clean
A123456A123456A1ViewDocument10/22/2019
A123456A123456A1ViewDocument10/22/2019
A123456A123456A1ViewDocument10/22/2019
A123456A123456A1ViewDocument10/22/2019
A123456 A1ExecuteSearch10/22/2019
B987654B987654A1ViewDocument10/22/2019
B987654B987654A1ViewDocument10/22/2019
B987654 A1ExecuteSearch10/22/2019
3 REPLIES 3
rlu
Frequent Visitor

any update?

dax
Community Support
Community Support

Hi rlu,

I am not familiar on R script, I will discuss this with outhers and try to reproduce this in my environment. I will inform you as soon as I get it 

In addition, if you want to fill it up, why not use M code to achieve this goal

You could replace empty cell by null, then click Fill->down

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQ0MjYxNVPSUXIEYkMgDstMLXfJTy7NTc0rAQkZ6BsZ6RsZGFoqxeoMPvVICl0rUpNLS1KDUxOLkjMwVTpZWpibmZoQbTKp6ol0SSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [MetaGuid = _t, User = _t, Serial = _t, Action = _t, ClientTime_clean = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"MetaGuid"}),
    #"Filled Down" = Table.FillDown(#"Replaced Value",{"MetaGuid"})
in
    #"Filled Down"

441.PNG

Thanks for your understanding and support.

Best Regards,
Zoe Zhi

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

 

rlu
Frequent Visitor

Thanks for your response Zoe. I'm looking to maintain just one data munging pipeline as I use the same R script for offline analysis as well and wanted to use the same script to turn on a PowerBI dashboard to monitor metrics. Thus, M code will not be beneficial for me.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.