Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
PANDAmonium
Resolver IV
Resolver IV

Using a column as a variable in Power Query function

Hi All,

 

I'm trying to create a function that reutrns a list based on the value of another column, but for the life of me can't figure out something seemingly basic. Hopefully you can help...

 

So I have a table that's basically this:

Id  | Condition A? | Condition B?

1   |   TRUE          |  FALSE

2   |   TRUE          | TRUE

3   |   FALSE         | TRUE

 

So my function should return the list of Id's where the condition is true, if I pass "Condition A" it should return {1, 2}. B should return {2, 3}. So this is what I have so far.

 

= let ListValuesByCondition= (DataTable as table, ReturnColumn as text) =>
Table.ToList(Table.SelectColumns((Table.SelectRows(DataTable, each ([#"Condition A?"] = true))), ReturnColumn))
in ListValuesByCondition

 

= FN_ListValuesByCondition(#"My Table", "Id")

 

But I want to pass the condition column ( ex: [#"Condition A?"] ) as a variable. And making it type list return nothing. Sounds like it's something obvious, but I haven't had much luck finding solutions. Thank you in advance!

1 ACCEPTED SOLUTION
PANDAmonium
Resolver IV
Resolver IV

Figured it out shortly after posting, but here's the solution in case anyone runs into the same.

 

So we're actually referencing a field instead of a column, and we can't use brackets since it will take the literal of the variable name. So instead we need to use Record.Field. It should look like:

 

= let ListValuesByCondition= (DataTable as table, ReturnColumn as text, ConditionColumn as text) =>
Table.ToList(Table.SelectColumns((Table.SelectRows(DataTable, each (Record.Field(_, ConditionColumn) = true)), ReturnColumn))
in ListValuesByCondition

 

= FN_ListValuesByCondition(#"My Table", "Id", "Condition A?")

View solution in original post

1 REPLY 1
PANDAmonium
Resolver IV
Resolver IV

Figured it out shortly after posting, but here's the solution in case anyone runs into the same.

 

So we're actually referencing a field instead of a column, and we can't use brackets since it will take the literal of the variable name. So instead we need to use Record.Field. It should look like:

 

= let ListValuesByCondition= (DataTable as table, ReturnColumn as text, ConditionColumn as text) =>
Table.ToList(Table.SelectColumns((Table.SelectRows(DataTable, each (Record.Field(_, ConditionColumn) = true)), ReturnColumn))
in ListValuesByCondition

 

= FN_ListValuesByCondition(#"My Table", "Id", "Condition A?")

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors