Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Referencing columns from tables in Query Editor

Good morning,   I have two tables I've pulled through to Power BI from SQL servers. The Table names are "QueryCMWritten" and "QueryPM Written" I want to create a new table, pulling through some co...
  • v-piga-msft's avatar
    7 years ago

    Hi Anonymous,

     

    By my tests, you could use Table.SelectColumns() fucntion to get the columns from another table.

     

    Here is my test sample.  

     

    Assuming that I have two tables Sheet1 and Sheet6, then I create the blank query and type the query below.

     

     

    let
        Source = Table.SelectColumns(Sheet6,"PostingYear"),
    in
        Source

    Then I will get the column [Posting Year] from table Sheet 6. Then I could create the custom column with the same query.

     

     

     

    let
        Source = Table.SelectColumns(Sheet6,"PostingYear"),
        #"Added Custom" = Table.AddColumn(Source, "Custom", each Table.SelectColumns(Sheet1,"TYPE")),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"TYPE"}, {"Custom.TYPE"})
    in
        #"Expanded Custom"

     

     

    The query above should make sense of you. I'm not good at M query.

     

    Hope ImkeF would help you get the solution more efficient.

     

    Best  Regard,

    Cherry

  • ImkeF's avatar
    ImkeF
    7 years ago

    Usually, I would merge 2 tables via a common key-column to make sure that each row has data that belongs together (see here for example: https://www.powerquery.training/portfolio/merge-tables/).

     

    But if you really just want to put one table beneath the other, you'd use Table.ToColumns on each of those tables and create a joined table using Table.FromColumns(TransformedFirstTable & TransformedSecondTable)