cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Josh_BI_UK Regular Visitor
Regular Visitor

Reference two or more Columns as Source in new query

Hi PWRBI family,

 

How do you references two columns from a table in another query as the source for a new query using Power BI - Query editor?

 

 

Example

 

Query A > Table called: "Tbl_CoursesbyTutorTable", Columns needed called: "Course Codes" and "Course Title"

 

New Query (B) > References only columns "Course Codes" and "Course Title" from Query A.

 

I don't want to use the Reference shortcut function or Duplicate the original query.

 

I would like to simply create a new query using M (Advanced Editor), and have some code similar to this, but for two columns, instead of just one:

 

 

 

let
    Source = Tbl_CoursesbyTutorTable[Course Title]
in
    Source

 

Mega appreciate the help in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Reference two or more Columns as Source in new query

I speak from personal experience - if there is difference in performance I don't think it will be visible in most cases
I do prefer the clarity of the code in my syntax, it's quite straightforward so in case you need to handover it shouldn't be too complex

as for learning you can give this a go
https://msdn.microsoft.com/en-us/query-bi/m/power-query-m-language-specification

a bit tough read, but gives much better understanding of M

EDIT
you can even simplify it more

let
    Source = Table.SelectColumns(Tbl_CoursesbyTutorTable,{"Course Codes", "Course Title"})
in
    Source

but I actually prefer to have the steps separated

6 REPLIES 6
Highlighted
Josh_BI_UK Regular Visitor
Regular Visitor

Re: Reference two or more Columns as Source in new query

Hi all, had a quick mess about and got something to work. I referenced one column using:

 let
    #"QueryA_Col_1" = Tbl_CoursesbyTutorTable[Course Title]
in
    #"QueryA_Col_1"

 then I edited the query and added a custom column which used my 2nd column as a source.

 Code I used:

 

let
    #"QueryA_Col_1" = Tbl_CoursesbyTutorTable[Course Title],
    #"Converted to Table" = Table.FromList(#"QueryA_Col_1", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"QueryA_Col_2" = Table.AddColumn(#"Converted to Table", "Course", each Tbl_CoursesbyTutorTable[Course Codes]),
    #"Expanded Course Codes Col" = Table.ExpandListColumn(#"QueryA_Col_2", "Course Codes")

in
    #"Expanded Course Codes Col"

 

Is there a cleaner / slimier code I could use? My code worked but looks clumsy.

Super User
Super User

Re: Reference two or more Columns as Source in new query

I'd reference the whole table, and then select only valid columns in next step

let
    Source = Tbl_CoursesbyTutorTable,
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Course Codes", "Course Title"})
in
    #"Removed Other Columns"
Josh_BI_UK Regular Visitor
Regular Visitor

Re: Reference two or more Columns as Source in new query

Hi @Stachu,

 

Thank you for sharing. Happy to go with your suggestion, if there is a reason why it is a better approach e.g. speed of processing, best practice, your experience etc.

 

Could you elaborate, so this post acts as a learning tool also? Any blogs mentioning the topics would also be fab.

 

 

Super User
Super User

Re: Reference two or more Columns as Source in new query

I speak from personal experience - if there is difference in performance I don't think it will be visible in most cases
I do prefer the clarity of the code in my syntax, it's quite straightforward so in case you need to handover it shouldn't be too complex

as for learning you can give this a go
https://msdn.microsoft.com/en-us/query-bi/m/power-query-m-language-specification

a bit tough read, but gives much better understanding of M

EDIT
you can even simplify it more

let
    Source = Table.SelectColumns(Tbl_CoursesbyTutorTable,{"Course Codes", "Course Title"})
in
    Source

but I actually prefer to have the steps separated

Josh_BI_UK Regular Visitor
Regular Visitor

Re: Reference two or more Columns as Source in new query

Thank you @Stachu , this works like a charm. I tried to apply the "{ }" curly brackets methodology, before your post but got the syntax wrong. Thank you, your code clears it up for me.Smiley Happy

Super User
Super User

Re: Reference two or more Columns as Source in new query

{} is the syntax for list e.g. {1..10} creates list from 1 to 10, while {"a","x","c"} is just hardcoded one
in the syntax i posted, if one of the preceding steps would contain the column names you could reference that list in Table.SelectColumns
I use it sometimes for dynamic column selection