Forum Discussion
PowerQuery Custom Column Lookup Calculation that handles blank values
- 1 year ago
Hi jawnne,
You could probably use 'try' 'otherwise' to get around this.
https://learn.microsoft.com/en-us/power-query/error-handling
- 1 year ago
Hi jawnne
You're using an M code custom column in Power Query to replicate a DAX-style lookup, and your current formula works well when the source_column contains values. However, when the source_column is blank (null), the formula throws an error because it still tries to perform the lookup on a null value. To handle this gracefully, you need to add a conditional check before attempting the lookup. The idea is to first check if the value from source_column is null; if it is, the formula should return null (or blank), and if not, it should proceed with the lookup. You can structure your formula like this:
if [source_column] = null then null else (let variable = [source_column] in try (Table.SelectRows(lookup_table, each [lookup_value] = variable)){0}[lookup_value] otherwise null)This updated expression first checks if source_column is null and returns null immediately if so. If not, it proceeds to perform the lookup using Table.SelectRows as you originally intended. Wrapping the lookup in a try ... otherwise block ensures that even if the value isn't found in the lookup table, it won’t result in an error—it will simply return null. This approach provides a safe and robust way to mimic a DAX lookup in Power Query, handling both filled and optional (blank) fields without failing.
Hi jawnne ,
Thank you for reaching out to the Microsoft Fabric Community forum.
Please follow below steps to fix the issue.
1. Created two tables with sample data.
2. Created new custom column in Table1 (SourceTable) , and place the below M code.
= Table.AddColumn(#"Changed Type", "New Custom", each if [ProductCode] = null then null
else
let
variable = [ProductCode],
result = Table.SelectRows(LookupTable, each [ProductCode] = variable)
in
if Table.IsEmpty(result) then null else result{0}[ProductName])
3. Please refer output snap and attached PBIX file.
The above M code handles the blank values with null data, instead of Error values.
If this information is helpful, please “Accept it as a solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.
Thank v-dineshya, thank you for the explanation, will give this a try as well.