Code Generator | M (Power Query)
generateConditionalColumn
This function creates a conditional column in a table based on specific conditions in two different columns. The user specifies the source table, column names for condition evaluation, new column name, and conditions. If both conditions are met,
Prompt
Answer
// Function: generateConditionalColumn
// Purpose: To create a conditional column based on specific conditions in 2 different columns
// Parameters:
// sourceTable: The table containing the data
// column1: The name of the first column for condition evaluation
// column2: The name of the second column for condition evaluation
// newColumnName: The name of the new conditional column to be added
// condition1: The condition for column1 (e.g. each [column1] > 100)
// condition2: The condition for column2 (e.g. each [column2] = "High")
// Return Type: table
// Exceptions:
// If the specified columns do not exist in the source table, an error will be raised.
generateConditionalColumn = (sourceTable as table, column1 as text, column2 as text, newColumnName as text, condition1 as logical, condition2 as logical) as table =>
let
// Add a conditional column based on the specified conditions
newTable = Table.AddColumn(sourceTable, newColumnName, each if condition1 and condition2 then "ConditionMet" else "ConditionNotMet")
in
newTable;
Example Usage:
// Create a new conditional column based on conditions in columns Column1 and Column2
generateConditionalColumn(sourceTable, "Column1", "Column2", "NewConditionalColumn", each [Column1] > 100, each [Column2] = "High")
Description
This function creates a conditional column in a table based on specific conditions in two different columns. The user specifies the source table, column names for condition evaluation, new column name, and conditions. If both conditions are met, the new column value is "ConditionMet"; otherwise, it's "ConditionNotMet". If the specified columns do not exist, an error is raised.
More Code Generators
Apache Flink Code Generator Apache Pig Code Generator Azure Data Factory Code Generator C/C++ Code Generator CouchDB Code Generator DAX Code Generator Excel Code Generator Firebase Code Generator Google BigQuery Code Generator Google Sheets Code Generator GraphQL Code Generator Hive Code Generator Java Code Generator JavaScript Code Generator Julia Code Generator Lua Code Generator M (Power Query) Code Generator MATLAB Code Generator MongoDB Code Generator Oracle Code Generator PostgreSQL Code Generator Power BI Code Generator Python Code Generator R Code Generator Redis Code Generator Regex Code Generator Ruby Code Generator SAS Code Generator Scala Code Generator Shell Code Generator SPSS Code Generator SQL Code Generator SQLite Code Generator Stata Code Generator Tableau Code Generator VBA Code Generator