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
volfied
Frequent Visitor

Custom Function to run a SQL query a list of servers

I'm wondering if I'm trying to be too cute here.

 

I've built a custom function that accepts three parameters (a server name and a couple arguments) and runs a SQL query against the server passed in via the first parameter. I then import a table of four server names and invoke the custom function on each row to pull in the data for each server. The first row expands fine, but the subsequent rows all return empty tables when I know they shouldn't. Obviously, I could do each of these as hard-coded separate queries, but it feels like this should work. Any thoughts on what I might be doing wrong?

 

 

let data = (prmServer, prmPromos, prmDateFrom) =>

let
    Source = Sql.Database(prmServer, "cis", [Query="<SQL QUERY GOES HERE>")
in
    Source

in data

 

 

 

let
    PromoSource = Excel.CurrentWorkbook(){[Name="tblPromos"]}[Content],
    PromoDelimit = Table.TransformColumns(PromoSource,{{"Promos", each Text.Combine({"'",(Text.Combine({(_),"'"}))})}}),
    PromoCombine = Text.Combine(Table.ToList(PromoDelimit), ","),
    DateSource = Excel.CurrentWorkbook(){[Name="tblDateFrom"]}[Content],
    #"Extracted Date" = Table.TransformColumns(DateSource,{{"DateFrom", DateTime.Date, type date}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Extracted Date",{{"DateFrom", type text}}),
    DateDelimit = Table.TransformColumns(#"Changed Type",{{"DateFrom", each Text.Combine({"'",(Text.Combine({(_),"'"}))})}}),
    DateCombine = Text.Combine(Table.ToList(DateDelimit), ","),
    Source = Excel.CurrentWorkbook(){[Name="tblServers"]}[Content],
    addPromos = Table.AddColumn(Source, "Promos", each PromoCombine),
    addDateFrom = Table.AddColumn(addPromos, "DateFrom", each DateCombine),
    #"Invoked Custom Function" = Table.AddColumn(addDateFrom, "Data", each qryProgs([server], [Promos], [DateFrom])),
    #"Expanded Data" = Table.ExpandTableColumn(#"Invoked Custom Function", "Data", {<COLUMN NAMES GO HERE>})
in
    #"Expanded Data"

 

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@volfied  as far as I know, you can't dynamically pass on a server name and DB name with a parameter. I experimented with this and go thte following error

 

Some data sources may not be listed because of hand-authored queries

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

2 REPLIES 2
smpa01
Super User
Super User

@volfied  as far as I know, you can't dynamically pass on a server name and DB name with a parameter. I experimented with this and go thte following error

 

Some data sources may not be listed because of hand-authored queries

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Bummer. It would have been so elegant. Back to the drawing board. Thank you!

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