Forum Discussion
Unique ids from a string using DAX measure
- 2 years ago
talkprem Updated verison, i have modified it to sort based on ASC/DESC you can control that as you by changing the parameter inside CONCATENATEX.
Unique ID String = IF ( ISINSCOPE ( report_table[site_id] ), VAR NumberCount = ADDCOLUMNS ( SUMMARIZE ( report_table, report_table[site_id], report_table[value] ), "@Number Count", LEN ( report_table[value] ) - LEN ( SUBSTITUTE ( report_table[value], ",", "" ) ) + 1 ) VAR MaxNumberCount = MAXX ( NumberCount, [@Number Count] ) VAR GenerateNumberSeries = SELECTCOLUMNS ( GENERATESERIES ( 1, MaxNumberCount, 1 ), "@Int", [value] ) VAR TempTable = FILTER ( GENERATE ( NumberCount, GenerateNumberSeries ), [@Int] <= [@Number Count] ) VAR SplitTextByNumber = ADDCOLUMNS ( TempTable, "@Final String", INT ( TRIM ( PATHITEM ( SUBSTITUTE ( report_table[value], ",", "|" ), [@Int], TEXT ) ) ) ) VAR DistinctNumbers = DISTINCT ( SELECTCOLUMNS ( SplitTextByNumber, [@Final String] ) ) VAR Result = CONCATENATEX ( DistinctNumbers, [@Final String], ", ", [@Final String], DESC ) RETURN Result ) - 2 years ago
After some debugging the DAX code, I found that
MaxNumberCount
variable is not working and the code fails to work.after looking at my data i found that max number of comma separate values in a single row are 5. So I hard coded the number 10 and code is working as per expectations. Till 10 iterations. Many thanks and Shoutout to AntrikshSharma for helping out.
Final code which is working fine for me (this can work even with null values in value column) -
Unique ID String null ok = IF ( ISINSCOPE ( report_table[site_id] ), VAR NumberCount = ADDCOLUMNS ( SUMMARIZE ( report_table, report_table[site_id], report_table[value] ), "@Number Count", LEN ( report_table[value] ) - LEN ( SUBSTITUTE ( report_table[value], ",", "" ) ) + 1 ) -- VAR MaxNumberCount = -- MAXX ( NumberCount, [@Number Count] ) VAR GenerateNumberSeries = SELECTCOLUMNS ( GENERATESERIES ( 1, 10, 1 ), "@Int", [value] ) VAR TempTable = FILTER ( GENERATE ( NumberCount, GenerateNumberSeries ), [@Int] <= [@Number Count] ) VAR SplitTextByNumber = ADDCOLUMNS ( TempTable, "@Final String", INT ( TRIM ( PATHITEM ( SUBSTITUTE ( report_table[value], ",", "|" ), [@Int], TEXT ) ) ) ) VAR DistinctNumbers = DISTINCT ( SELECTCOLUMNS ( SplitTextByNumber, [@Final String] ) ) VAR Result = CONCATENATEX ( DistinctNumbers, [@Final String], ", ", [@Final String], DESC ) RETURN Result )
talkprem Updated verison, i have modified it to sort based on ASC/DESC you can control that as you by changing the parameter inside CONCATENATEX.
Unique ID String =
IF (
ISINSCOPE ( report_table[site_id] ),
VAR NumberCount =
ADDCOLUMNS (
SUMMARIZE ( report_table, report_table[site_id], report_table[value] ),
"@Number Count",
LEN ( report_table[value] ) - LEN ( SUBSTITUTE ( report_table[value], ",", "" ) ) + 1
)
VAR MaxNumberCount =
MAXX ( NumberCount, [@Number Count] )
VAR GenerateNumberSeries =
SELECTCOLUMNS (
GENERATESERIES ( 1, MaxNumberCount, 1 ),
"@Int", [value]
)
VAR TempTable =
FILTER (
GENERATE ( NumberCount, GenerateNumberSeries ),
[@Int] <= [@Number Count]
)
VAR SplitTextByNumber =
ADDCOLUMNS (
TempTable,
"@Final String", INT ( TRIM ( PATHITEM ( SUBSTITUTE ( report_table[value], ",", "|" ), [@Int], TEXT ) ) )
)
VAR DistinctNumbers =
DISTINCT (
SELECTCOLUMNS (
SplitTextByNumber,
[@Final String]
)
)
VAR Result =
CONCATENATEX (
DistinctNumbers,
[@Final String],
", ",
[@Final String], DESC
)
RETURN
Result
)
AntrikshSharma We have
values like " " and NULLS which are causing leading or trailing "," (commas to be added) in the output.
could you please help me in this brother?
current output
| id | value |
| 10023 | ,123,456,789, |
| 10023 | 123,456,789, |
expected output
| id | value |
| 10023 | 123,456,789 |
| 10024 | 123,456,789 |
thanks in advance.