Power Query - Count if custom function
<p><a href="https://stackoverflow.com/questions/35123346/power-query-add-column-countif-across-two-tables-ie-countifpeoplecityid">Following on from this question</a>.</p>

<p>I'd like to create a custom function where I pass in a table, a column and value and it counts all the rows where those conditions are met.</p>

<p>So far I have </p>

<pre><code>let 
    fCountif = (tbl as table, col as text, value as any) as number =&gt;
    let
        select_rows = Table.SelectRows(tbl, each [col] = value),
        count_rows = Table.RowCount(select_rows)
    in
        count_rows
in
    fCountif
</code></pre>

<p>But am running into all sorts of issues. The first is invoking the function to test it, when I pass in a string for the Table name it sends a literal string to the function instead of converting it to table. This is probably only an issue when invoking it through the query editor button and should resolve itself when using the function throughout the rest of my M code.</p>

<p>The second is how to pass the <code>col</code> value into the <code>[col]</code> part of the function. I have seen fat rockets used in <a href="http://www.mrexcel.com/forum/power-bi/865756-sumifs-functionality-using-power-query-m-advanced-editor.html" rel="nofollow noreferrer">other examples</a> ie <code>[column name] &lt;= col</code> but just can't get it to work.</p>

<p>I'm trying hard to get my head around functions and was comfortable doing this in vba so if you have any tips please shout out. Documentation on this is hard to come by. Cheers.</p>