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

idvalue
10023123,456
10023123,456,789
10023456,789

which by using a DAX measure i need to show in tablix visual as below for the value column, please dont give reply for power query as that may not fit my purpose.
Looking for DAX measure approach

idvalue
10023123,456,789
  • 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
    )

     

     

  • 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
    )

     

     

9 Replies

  • AntrikshSharma's avatar
    AntrikshSharma
    Community Champion

    talkprem 

     

    Unique ID String = 
    IF ( 
        ISINSCOPE ( t[id] ),
        VAR NumberCount =
            ADDCOLUMNS (
                t,
                "@Number Count",
                    LEN ( t[value] ) - LEN ( SUBSTITUTE ( t[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", TRIM ( PATHITEM ( SUBSTITUTE ( t[value], ",", "|" ), [@Int], TEXT ) )
            )
        VAR DistinctNumbers = 
            DISTINCT ( 
                SELECTCOLUMNS ( 
                    SplitTextByNumber,
                    [@Final String]
                )
            )
        VAR Result = 
            CONCATENATEX ( 
                DistinctNumbers,
                [@Final String],
                ", "
            )
        RETURN 
            Result
    )

     

     

    • talkprem's avatar
      talkprem
      Helper I

      Hi Antrishk,

       

      Thank you for helping out i tried to use the measure but got an error , "the arguments of generate series function cant be blank". i actually have few more columns which i need to show in this manner.

      id number date id material value
      D77656/17/2024 10023 4002123,456
      D77656/17/2024 10023 4002123,456,789
      D77656/17/2024 10023 4002456,789

       

      to something like this

       

      id number date idmaterial value
      D7765 6/17/2024 10023 4002123,456,789

       

      can you please help me, many many thanks and respect in advance.

    • talkprem's avatar
      talkprem
      Helper I

      Hi AntrikshSharma,

       

      I have tried the dax query you posted but i got an error,
      "the arguments of a generateseries function cant be blank"
      i have 5 column like this

      plate   datetime   material   site_id   value  
      123 6/17/2024 345 10023 123,456,789
      123 6/17/2024 345 10023 123,456
      123 6/17/2024 345 10023 456,789

       to this 

      plate   datetime  material  site_id  value  
      123 6/17/2024 345 10023 123,456,789

      can you have a look in the dax query once please table name "report_table"



      • AntrikshSharma's avatar
        AntrikshSharma
        Community Champion

        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
        )

         

         

  • 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
    )