Forum Discussion

mussaenda's avatar
mussaenda
Community Champion
5 years ago
Solved

Text Manipulation using DAX

Hi, This is my sample data: job description item week 1200662 aa 1 1 1200662 bb 1 1 1200662 aa 2 1 1200662 bb 2 1 1200662 aa ...
  • AlB's avatar
    5 years ago

    mussaenda 

    With your current model. It should be able to deal with any combination of contiguous and non-contiguous blocks of items:

     

    MeasureV2 =
    VAR itemsT_ = CALCULATETABLE ( DISTINCT ( Table1[item] ), ALL ( Table1[description] ) )
    VAR rankedT_ = ADDCOLUMNS ( itemsT_, "@index", RANKX ( itemsT_, [item],, ASC ) )
    VAR aux_ = CONCATENATEX ( rankedT_, [item], "|", [@index], ASC )
    VAR minIndex_ = 1
    VAR maxIndex_ = COUNTROWS ( rankedT_ )
    VAR res_ =
        CONCATENATEX (
            rankedT_,
            VAR last_ = PATHITEM ( aux_, [@index] - 1, INTEGER )
            VAR isBlockStart_ = last_ < ( [item] - 1 ) || [@index] = minIndex_
            VAR next_ = PATHITEM ( aux_, [@index] + 1, INTEGER )
            VAR isBlockEnd_ = ( next_ > ( [item] + 1 ) ) || [@index] = maxIndex_
            RETURN
                IF (
                    isBlockStart_ && NOT isBlockEnd_,
                    [item] & "-",
                    IF ( isBlockEnd_, [item] & "," )
                ),
            ,
            [@index], ASC
        )
    RETURN
        SELECTEDVALUE ( Table1[job] ) & "/"
            & IF ( RIGHT ( res_, 1 ) = ",", LEFT ( res_, LEN ( res_ ) - 1 ), res_ )

     

     

    I'm sure it can be coded more elegantly but I haven't had time to polish it yet

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

     

     

  • AlB's avatar
    5 years ago

    mussaenda 

    The measure:

    1. Gets the list of items

    2. Ranks them to get them sorted in ascending order

    3. Builds a string we can use PATHITEM on, since it simplifies the access to the ranked table

    4. For each row in the ranked (sorted table), it checks the previous and next row to see if it's the beginning and/or end of a block.

    5. Builds the final string, removing the unwanted "," at the end (this can be done earlier too) 

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers