Convert comma separated text to a list of numbers

允我心安 提交于 2020-07-30 11:15:25

问题


In Power BI, I have created a DAX query created with a var giving comma-separated text using CONCATENATEX function.

Output like

Var = "1,2,3,4,5,6"

Now I want to search this var into my table column with syntax

Table[col] in {var}.

It is throwing an error.

I even tried converting the column to string with syntax

Convert(table[col], string)  in {var}

The error is removed but the data doesn't match with column data.


回答1:


I found this Power BI community thread that is essentially the same question.

The solution is to convert the string into a path object.

Here's the code from the link:

Measure 3 =
VAR mymeasure =
    SUBSTITUTE ( [Measure], ",", "|" )
VAR Mylen =
    PATHLENGTH ( mymeasure )
VAR mytable =
    ADDCOLUMNS (
        GENERATESERIES ( 1, mylen ),
        "mylist", VALUE ( PATHITEM ( mymeasure, [Value] ) )
    )
VAR mylist =
    SELECTCOLUMNS ( mytable, "list", [mylist] )
RETURN
    CALCULATE ( COUNTROWS ( Table1 ), Table1[ID] IN mylist )

There's a bit of redundancy in the above, so I'd probably condense it a bit and write it like this:

SomeMeasureFilteredByVar =
VAR PathVar = SUBSTITUTE ( [Var], ",", "|" )
VAR VarList =
    SELECTCOLUMNS (
        GENERATESERIES ( 1, PATHLENGTH ( PathVar ) ),
        "Item", VALUE ( PATHITEM ( PathVar, [Value] ) )
    )
RETURN
    CALCULATE ( [SomeMeasure], 'Table'[ID] IN VarList )


Edit: To just create a calculated table, you can skip the last step:

TableFromString =
VAR PathVar = SUBSTITUTE ( "1,2,3,4,5,6", ",", "|" )
RETURN
    SELECTCOLUMNS (
        GENERATESERIES ( 1, PATHLENGTH ( PathVar ) ),
        "Item", VALUE ( PATHITEM ( PathVar, [Value] ) )
    )


Note that it is not possible to create a calculated table that is dynamically responsive to report filters and slicers. Materialized calculated tables are only calculated once when the data loads, not every time you adjust a filter or slicer.

Therefore, you can use a measure in the table instead of the string "1,2,3,4,5,6" but the table output will be the same regardless of what the measure returns within different filter contexts.



来源:https://stackoverflow.com/questions/62104823/convert-comma-separated-text-to-a-list-of-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!