Forum Discussion

Richtpt's avatar
Richtpt
Icon for Helper I rankHelper I
11 months ago
Solved

How to pivot rows to columns with dates using the DAX language?

I'm creating a paginated report that uses a semantic model as the data source.  I'm doing a DAX query to get the data.

 

I've got data that looks like this:

WorkIDActivityNameActivityDate
100Requirements2/1/2025
100Development2/15/2025
100Testing3/1/2025
100Production Deployment3/10/2025
105Requirements2/3/2025

 

Each WorkID can have up to 4 rows of data (there are four different ActivityName's).  I want to pivot (unpivot?) the data so each ActivityName is a column heading with the cooresponding date in each column.  Like this:

WorkIDRequirementsDevelopmentTestingProduction Deployement
1002/1/20252/15/20253/1/20253/10/2025
1052/3/2025   

 

WorkID is in TableA, the ActivityName and ActivityDate's are in TableB.

 

I want to do this with the DAX language.  I've found somethings about doing this, but I can't figure out how to do it with dates.  Any suggestions?  Thanks!

  • Hi Richtpt,

    I hope you are doing well 😀❤️

     

    Ok since you are working with a semantic model and need this for a paginated report...here is my solution:

     

    Using SUMMARIZE and SELECTEDVALUE :

    In your paginated report when you set up the dataset that uses the semantic model as Data source use this DAX query:

    EVALUATE
    SUMMARIZE(
        'TableA',
        'TableA'[WorkID],
        "Requirements",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Requirements"
            ),
        "Development",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Development"
            ),
        "Testing",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Testing"
            ),
        "Production Deployment",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Production Deployment"
            )
    )
    ORDER BY 'TableA'[WorkID]

     

    If you prefer better performance you can try this:

    Pivoted Data =
    VAR RequirementsTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Requirements"
        )
    VAR DevelopmentTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Development"
        )
    VAR TestingTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Testing"
        )
    VAR ProductionTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Production Deployment"
        )
    
    RETURN
    SUMMARIZE(
        'TableA',
        'TableA'[WorkID],
        "Requirements",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                RequirementsTable
            ),
        "Development",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                DevelopmentTable
            ),
        "Testing",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                TestingTable
            ),
        "Production Deployment",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                ProductionTable
            )
    )
    • Do not forget to change the tables names (based on yours) from your semantic model
    • Make sure the activity names exactly match your data
    • Use the first query with "EVALUATE" in your dataset connection (For Paginated Reports)

    Execute this DAX query in your paginated report dataset (It will return the pivoted data exactly as you wanted)

    WorkIDRequirementsDevelopmentTestingProduction Deployement
    1002/1/20252/15/20253/1/20253/10/2025
    1052/3/2025   

     

    if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.

4 Replies

  • Hi Richtpt,

    I hope you are doing well 😀❤️

     

    Ok since you are working with a semantic model and need this for a paginated report...here is my solution:

     

    Using SUMMARIZE and SELECTEDVALUE :

    In your paginated report when you set up the dataset that uses the semantic model as Data source use this DAX query:

    EVALUATE
    SUMMARIZE(
        'TableA',
        'TableA'[WorkID],
        "Requirements",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Requirements"
            ),
        "Development",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Development"
            ),
        "Testing",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Testing"
            ),
        "Production Deployment",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                'TableB'[ActivityName] = "Production Deployment"
            )
    )
    ORDER BY 'TableA'[WorkID]

     

    If you prefer better performance you can try this:

    Pivoted Data =
    VAR RequirementsTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Requirements"
        )
    VAR DevelopmentTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Development"
        )
    VAR TestingTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Testing"
        )
    VAR ProductionTable =
        FILTER(
            'TableB',
            'TableB'[ActivityName] = "Production Deployment"
        )
    
    RETURN
    SUMMARIZE(
        'TableA',
        'TableA'[WorkID],
        "Requirements",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                RequirementsTable
            ),
        "Development",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                DevelopmentTable
            ),
        "Testing",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                TestingTable
            ),
        "Production Deployment",
            CALCULATE(
                SELECTEDVALUE('TableB'[ActivityDate]),
                ProductionTable
            )
    )
    • Do not forget to change the tables names (based on yours) from your semantic model
    • Make sure the activity names exactly match your data
    • Use the first query with "EVALUATE" in your dataset connection (For Paginated Reports)

    Execute this DAX query in your paginated report dataset (It will return the pivoted data exactly as you wanted)

    WorkIDRequirementsDevelopmentTestingProduction Deployement
    1002/1/20252/15/20253/1/20253/10/2025
    1052/3/2025   

     

    if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.
    • Richtpt's avatar
      Richtpt
      Icon for Helper I rankHelper I

      Thanks!!  I went with a version of the first query and it's working great!

       

      Another follow-up question.  What if WorkID from TableA doesn't have any Activities in TableB?  I still want the WorkID to display.

      • Ahmed-Elfeel's avatar
        Ahmed-Elfeel
        Icon for Super User rankSuper User

        Excellent Richtpt!

        I am glad the first query worked great! 😊

         

        For your follow-up question :

        yes the query already handles that perfectly! The way you have it written will still display WorkIDs even if they have no activities in TableB

         

        The SUMMARIZE() function starts with TableA (which contains all WorkIDs) and then for each activity column it uses CALCULATE() with SELECTEDVALUE()

        • If no matching activity exists SELECTEDVALUE() returns Blank
        • The WorkID still appears in the results from TableA
        • The activity columns will just show as blank or null

        So the output will be something like this:

        WorkIDRequirements Development Testing ProductionDeployment 
        1002/1/20252/15/20253/1/20253/10/2025
        1052/3/2025nullnullnull
        110nullnullnullnull

         

        I hope this was helpful for you 😅❤️

        Best Regards!