DAX is not like M when it comes to data manipulation, and it is not supposed to do that. However, if you need something in DAX similar to Table.FromList in M, this blog post is for you.
If you have a list of values in a string in DAX and you want to obtain a table with one row for each item in the list, you can do the following:
- Use “|” as item separator in the string (instead of “,” or “;”)
- Determines the number of items in the string by using PATHLENGTH
- Iterates the string by using GENERATESERIES to create one row for each item
- Extract the item by using PATHITEM and project the result by using SELECTCOLUMNS
Here is an example you can test online.
-- Covert a list of items in a string -- into a table with one row for each item -- -- PATHLENGTH determines the number of items -- GENERATESERIES iterates the items -- PATHITEM extract the Nth item EVALUATE VAR list = "123|456|789|764" VAR _length = PATHLENGTH ( list ) VAR Result = SELECTCOLUMNS ( GENERATESERIES ( 1, _length ), -- Use TEXT instead of INTEGER -- to get a list of strings "Item", PATHITEM ( list, [value], INTEGER ) ) RETURN Result
Item |
---|
123 |
456 |
789 |
764 |
This way, you get a table that can be used in a more effective way to filter a column.
For example, this is not a good idea, because you repeat PATHCONTAINS for each row in the table:
FILTER ( table, PATHCONTAINS ( "123|456|789|764", table[column] ) )
This could be better:
FILTER ( ALL ( table[column] ), PATHCONTAINS ( "123|456|789|764", table[column] ) )
However, it still suffers from performance if table[column] has a high cardinality.
Thus, in principle, this approach is faster:
VAR list = "123|456|789|764" VAR _length = PATHLENGTH ( list ) VAR _pivot = SELECTCOLUMNS ( GENERATESERIES ( 1, _length ), "Item", PATHITEM ( list, [value] ) ) VAR _filter = TREATAS ( _pivot, table[column] ) RETURN _filter
However, if you have the string as a constant parameter, you should definitely use the faster table constructor syntax:
-- Version with strings TREATAS ( { "123", "456", "789", "764" }, table[column] ) -- Version with numbers TREATAS ( { 123, 456, 789, 764 }, table[column] )
Returns returns the number of items in a particular path string. This function returns 1 for the path generated for an ID at the top/root of a hierarchy.
PATHLENGTH ( <Path> )
Returns a table with one column, populated with sequential values from start to end.
GENERATESERIES ( <StartValue>, <EndValue> [, <IncrementValue>] )
Returns the nth item in the delimited list produced by the Path function.
PATHITEM ( <Path>, <Position> [, <Type>] )
Returns a table with selected columns from the table and new columns specified by the DAX expressions.
SELECTCOLUMNS ( <Table> [[, <Name>], <Expression> [[, <Name>], <Expression> [, … ] ] ] )
Returns TRUE if the specified Item exists within the specified Path.
PATHCONTAINS ( <Path>, <Item> )