Forum Discussion

Pandadev's avatar
Pandadev
Icon for Post Prodigy rankPost Prodigy
5 years ago

R Script to export table in power query not working when I add step to retrieve table data

Hi I have a powerbi report with 85 tables , all pulled in by SQL , I have created 3 merged tables called

QC1 , QC2 and QC3

QC1 is combined with sript below , and the R Script is added , which works great , but the table is emptied after the R Script completes , so I went to the advanced editor and replaced last line #"Run R script" with this code

ReturnValue = #"Changed Type"
in
#"Changed Type"

 

This brings back the data which is used in the report , but the R Script is no longer working , it just seems to get bypassed .

Is there something I have done wrong . I want to export the combined table with selected columns everytime the report is published on a scheduled refresh , to get a historic dataset.

let
Source = Table.Combine({
Table.SelectColumns(#"1", {"QC Check", "Researcher"}),
Table.SelectColumns(#"2", {"QC Check", "Researcher"}),

Table.SelectColumns(#"3", {"QC Check", "Researcher"}),
Table.SelectColumns(#"4", {"QC Check", "Researcher"}),
Table.SelectColumns(#"5", {"QC Check", "Researcher"}),
Table.SelectColumns(#"6", {"QC Check", "Researcher"}),
Table.SelectColumns(#"7", {"QC Check", "Researcher"}),
Table.SelectColumns(#"8", {"QC Check", "Researcher"}),
Table.SelectColumns(#"9", {"QC Check", "Researcher"}),
Table.SelectColumns(#"10", {"QC Check", "Researcher"}),

Table.SelectColumns(#"11", {"QC Check", "Researcher"}),
Table.SelectColumns(#"12", {"QC Check", "Researcher"}),
Table.SelectColumns(#"13", {"QC Check", "Researcher"}),
Table.SelectColumns(#"14", {"QC Check", "Researcher"}),
Table.SelectColumns(#"15", {"QC Check", "Researcher"}),
Table.SelectColumns(#"16", {"QC Check", "Researcher"}),
Table.SelectColumns(#"17", {"QC Check", "Researcher"}),
Table.SelectColumns(#"18", {"QC Check", "Researcher"}),
Table.SelectColumns(#"19", {"QC Check", "Researcher"}),
Table.SelectColumns(#"20", {"QC Check", "Researcher"}),
Table.SelectColumns(#"21", {"QC Check", "Researcher"}),
Table.SelectColumns(#"22", {"QC Check", "Researcher"}),
Table.SelectColumns(#"23", {"QC Check", "Researcher"}),
Table.SelectColumns(#"24", {"QC Check", "Researcher"}),
Table.SelectColumns(#"25", {"QC Check", "Researcher"}),
Table.SelectColumns(#"26", {"QC Check", "Researcher"}),
Table.SelectColumns(#"27", {"QC Check", "Researcher"}),
Table.SelectColumns(#"28", {"QC Check", "Researcher"}),
Table.SelectColumns(#"29", {"QC Check", "Researcher"}),
Table.SelectColumns(#"30", {"QC Check", "Researcher"})}),
#"Grouped Rows" = Table.Group(Source, {"QC Check", "Researcher"}, {{"Rows", each Table.RowCount(_), Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Date", each DateTime.Date(DateTime.LocalNow())),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Date", type text}}),
#"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)##

Determine the user#

(lf)user <- tolower(unname(Sys.info()[""user""]))#(lf)#(lf) #(lf)#(lf)## Determine the address#(lf)

if(user == ""dd""){#(lf) address <- address <- paste0(""C:/Users/"",user,""/OneDrive/Metrics/ddMetrics.csv"")#(lf) #

(lf)} else if(user == ""bb""){#(lf) address <- paste0(""C:/Users/"",user,""/OneDrive/Metrics/bbMetrics.csv"")#(lf) #

(lf)} else if(user == ""ll""){#(lf) address <- paste0(""C:/Users/"",user,""/OneDrive/Metrics/llMetrics.csv"")#(lf) #

(lf)} else if(user == ""kk""){#(lf) address <- address <- paste0(""C:/Users/"",user,""/OneDrive/Metrics/kkMetrics.csv"") #(lf) #

(lf)} else if(user == ""cd""){#(lf) #(lf) #(lf)}#(lf)#(lf) #(lf)#(lf)## Append table#(lf)write.table(dataset, file = address,#(lf) append = T, na = """", row.names = F, col.names = F, quote = F, sep = "","")",[dataset=#"Changed Type"]),
ReturnValue = #"Changed Type"
in
#"Changed Type"

5 Replies

  • ReturnValue = #"Changed Type"
    in
    #"Changed Type"

     

    should be 


    in
    #"Run R Script"

     

    Otherwise you return the data before the R script transform.

    • Pandadev's avatar
      Pandadev
      Icon for Post Prodigy rankPost Prodigy

      when i add that , it works , but the table is empty , and i need to populated to show in th ereport , that is my issue. i want the rscript to run , then to bring back the data in the table to show in the report , on every scheduled refresh

    • Pandadev's avatar
      Pandadev
      Icon for Post Prodigy rankPost Prodigy

      could you please show me hw to add that please

  • Provide a small sample of your data and a simple R script for testing.

     

    (Also, please explain your rationale for using R script for this kind of transform)