Microsoft Fabric Community Conference 2025, March 31 - April 2, Las Vegas, Nevada. Use code FABINSIDER for a $400 discount.
Register nowGet inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.
Hi there. I may not be doing this the best way, so I wanted to get some feedback on a potential better way to do this. Essentially, I have a spreadsheet that gets downloaded from an external website. It includes customer IDs in our database. I want to place those customer IDs into a SQL Query within an in-statement. Example "WHERE CustomerID in (ID1, ID2, ID2, and so forth)." Since the in-statement required commas between them, what I'm currently doing is using the following Excel formula to grab the customer IDs from the column, concatenate them with a comma, and return that to a cell. Formula is =TEXTJOIN(",",TRUE,E2:E100487).
I then use power query to grab that cell as a reference called CustIDs and, and put it in the SQL Statement that runs in power query.
This works great up to a certain amount of Customer IDs. About 120. After that, there's a limit to the number of characters that can go into a cell. So, this works well for small daily batches, but I'd like to be able to run this for larger batches.
So, is there a way in power query to grab the contents of a column, concatenate them with commas, and then assign them to a variable to put in my query like above?
OP, did you ever find a solution? I'm trying to do the same thing.
Yes, there is a way to grab the contents of a column in Power Query, concatenate them with commas, and assign them to a variable to use in your query. Here's how you can do it:
Now that you have the customer IDs concatenated with commas in a table, you can create a new query that references this table and uses the customer IDs in your SQL query. Here's an example:
Now, you can reference the "CustomerIDs" column in your SQL query by using the following syntax: WHERE CustomerID in ( $(CustomerIDs) ). Make sure to replace "CustomerIDs" with the name of your custom column. When you run the query, Power Query will replace "$(CustomerIDs)" with the concatenated customer IDs from the table, allowing you to use as many customer IDs as you need without hitting the character limit of a cell.
Not 100% sure what you are asking, but you can take a list of text values from a column, concatenate them, and assign to a variable.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUcpKTUtTitWJVkoCcnITi7LBnGQIpxLMSQFyiktSy1KVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Name = _t]),
AllNames =
Table.AddColumn(
Source,
"All Names",
each
let
varAllNames = Text.Combine(Source[Name], ", ")
in
varAllNames
)
in
AllNames
So the varAllNames variable can be returned or further acted upon by other functions.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingThanks for the quick response. Sorry if my question wasn't very clear. I'm very new to power query and M Code. Basically, I need the numbers in the column called Reference Id turned into a comma-separated variable that I can then reference in a SQL Query.
Here is my M Code thus far:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Removed Columns" = Table.RemoveColumns(Source,{"Review Id", "Review Consumer User Id", "Review Username", "Review Title", "Review Content", "Company Response", "Company Response Author", "Review Language", "Domain Url", "Webshop Name", "Business Unit Id", "Tags", "Company Reply Date (UTC)", "Location Name", "Location Id"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Reference Id] <> null)),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Reference Id", Int64.Type}}),
#"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type", {"Reference Id"}),
#"Added Conditional Column" = Table.AddColumn(#"Removed Errors", "5 Star Count", each if [Review Stars] = 5 then 1 else 0),
#"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Survay Count", each if [Review Stars] <= 5 then 1 else 0),
#"Added Conditional Column2" = Table.AddColumn(#"Added Conditional Column1", "1 Stars", each if [Review Stars] = 1 then 1 else 0)
in
#"Added Conditional Column2"
that I can then place into here where it currently references &CustIDs& in the SQL Query:
let
Source = Sql.Database("PagListener02", "CustSrv", [Query="SELECT C.CustomerID,
CT.CreateUser
FROM Customer C
INNER JOIN CustomerTransaction CT ON C.CustomerID = CT.CustomerID
WHERE CT.TransactionCode = 'Install'
And CT.StatusCode <>'c'
And C.CustomerID in("&CustIDs&")"]),
#"Lowercased Text" = Table.TransformColumns(Source,{{"CreateUser", Text.Lower, type text}}),
#"Merged Queries" = Table.NestedJoin(#"Lowercased Text", {"CreateUser"}, Roster1, {"Agent"}, "Roster1", JoinKind.RightOuter),
#"Expanded Roster1" = Table.ExpandTableColumn(#"Merged Queries", "Roster1", {"Name", "Supervisor"}, {"Roster1.Name", "Roster1.Supervisor"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Roster1",{"CustomerID", "Roster1.Name", "Roster1.Supervisor", "CreateUser"}),
#"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Roster1.Name", "Consultant"}}),
#"Reordered Columns1" = Table.ReorderColumns(#"Renamed Columns",{"CustomerID", "Consultant", "CreateUser", "Roster1.Supervisor"}),
#"Renamed Columns1" = Table.RenameColumns(#"Reordered Columns1",{{"CreateUser", "User ID"}, {"Roster1.Supervisor", "Team"}}),
#"Merged Queries1" = Table.NestedJoin(#"Renamed Columns1", {"CustomerID"}, #"Data Output", {"Reference Id"}, "5 Stars", JoinKind.LeftOuter),
#"Expanded 5 Stars" = Table.ExpandTableColumn(#"Merged Queries1", "5 Stars", {"1 Stars", "5 Star Count", "Review Stars", "Survay Count"}, {"1 Stars", "5 Star Count", "Review Stars", "Survay Count"}),
#"Grouped Rows" = Table.Group(#"Expanded 5 Stars", {"Consultant", "User ID", "Team"}, {{"5 Stars", each List.Sum([5 Star Count]), type number}, {"Survays", each List.Sum([Survay Count]), type number}, {"1 Stars", each List.Sum([1 Stars]), type number}, {"Total", each List.Sum([Review Stars]), type number}}),
#"Reordered Columns2" = Table.ReorderColumns(#"Grouped Rows",{"Consultant", "User ID", "Team", "5 Stars", "1 Stars", "Total", "Survays"}),
#"Added Custom" = Table.AddColumn(#"Reordered Columns2", "Trust Pilot Average", each [Total]/[Survays])
in
#"Added Custom"
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!
Check out the February 2025 Power BI update to learn about new features.
User | Count |
---|---|
31 | |
31 | |
22 | |
16 | |
12 |
User | Count |
---|---|
21 | |
20 | |
16 | |
11 | |
10 |