3

In Power BI I have an M Query that tests for the value in a column equaling or not equaling to null.

When I add the statement for [Sale.Revenue] <> null I get an error however it works fine for the [UserRole.Name] = null it works fine. Tested just by removing the statement and adding it back.

We cannot convert the value null to type Logical.

This seems like it should work but just can't figure it out.

add_user_role_group = Table.AddColumn(
    join_expand_sale, 
    "UserRole.Group1", 
    each (
      if [UserRole.Name] = null and
         [Sale.Revenue] <> null then
        "Group1"
      else if Text.Contains([UserRole.Name], "Manager") then
        "Group2"
      else
        "Undefined"
    )
  )

I am sure it is something glaringly obvious :/ Thanks for your thoughts on this.

4

2 回答 2

9

您的其中一行的 UserRole.Name 和 Sale.Revenue 都为空值。您需要明确检查,然后将其添加到“未定义”组。

发生的情况是第一个条件失败,因为 Sale.Revenue 为空。第二个条件调用 Text.Contains,它在 [UserRole.Name] 为 null 时返回 null(Text.Contains 返回一个可为 null 的逻辑值)。null 不是真或假,所以你得到错误。

于 2016-03-10T00:12:45.937 回答
3

经过这样的旅程,我终于找到了 Text.Length !

你可以像这样解决你的问题:

if Text.Length([UserRole.Name]) = 0 and
         Text.Length([Sale.Revenue]) > 0 then

我希望我对你有所帮助。参考:Power Query M - Text.Length

于 2018-12-17T23:17:05.957 回答