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
Anonymous
Not applicable

make a function to getdata from different databases

Hi,

 

i have an SQL database with several "customer" tables over different databases. Of course i can load them seperately and with append-queryies merge them in a new table.

 

Now i want to be more flexible because i have over the 15 databases.

 

The different names of the databases can i save in new table (in my example 001, 002, 003 etc) . After that i want to invoke a funtion that for each database load the "customer" table and merge it in a new table.

let
    Source = Sql.Databases("Server"),
    #"001" = Source{[Name="001"]}[Data],
    customer1 = #"001"{[Schema="dbo",Item="customer"]}[Data],
in
    customer1
- - - - - - - - - - - - -
let
    Source = Sql.Databases("Server"),
    #"002" = Source{[Name="002"]}[Data],
    customer2 = #"002"{[Schema="dbo",Item="customer"]}[Data],
in
    customer2

 

Can someone help me make this function and how to implementate this?

With kind regards

 

2 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

Create your own function that uses the table as parameter.

I will not be able to post here the complete function, but I will put down an example how to achieve this

function could look like:

(customertable) =>
let
    Source = Sql.Databases("Server"),
    #"001" = Source{[Name=customertable]}[Data],
    customer = #"001"{[Schema="dbo",Item="customer"]}[Data],
in
    customer

 

then create a table and add new column to invoke the function (code is not tested)

let
    Table = #table({"Customer""}, {{"001"},{"002"},{"003"}},
    AddColumn = Table.AddColumn(Table,"SQL",each YourFunctionName([Customer]))
in
    AddColumn

 

If this post helps or solves your problem, please mark it as solution.
Kudos are nice to - thanks
Have fun

Jimmy

 

View solution in original post

Anonymous
Not applicable

this was the solution:

 

(Databasenaam, Tabelnaam) =>
let
    Bron = Sql.Databases(Server),
    Database = Bron{[Name=Databasenaam]}[Data],
    Tabel = Database{[Schema="dbo", Item=Tabelnaam]}[Data]
in
    Tabel

 

let
    Table = Databases,
    Records = Table.AddColumn(Table, "Records", each HaalTabelOpUitDB([Databasenaam], "bedryf"))
in
    Records

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

this was the solution:

 

(Databasenaam, Tabelnaam) =>
let
    Bron = Sql.Databases(Server),
    Database = Bron{[Name=Databasenaam]}[Data],
    Tabel = Database{[Schema="dbo", Item=Tabelnaam]}[Data]
in
    Tabel

 

let
    Table = Databases,
    Records = Table.AddColumn(Table, "Records", each HaalTabelOpUitDB([Databasenaam], "bedryf"))
in
    Records

 

Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

were you able to solve the problem with any reply given?

If so, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

All the best

Jimmy

Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

Create your own function that uses the table as parameter.

I will not be able to post here the complete function, but I will put down an example how to achieve this

function could look like:

(customertable) =>
let
    Source = Sql.Databases("Server"),
    #"001" = Source{[Name=customertable]}[Data],
    customer = #"001"{[Schema="dbo",Item="customer"]}[Data],
in
    customer

 

then create a table and add new column to invoke the function (code is not tested)

let
    Table = #table({"Customer""}, {{"001"},{"002"},{"003"}},
    AddColumn = Table.AddColumn(Table,"SQL",each YourFunctionName([Customer]))
in
    AddColumn

 

If this post helps or solves your problem, please mark it as solution.
Kudos are nice to - thanks
Have fun

Jimmy

 

Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

The below syntax will list all the database, then you will be able to reference this as a connection and expand all the tables you need and append together.

let
    Source = Sql.Databases("Server") 
in
    Source

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

 

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