【问题标题】:DAX Search a string for multiple valuesDAX 在字符串中搜索多个值
【发布时间】:2021-08-24 17:28:51
【问题描述】:

我需要创建一个新的 DAX 列,该列将从同一个表的另一列中搜索字符串。它将搜索第二个表中的任何值,如果找到任何这些值,则返回 True。简化示例:

假设我有一个名为 Sentences 的表,其中包含 1 列:

Sentences
Col1
----------------
"The aardvark admitted it was wrong"
"The attractive peanut farmer graded the term paper"
"The awning was too tall to touch"

还有一个名为 FindTheseWords 的表,其中包含值列表

FindTheseWords 
Col1
----------------
peanut
aardvark

我将在 Sentences 表中创建 Col2,它应该返回

Sentences
Col1                                                    Col2
----------------------------------------------------    ------------------------
"The aardvark admitted it was wrong"                    TRUE
"The attractive peanut farmer graded the term paper"    TRUE
"The awning was too tall to touch"                      FALSE

FindTheseWords 的列表实际上很长,所以我不能硬编码它们并使用 OR。我需要参考表格。我不关心空格,所以带有“花生”的句子也会返回 true。

我在 M 中看到了一个很好的实现,但是我的负载性能受到了相当大的打击,所以我希望为新列找到一个 DAX 选项。 M方案,供参考:How to search multiple strings in a string?

【问题讨论】:

    标签: excel dax powerpivot


    【解决方案1】:

    事实表

    | Column1                                              |
    |------------------------------------------------------|
    | The aardvark admitted it was   wrong                 |
    | The attractive peanut farmer   graded the term paper |
    | The awning was too tall to   touch                   |
    | This is  text string                                 |
    | Tester is needed                                     |
    

    句子表

    | Column1    |
    |------------|
    | attractive |
    | peanut     |
    | aardvark   |
    | Tester     |
    

    计算列

    Column =
    VAR _1 =
        ADDCOLUMNS ( 'fact', "newColumn", SUBSTITUTE ( 'fact'[Column1], " ", "|" ) )
    VAR _2 =
        GENERATE (
            _1,
            ADDCOLUMNS (
                GENERATESERIES ( 1, PATHLENGTH ( [newColumn] ) ),
                "Words", PATHITEM ( [newColumn], [Value], TEXT )
            )
        )
    VAR _3 =
        ADDCOLUMNS (
            _2,
            "test", CONTAINS ( VALUES ( sentence[Column1] ), sentence[Column1], [Words] )
        )
    VAR _4 =
        DISTINCT (
            SELECTCOLUMNS (
                FILTER ( _3, [test] = TRUE ),
                "Column1", [Column1] & "",
                "test", [test] & ""
            )
        )
    VAR _5 =
        DISTINCT (
            SELECTCOLUMNS (
                FILTER ( _3, [test] = FALSE ),
                "Column1", [Column1] & "",
                "test", [test] & ""
            )
        )
    VAR _7 =
        FILTER ( _5, [Column1] = MAXX ( _4, [Column1] ) )
    VAR _8 =
        UNION ( _4, _7 )
    RETURN
        MAXX (
            FILTER ( _8, [Column1] = CALCULATE ( MAX ( 'fact'[Column1] ) ) ),
            [test]
        )
    

    【讨论】:

    • 我想我正在关注其中的大部分内容。由于我在 Excel 中使用 Power Pivot,我将无法使用 GENERATESERIES。有没有你推荐的替代品?
    • 请正确标记以吸引正确的回答者,这样可以节省很多时间。不过,您在原始问题中标记了 PowerBI。我不是excel用户,如果它不起作用,请见谅。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    相关资源
    最近更新 更多