Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Return a concatenated string value based on calculation

Greetings everyone!

I need help with the following moment:










There are 2 columns:
1) Say the first column is a name of product
2) Second column is a part that belongs to a certain product 

A particular part can be assigned to any product:


What I want: 
To create a measure, where:
        - DAX checks IF:
                  - part belongs to only one project - it returns the name of the project (i.e. 1st column's value)
                  - part belongs to a couple of projects - it returns concatenated string where project names are concatenated via comma

The result will be in the third column. Thank you!

  • Anonymous 
    Please try

    =
    VAR CurrentProject =
        SELECTEDVALUE ( TableName[Name] )
    VAR CurrentPartTable =
        CALCULATETABLE ( TableName, ALLEXCEPT ( TableName, TableName[Part #] ) )
    RETURN
        IF (
            COUNTROWS ( CurrentPartTable ) = 1,
            CurrentProject,
            CONCATENATEX ( CurrentPartTable, TableName[Name], ", ", TableName[Name], ASC )
        )

10 Replies

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    Anonymous 
    Please try

    =
    VAR CurrentProject =
        SELECTEDVALUE ( TableName[Name] )
    VAR CurrentPartTable =
        CALCULATETABLE ( TableName, ALLEXCEPT ( TableName, TableName[Part #] ) )
    RETURN
        IF (
            COUNTROWS ( CurrentPartTable ) = 1,
            CurrentProject,
            CONCATENATEX ( CurrentPartTable, TableName[Name], ", ", TableName[Name], ASC )
        )
    • Anonymous's avatar
      Anonymous
      Not applicable

      I am sorry, silly me. Your solution actually works, it was me who didn't make it work right. THANK YOU!

      • tamerj1's avatar
        tamerj1
        Icon for Community Champion rankCommunity Champion

        Anonymous 
        Happy to hear that. Thank you for marking my reply as acceptable solution

    • Anonymous's avatar
      Anonymous
      Not applicable

      It return the list of every occurence now, but that's a good way to start?

      • tamerj1's avatar
        tamerj1
        Icon for Community Champion rankCommunity Champion

        Anonymous 
        Didn't know they are repeated. Please use

        =
        VAR CurrentProject =
            SELECTEDVALUE ( TableName[Name] )
        VAR CurrentPartTable =
            CALCULATETABLE ( TableName, ALLEXCEPT ( TableName, TableName[Part #] ) )
        VAR ProjectNames =
            DISTINCT ( SELECTCOLUMNS ( CurrentPartTable, "@Name", TableName[Name] ) )
        RETURN
            IF (
                COUNTROWS ( ProjectNames ) = 1,
                CurrentProject,
                CONCATENATEX ( ProjectNames, [@Name], ", ", [@Name], ASC )
            )
  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 
    Do you want to create a measure or a calculated column? Can you share the same sample data as copy/paste?

    • Anonymous's avatar
      Anonymous
      Not applicable

      A measure would be more preferrable. I guess it's going to take a couple of variables to make it. Unfortunately, I can't send the data because I have no rights....

    • Anonymous's avatar
      Anonymous
      Not applicable

      but there are only 2 columns that are being shown on the screenshot