电源查询

编程入门 行业动态 更新时间:2024-10-25 09:35:58
电源查询 - 计数是否自定义功能(Power Query - Count if custom function)

继续这个问题 。

我想创建一个自定义函数,我传入一个表,一列和一个值,它会计算满足这些条件的所有行。

到目前为止,我有

let fCountif = (tbl as table, col as text, value as any) as number => let select_rows = Table.SelectRows(tbl, each [col] = value), count_rows = Table.RowCount(select_rows) in count_rows in fCountif

但是我遇到了各种各样的问题。 第一个是调用函数来测试它,当我传入一个表名的字符串时,它会向函数发送一个文字字符串,而不是将它转换为表。 当通过查询编辑器按钮调用它时,这可能只是一个问题,并且应该在我的M代码的其余部分使用该函数时自行解决。

第二个是如何将col值传递给函数的[col]部分。 我看到其他例子中使用了肥胖的火箭,即[column name] <= col但是无法使其工作。

我努力让自己的脑袋围绕着功能,并且很乐意在vba中这样做,所以如果你有任何提示,请大声喊出来。 这方面的文档很难得到。 干杯。

Following on from this question.

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.

So far I have

let fCountif = (tbl as table, col as text, value as any) as number => let select_rows = Table.SelectRows(tbl, each [col] = value), count_rows = Table.RowCount(select_rows) in count_rows in fCountif

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.

The second is how to pass the col value into the [col] part of the function. I have seen fat rockets used in other examples ie [column name] <= col but just can't get it to work.

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.

最满意答案

表可以被认为是记录列表,其中每个记录都是一行。 在Table.AddColumn和Table.SelectRows的函数中, [col]是写入Record.Field(_, "col")一种较短的方式,它访问记录的“col”字段。

这是编写你的函数的一种方法:

let fCountif = (tbl as table, col as text, value as any) as number => let select_rows = Table.SelectRows(tbl, each Record.Field(_, col) = value), count_rows = Table.RowCount(select_rows) in count_rows in fCountif

Tables can be thought of as a list of records, where each record is a row. In the functions for Table.AddColumn and Table.SelectRows, [col] is a shorter way of writing Record.Field(_, "col"), which accesses the "col" field of the record.

Here is one way to write your function:

let fCountif = (tbl as table, col as text, value as any) as number => let select_rows = Table.SelectRows(tbl, each Record.Field(_, col) = value), count_rows = Table.RowCount(select_rows) in count_rows in fCountif

更多推荐

本文发布于:2023-08-01 02:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电源

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!