Forum Discussion

Jyaul1122's avatar
Jyaul1122
Helper III
1 year ago
Solved

Concatenex with Rank

Hello friend, 

 

I have Stage table with fields: Project,Stage and Date.

ProjectStageDate
Project 1S125-Dec-24
Project 1S227-Dec-24
Project 1S327-Dec-24
Project 1S428-Dec-24
Project 1S529-Dec-24
Project 1S606-Jan-25
Project 1S709-Jan-26
Project 2S110-Jan-26
Project 2S216-Jan-26
Project 2S317-Jan-26
Project 2S418-Jan-27
Project 2S521-Jan-27
Project 3S127-Jan-25
Project 3S225-Jan-25
Project 3S331-Jan-26
Project 3S404-Feb-26
Project 3S505-Feb-27
Project 3S629-Mar-27
Project 3S713-Feb-27

 

I would  like to get table where , Stage  falls in which year so that I wrote measure like based on the date order:

Project2024202520262027
Project 1S1,S2,S3,S4,S5S6S7 
Project 2  S1,S2,S3S4,S5
Project 3 S2,S1S3,S4S5,S7,S5

 

Stage measure = CONCATENATEX('Stage table',CALCULATE(MAX('Stage table'[Stage]),'Stage table'[Date]<=MAX('Stage table'[Date])),",",'Stage table'[Date],ASC)
 
But actually I would like to get table with combination of rank by date in individual year like: Both Rank  Stage :
ex: 1 S2, 2 S1 -- 1,2 represent rank and S2,S1 represent Stage   
Project2024202520262027
Project 11 S1,2 S2,3 S3,4 S4,5 S51 S61 S7 
Project 2  1 S1,2 S2,3 S31 S4,2 S5
Project 3 1 S2, 2 S11 S3,2 S41 S5,2 S7,2 S5

 

how can i combine Rank inside Concatenax or do you have any idea by DAX ?

  • DataNinja777 Kedar_Pande 

    I solved by editing above measures.

    First I created one column Sorting in Power query from date column and stage

    Date.Year([Date])*100000000
    +Date.Month([Date])*1000000
    +Date.Day([Date])*10000
    +Text.End([Stage],1)

     

    and measure edit:

     

    Stage Measure = 
    VAR CurrentYear = SELECTEDVALUE('DateTable'[Year]) -- Gets the selected year from the DateTable
    VAR Projects = DISTINCT('Stage table'[Project]) -- Get distinct projects for iteration
    RETURN
    CONCATENATEX(
        ADDCOLUMNS(
            FILTER(
                'Stage table',
                YEAR('Stage table'[Date]) = CurrentYear
            ),
            "Rank_Stage", 
            FORMAT(
                RANKX(
                    FILTER(
                        'Stage table',
                        'Stage table'[Project] = EARLIER('Stage table'[Project]) &&
                        YEAR('Stage table'[Date]) = CurrentYear
                    ),
                    'Stage table'[Sorting],
                    ,
                    ASC
                ), "0"
            ) & " " & 'Stage table'[Stage]
        ),
        [Rank_Stage],
        ", ",
        'Stage table'[Date],
        ASC
    )

     

     

8 Replies

  • Hi Jyaul1122 , I have achieved your expected output as below, have attached the PBIX file along with this post

     

    Download PBIX

     

    Did I answer your question ? If yes, please mark this post as a solution.

     

    Thanks,

    Jai 

     

    • Jyaul1122's avatar
      Jyaul1122
      Helper III

      Thanks for your reply, we are very close to solution.

      Could you please sort by rank, for example in Project 3, rank 1 will be appear first with stage, then second rank with stage and so on

       

       

       

       

      • Jai-Rathinavel's avatar
        Jai-Rathinavel
        Super User

        Jyaul1122 , It can be done by just introducing an orderby field (Table[Rank]) to the CONCATENATEX funtion. Replace the current measure expression with the below dax. 

         

        Concat = 
        var res = CONCATENATEX('Table', 'Table'[Rank] & " " & 'Table'[Stage],",",'Table'[Rank])
        RETURN
        res

         

         

        Output:

         

        If the DAX meets your expectation, please mark this post as a solution

         

        Thanks,

        Jai

  • Hi Jyaul1122 ,

     

    You can produce your required output by writing a measure like below after creating a calendar table (DateTable), and creating a relationship between your fact table and the calendar dimension table.

    Stage Measure = 
    VAR CurrentYear = SELECTEDVALUE('DateTable'[Year]) -- Gets the selected year from the DateTable
    VAR Projects = DISTINCT('Stage table'[Project]) -- Get distinct projects for iteration
    RETURN
    CONCATENATEX(
        ADDCOLUMNS(
            FILTER(
                'Stage table',
                YEAR('Stage table'[Date]) = CurrentYear
            ),
            "Rank_Stage", 
            FORMAT(
                RANKX(
                    FILTER(
                        'Stage table',
                        'Stage table'[Project] = EARLIER('Stage table'[Project]) &&
                        YEAR('Stage table'[Date]) = CurrentYear
                    ),
                    'Stage table'[Date],
                    ,
                    ASC
                ), "0"
            ) & " " & 'Stage table'[Stage]
        ),
        [Rank_Stage],
        ", ",
        'Stage table'[Project],
        ASC
    )
    

    The resulting output will be as shown below, which is in line with your required output. 

    I've attached an example pbix file for your reference.

     

    Best regards,

     

  • Jyaul1122 

    Add a calculated column in your Stage table to extract the year from the date:

    Year = YEAR('Stage table'[Date])

    Create a Rank Measure

    RankStage = 
    RANKX(
    FILTER(
    'Stage table',
    'Stage table'[Project] = MAX('Stage table'[Project]) &&
    YEAR('Stage table'[Date]) = YEAR(MAX('Stage table'[Date]))
    ),
    'Stage table'[Date],
    , ASC,
    DENSE
    )

    Create the Combined Measure:

    CombinedStageRank = 
    CONCATENATEX(
    FILTER(
    'Stage table',
    'Stage table'[Project] = MAX('Stage table'[Project]) &&
    YEAR('Stage table'[Date]) = MAX('Stage table'[Year])
    ),
    RANKX(
    FILTER(
    'Stage table',
    'Stage table'[Project] = MAX('Stage table'[Project]) &&
    YEAR('Stage table'[Date]) = YEAR(MAX('Stage table'[Date]))
    ),
    'Stage table'[Date],
    , ASC,
    DENSE
    ) & " " & 'Stage table'[Stage],
    ", ",
    'Stage table'[Date],
    ASC
    )

    Create a Matrix Visual:

    Use the Project field for the Rows.
    Use the Year field for the Columns.
    Use the CombinedStageRank measure for the Values.

     

    ๐Ÿ’Œ If this helped, a Kudos ๐Ÿ‘ or Solution mark would be great! ๐ŸŽ‰
    Cheers,
    Kedar
    Connect on LinkedIn

  • Kedar_Pande DataNinja777 

    Thanks for your time.

    The replies are close to solution, what if the two or more stage have same dates. For example Project 4: Stage 3 and Stage 5 are have same date. As per your logic I am getting rank 1 for both but I would like to get: 

    Project 4  1 S1, 2 S21 S3, 2 S5
         
         

     

    Data:

    ProjectStageDate
    Project 1S125-Dec-24
    Project 1S227-Dec-24
    Project 1S327-Dec-24
    Project 1S428-Dec-24
    Project 1S529-Dec-24
    Project 1S606-Jan-25
    Project 1S709-Jan-26
    Project 2S110-Jan-26
    Project 2S216-Jan-26
    Project 2S317-Jan-26
    Project 2S418-Jan-27
    Project 2S521-Jan-27
    Project 3S127-Jan-25
    Project 3S225-Jan-25
    Project 3S331-Jan-26
    Project 3S404-Feb-26
    Project 3S505-Feb-27
    Project 3S629-Mar-27
    Project 3S713-Feb-27
    Project 4S114-May-26
    Project 4S215-May-26
    Project 4S316-May-27
    Project 4S516-May-27
    • DataNinja777's avatar
      DataNinja777
      Super User

      Hi Jyaul1122 ,

       

      To address the case where multiple stages for a project have the same date and ensure unique rankings (e.g., assigning a secondary order within the same date), we need to enhance the ranking logic. This can be achieved by considering the Stage column as a secondary sorting criterion in the RANKX function.

      Hereโ€™s the revised measure:

       

      Stage Measure =
      VAR CurrentYear = SELECTEDVALUE('DateTable'[Year]) -- Selected year context
      RETURN
      CONCATENATEX(
          ADDCOLUMNS(
              FILTER(
                  'Stage table',
                  YEAR('Stage table'[Date]) = CurrentYear
              ),
              "Rank_Stage",
              FORMAT(
                  RANKX(
                      FILTER(
                          'Stage table',
                          'Stage table'[Project] = EARLIER('Stage table'[Project]) &&
                          YEAR('Stage table'[Date]) = CurrentYear
                      ),
                      'Stage table'[Date] & 'Stage table'[Stage],
                      ,
                      ASC
                  ),
                  "0"
              ) & " " & 'Stage table'[Stage]
          ),
          [Rank_Stage],
          ", ",
          'Stage table'[Project],
          ASC
      )
      

      The resulting output is as shown below:

      The output respects both the date and stage ordering criteria.

      I've attached an example pbix file for your reference. 

       

      Best regards,

       

  • DataNinja777 Kedar_Pande 

    I solved by editing above measures.

    First I created one column Sorting in Power query from date column and stage

    Date.Year([Date])*100000000
    +Date.Month([Date])*1000000
    +Date.Day([Date])*10000
    +Text.End([Stage],1)

     

    and measure edit:

     

    Stage Measure = 
    VAR CurrentYear = SELECTEDVALUE('DateTable'[Year]) -- Gets the selected year from the DateTable
    VAR Projects = DISTINCT('Stage table'[Project]) -- Get distinct projects for iteration
    RETURN
    CONCATENATEX(
        ADDCOLUMNS(
            FILTER(
                'Stage table',
                YEAR('Stage table'[Date]) = CurrentYear
            ),
            "Rank_Stage", 
            FORMAT(
                RANKX(
                    FILTER(
                        'Stage table',
                        'Stage table'[Project] = EARLIER('Stage table'[Project]) &&
                        YEAR('Stage table'[Date]) = CurrentYear
                    ),
                    'Stage table'[Sorting],
                    ,
                    ASC
                ), "0"
            ) & " " & 'Stage table'[Stage]
        ),
        [Rank_Stage],
        ", ",
        'Stage table'[Date],
        ASC
    )