Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
seanmcc
Helper I
Helper I

Pivot/Unpivot calculated columns using DAX

Hi Guys,

 

I am looking for some assistance in pivoting and unpivoting a table without using power query.

I have done some research and found that I could possibly use UNION and SUMMARIZECOLUMNS funtions, however I am struggling to get my head around how to pivot and unpivot within same table. I am unsure if this is possible.

 

I have a table in desktop where I have a number of calculated columns ( for example, 5 min relative and 10 min relative).

 

Current Table               
                
TeamSeasonCompetitionGameVenueFixtureDateResultGoals ForGoals AgainstPosition GroupPositionPlayerAttribute5 min Relative10 min Relative
Manchester United2023/24Premier LeagueGame 1HomeManchester City (H)01/09/2022Win10DefenderCBHarry MaguireTotal Distance106.299.5
Manchester United2023/24Premier LeagueGame 1HomeManchester City (H)01/09/2022Win10DefenderCBHarry MaguireSprint Distance2.41.9

 

What I am trying to do from the original table is to pivot 'Attributes' into columns and unpivot the columns '5 min relative' and '10 min relative' into a column called 'Time Period'. See below for the desired outcome.

 

Desired Table               
                
TeamSeasonCompetitionGameVenueFixtureDateResultGoals ForGoals AgainstPosition GroupPositionPlayerTime PeriodTotal DistanceSprint Distance
Manchester United2023/24Premier LeagueGame 1HomeManchester City (H)01/09/2022Win10DefenderCBHarry Maguire5 min Relative106.22.4
Manchester United2023/24Premier LeagueGame 1HomeManchester City (H)01/09/2022Win10DefenderCBHarry Maguire10 min Relative99.51.9

 

If there is a way to do this with DAX, it would be great. Otherwise, would there be another way of acheiving the desired outcome?

 

Thank you in advance.

Sean

4 REPLIES 4
seanmcc
Helper I
Helper I

Hi @lbendlin, yes the attribute field names are always the same and present. The columns '5 min Relative', '10 min Relative' etc will always be present, however not always have values within them.

 

I hope that helps.

 

Kind Regards,

Sean

This DAX is based on the sample data you provided.  The CALCULATE filters will need to be adjusted for scenarios with more than one player etc.

 

Table2 =
UNION (
    SUMMARIZE (
        'Table',
        [Team],
        [Season],
        [Competition],
        [Venue],
        [Fixture],
        [Date],
        [Result],
        [Goals For],
        [Goals Against],
        [Position Group],
        [Position],
        'Table'[Player],
        "Time Period", "5 min Relative",
        "Total Distance",
            CALCULATE (
                MAX ( 'Table'[5 min Relative] ),
                'Table'[Attribute] = "Total Distance"
            ),
        "Sprint Distance",
            CALCULATE (
                MAX ( 'Table'[5 min Relative] ),
                'Table'[Attribute] = "Sprint Distance"
            )
    ),
    SUMMARIZE (
        'Table',
        [Team],
        [Season],
        [Competition],
        [Venue],
        [Fixture],
        [Date],
        [Result],
        [Goals For],
        [Goals Against],
        [Position Group],
        [Position],
        'Table'[Player],
        "Time Period", "10 min Relative",
        "Total Distance",
            CALCULATE (
                MAX ( 'Table'[10 min Relative] ),
                'Table'[Attribute] = "Total Distance"
            ),
        "Sprint Distance",
            CALCULATE (
                MAX ( 'Table'[10 min Relative] ),
                'Table'[Attribute] = "Sprint Distance"
            )
    )
)

Here's another, slightly more concise way:

Table2 =
CROSSJOIN (
    SUMMARIZE (
        'Table',
        [Team],
        [Season],
        [Competition],
        [Venue],
        [Fixture],
        [Date],
        [Result],
        [Goals For],
        [Goals Against],
        [Position Group],
        [Position],
        'Table'[Player]
    ),
    UNION (
        ROW (
            "Time Period", "5 min Relative",
            "Total Distance",
                CALCULATE (
                    MAX ( 'Table'[5 min Relative] ),
                    'Table'[Attribute] = "Total Distance"
                ),
            "Sprint Distance",
                CALCULATE (
                    MAX ( 'Table'[5 min Relative] ),
                    'Table'[Attribute] = "Sprint Distance"
                )
        ),
        ROW (
            "Time Period", "10 min Relative",
            "Total Distance",
                CALCULATE (
                    MAX ( 'Table'[10 min Relative] ),
                    'Table'[Attribute] = "Total Distance"
                ),
            "Sprint Distance",
                CALCULATE (
                    MAX ( 'Table'[10 min Relative] ),
                    'Table'[Attribute] = "Sprint Distance"
                )
        )
    )
)
lbendlin
Super User
Super User

Do you need this to be dynamic or do you know that the attribute and time period field names/values are always the same and always present?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.