Forum Discussion

talkprem's avatar
talkprem
Helper I
2 years ago
Solved

Unique ids from a string using DAX measure

Hi All,   I have data in this manner id value 10023 123,456 10023 123,456,789 10023 456,789 which by using a DAX measure i need to show in tablix visual as below for the value ...
  • AntrikshSharma's avatar
    AntrikshSharma
    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
    )

     

     

  • talkprem's avatar
    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
    )