Forum Discussion
Using column value as column reference
- 4 years ago
Hi Hambach ,
The missing puzzle is the reference table. Therefore, I mocked up one to show you how the initial provided works.
Reference Table (mock up)
Query Name: ReferenceTbl
Main table (provided sample)
Query Name: MainTbl
Code:
Table.AddColumn(#"Renamed Columns", "Customn Column (the result)", each Table.SelectRows(ReferenceTbl, (x)=> x[Object_ID] = [Object_ID_MainTbl] and x[Object] = [Object_MainTbl])[ReferenceColumn]{0})
ReferenceTbl = Reference Table
x[Object_ID] = [Object_ID_MainTbl] = 1st criteria (x[Object_ID] means each cell under [Object_ID] column from reference table = the value from [Object_ID_MainTbl] from main table.
x[Object] = [Object_MainTbl] = 2nd criteria (x[Object] means each cell under [Object] column from reference table = the value from [ObjectMainTbl] from main table.
[ReferenceColumn] = whatever column you want from the reference table.
*You can add more criteria
The above code returned a single column, whereas the code below returns a table, and you can expand multiple columns.
Table.AddColumn(#"Renamed Columns", "Customn Column (the result)", each Table.SelectRows(ReferenceTbl, (x)=> x[Object_ID] = [Object_ID_MainTbl] and x[Object] = [Object_MainTbl]))
The codes are similar, we dropped "[ReferenceColumn]{0}" from the single column code.
I hope it makes sense and helps.
Regards
KT
Hi Hambach,
It will be much easier If you have the sample data with sensitive information removed and the expected outcome.
I can then provide you step by step to achieve the outcome you want.
Regards
KT
Thank you for your quick response. Unfortunately the whole table (1k+ rows, 200 columns) is sensitive data. So I would prefer to stay with the example if that is ok.
I thought this was a quicker fix than it seems to be. Right now me table is the following with the referencing part from another table already done (I renamed "Output" column in this example since it might be confusing):
| Object_ID | Object | 1 | 2 | 3 | 4 | Referenced from other table | Customn Column (the result) |
| 1 | A | 3 | 7 | 9 | 8 | 2 | |
| 2 | B | 5 | 9 | 4 | 11 | 4 | |
| 3 | C | 9 | 4 | 3 | 4 | 1 |
My hope was that I just could use a simple custom column formula in the final column that would lookup the value in the respective column. So some sort of = [ [ Referenced from other table ] ]. So using the value of the column as the new selected column
Expected Outcome:
| Object_ID | Object | 1 | 2 | 3 | 4 | Referenced from other table | Customn Column (the result) |
| 1 | A | 3 | 7 | 9 | 8 | 2 | 7 |
| 2 | B | 5 | 9 | 4 | 11 | 4 | 11 |
| 3 | C | 9 | 4 | 3 | 4 | 1 | 9 |
The "referenced from other table" column I could just delete afterwards I guess...
Thank you for your help