Code Generator | M (Power Query)
getMatchedValue Function
This function returns a value from a column based on a match to another column in the same row in another table. It takes input parameters such as source table, source column, match column, match value, result table, and result column. The function
![Empty image or helper icon](https://fs.enterprisedna.co/library/data-mentor/cover-images/a3IAeukQg0Yiya5x5GPZFpUkSxV9nzR2yDXSGXwY.webp)
Prompt
Answer
// Function: getMatchedValue
// Purpose: Returns a value from a column based on a match to another column in the same row in another table
// Parameters:
// sourceTable: table - The table containing the source column
// sourceColumn: text - The name of the column from which to retrieve the value
// matchColumn: text - The name of the column used for matching
// matchValue: any - The value to match in the matchColumn
// resultTable: table - The table containing the result column
// resultColumn: text - The name of the column to retrieve the value from
// Return Type: any - The value from the resultColumn matching the matchValue in the matchColumn
(getMatchedValue as function) =>
(sourceTable as table, sourceColumn as text, matchColumn as text, matchValue as any, resultTable as table, resultColumn as text) as any =>
let
// Filter the result table based on the matchValue in the matchColumn
filteredResult = Table.SelectRows(resultTable, each _[matchColumn] = matchValue),
// Get the value from the resultColumn where the match occurred
matchedValue = filteredResult{0}[resultColumn]
in
matchedValue
Example Usage:
let
sourceTable = #table({"ID", "Name"}, {{1, "John"}, {2, "Alice"}}),
resultTable = #table({"ID", "Age"}, {{1, 30}, {2, 25}}),
matchedValue = getMatchedValue(sourceTable, "Name", "ID", 1, resultTable, "Age")
in
matchedValue
In this example, the getMatchedValue function retrieves the Age corresponding to the Name "John" from the sourceTable and resultTable.
Description
This function returns a value from a column based on a match to another column in the same row in another table. It takes input parameters such as source table, source column, match column, match value, result table, and result column. The function filters the result table based on the match value and returns the value from the result column where the match occurred. An example usage is provided to illustrate its functionality.
More Code Generators
![Apache Flink Code Generator](https://fs.enterprisedna.co/library/icons/flink.png)
![Apache Pig Code Generator](https://fs.enterprisedna.co/library/icons/apache_pig.png)
![Azure Data Factory Code Generator](https://fs.enterprisedna.co/library/icons/azure-data-factory.png)
![C/C++ Code Generator](https://fs.enterprisedna.co/library/icons/c_c_plus.png)
![CouchDB Code Generator](https://fs.enterprisedna.co/library/icons/couchdb.png)
![DAX Code Generator](https://fs.enterprisedna.co/library/icons/power-bi.png)
![Excel Code Generator](https://fs.enterprisedna.co/library/icons/excel.png)
![Firebase Code Generator](https://fs.enterprisedna.co/library/icons/firebase.png)
![Google BigQuery Code Generator](https://fs.enterprisedna.co/library/icons/google_bigquery.png)
![Google Sheets Code Generator](https://fs.enterprisedna.co/library/icons/google_sheets.png)
![GraphQL Code Generator](https://fs.enterprisedna.co/library/icons/graphql.png)
![Hive Code Generator](https://fs.enterprisedna.co/library/icons/hive.png)
![Java Code Generator](https://fs.enterprisedna.co/library/icons/java.png)
![JavaScript Code Generator](https://fs.enterprisedna.co/library/icons/javascript.png)
![Julia Code Generator](https://fs.enterprisedna.co/library/icons/julia.png)
![Lua Code Generator](https://fs.enterprisedna.co/library/icons/lua.png)
![M (Power Query) Code Generator](https://fs.enterprisedna.co/library/icons/m.png)
![MATLAB Code Generator](https://fs.enterprisedna.co/library/icons/matlab.png)
![MongoDB Code Generator](https://fs.enterprisedna.co/library/icons/mongodb.png)
![Oracle Code Generator](https://fs.enterprisedna.co/library/icons/oracle.png)
![PostgreSQL Code Generator](https://fs.enterprisedna.co/library/icons/postgresql.png)
![Power BI Code Generator](https://fs.enterprisedna.co/library/icons/power-bi.png)
![Python Code Generator](https://fs.enterprisedna.co/library/icons/python.png)
![R Code Generator](https://fs.enterprisedna.co/library/icons/r.png)
![Redis Code Generator](https://fs.enterprisedna.co/library/icons/redis.png)
![Regex Code Generator](https://fs.enterprisedna.co/library/icons/regex.png)
![Ruby Code Generator](https://fs.enterprisedna.co/library/icons/ruby.png)
![SAS Code Generator](https://fs.enterprisedna.co/library/icons/sas.png)
![Scala Code Generator](https://fs.enterprisedna.co/library/icons/scala.png)
![Shell Code Generator](https://fs.enterprisedna.co/library/icons/shell.png)
![SPSS Code Generator](https://fs.enterprisedna.co/library/icons/spss.png)
![SQL Code Generator](https://fs.enterprisedna.co/library/icons/sql.png)
![SQLite Code Generator](https://fs.enterprisedna.co/library/icons/sqlite.png)
![Stata Code Generator](https://fs.enterprisedna.co/library/icons/stata.png)
![Tableau Code Generator](https://fs.enterprisedna.co/library/icons/tableau.png)
![VBA Code Generator](https://fs.enterprisedna.co/library/icons/vba.png)