+ Reply to Thread
Results 1 to 19 of 19

Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

  1. #1
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    Hi all,

    I'm still fairly new to Power Query and am attempting to do a lookup based on a Column's Header in another table. I know how to use List.PositionOf to find the row for a specific value in a column in another table, but I'm not sure how to limit this to just the column's header itself rather than the individual values in that column. For instance, if I have a Data table with column headers Column1, Column2, and Column3 with n rows of data and another Lookup table with columns Header and Letter where the three row contain values Column1 | A, Column2 | B, and Column3 | C I would like to be able to look up the header names from Data in column Header and return the Letter from Lookup.

    I cannot post an example file currently. Please let me know if you require any clarification.

    Is this possible?

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,722

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    We are you unab;e to post a sample workbook?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    I am working from the office and am unable to attach files from our network. I've attempted to attach an image to this reply.

    PQ Example.PNG

  4. #4
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    as you can see this is possible

    Column1 Column2 Column3 Column4 Column5 Header Letter
    3
    9
    6
    7
    5
    Column1 A
    2
    3
    10
    3
    10
    Column2 B
    8
    4
    1
    1
    3
    Column3 C
    Column4 D
    Column5 E
    Column1 Column2 Column3 Column4 Column5
    A B C D E
    A B C D E
    A B C D E


    unpivot first table , merge with second by columns, expand, remove unnecessary columns, group, add custom column with list, extract values, remove column with tables, pivot header by custom, then split each column by delimiter and use Table.FirstN()

    there is a simpler way but have no time, sorry

  5. #5
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    Thank you, Sandy. If anyone has a simpler way, preferably without merging tables, I would greatly appreciate it. Leaving the question open for now.

  6. #6
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    sure, with table2 only
    Please Login or Register  to view this content.
    Header Letter Column1 Column2 Column3 Column4 Column5
    Column1 A A B C D E
    Column2 B A B C D E
    Column3 C A B C D E
    Column4 D
    Column5 E


    next time if you posting a picture turn off filter buttons !!! but usually we need excel file
    Last edited by sandy666; 10-24-2022 at 12:14 PM.

  7. #7
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    Hi Sandy, thank you again for your time. I believe I may have not communicated myself effectively. I am not trying to convert the Lookup table into the format of the Data table, I am trying to look up the Column headers from the Data table in the Lookup table and return the Letter in each cell of the Data table. Is this possible?

  8. #8
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    maybe easier will be with formula(s) but this is not my melody

    btw. LOOKUP in PQ is just Merge

  9. #9
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    I have been able to perform lookups using List.PositionOf. If there was only some way to refer the Column Header of each cell I believe this could be performed quite easily, I'm just not sure how to do this/if it's possible.

  10. #10
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    so try try and try then you will know

    ok, have a nice day

  11. #11
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    I don't need any List.PositionOf because simple Append feature (works like LOOKUP) will do that for me, matches the appropriate columns in one table with the corresponding columns in the other table

    Please Login or Register  to view this content.
    Last edited by sandy666; 10-24-2022 at 01:46 PM. Reason: M added

  12. #12
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    I found a simple way to return the columns' header:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Where i is the column index. Combining this result with List.PositionOf can return the desired value from the Lookup table.

  13. #13
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    post a whole M code after finish

  14. #14
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    It's slightly messy but here is the final applied step:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    The goal was to look up the number of Hours in the Lookup table based on the column header 1/1/2022 and divide the value in the cell by the returned number of hours. If anyone knows of a way to simplify, I'd appreciate it.

  15. #15
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    first read big yellow banner at the top of this site and do this because your picture has nothing to do with the last post #14

  16. #16
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    Attaching example file with solution.
    Attached Files Attached Files

  17. #17
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    date is hardcoded, if it will be changed in the sources you'll see something like that

    experr.png

  18. #18
    Registered User
    Join Date
    01-18-2021
    Location
    california, united states
    MS-Off Ver
    MS365 (PC) Version 2209
    Posts
    39

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    Hi Sandy, unfortunately I still have to work some of the details out. I didn't want to ask another question in the middle of a post that was originally just trying to find a way to return a column header. I am hoping to eventually find a solution that would iterate through all "dated" columns dynamically by their index number to account for varying amounts of columns and varying column names with just one transformation; a way to loop through column indexes 2-12. I don't have any experience writing loops in M and don't know the syntax. If you're up for it, any help would be appreciated. Otherwise I can create a new thread.

  19. #19
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Power Query: Lookup Column Headers in Another Table, Return Corresponding Column Value

    don't create a new thread because the problem remains the same, imho will be better if you stay here. I will be watching with interest how you handle it

    maybe try List.Generate

    Please Login or Register  to view this content.
    Last edited by sandy666; 10-24-2022 at 10:22 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2022, 10:35 AM
  2. Power Query - Add another column to Table.TransformColumns
    By signup1 in forum Excel Charting & Pivots
    Replies: 15
    Last Post: 08-27-2021, 01:31 AM
  3. [SOLVED] Power Query and imported table headers
    By Tresfjording in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-18-2019, 06:38 PM
  4. [SOLVED] Variable Column Headers in or attached to a Power Pivot Table
    By RAdams1 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 10-25-2017, 11:29 AM
  5. [SOLVED] Lookup (index/match) and return column headers
    By Barslund in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-08-2017, 06:01 AM
  6. Power Query - add column with Table name
    By zico8 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 05-11-2017, 12:58 AM
  7. Return closest value less than given number matching another column - Power Query ?
    By ChipsSlave in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-29-2017, 03:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1