Forum Discussion

mariajuliao's avatar
mariajuliao
Frequent Visitor
4 years ago
Solved

Transposing / Transforming data with DAX

I want to transform the data from this format:

IDCaseNumberAsset1Asset2
AABB12
CCDD34

 

To this format:

IDCaseNumberAssetValue
AABBAsset11
AABBAsset22
CCDDAsset13
CCDDAsset24

 

Since columns Asset1, Asset2, Asset3, etc. are calculated column in DAX I cannot use PowerQuery to unpivot the data. Let me know any solutions with DAX. 

 

Thanks!!

5 Replies

  • Hi mariajuliao 

     

    Try this code to create a new table with DAX:

     

    Table 2 =
    VAR _A =
        SUMMARIZE ( 'Table', 'Table'[ID], 'Table'[CaseNumber] )
    VAR _B =
        SELECTCOLUMNS ( { "Asset1", "Asset2" }, "Asset", [Value] )
    VAR _C =
        CROSSJOIN ( _A, _B )
    VAR _ASSET =
        ADDCOLUMNS (
            _C,
            "Value",
                IF (
                    [Asset] = "Asset1",
                    CALCULATE (
                        MAX ( 'Table'[Asset1] ),
                        FILTER (
                            'Table',
                            'Table'[ID] = EARLIER ( [ID] )
                                && 'Table'[CaseNumber] = EARLIER ( [CaseNumber] )
                        )
                    ),
                    CALCULATE (
                        MAX ( 'Table'[Asset2] ),
                        FILTER (
                            'Table',
                            'Table'[ID] = EARLIER ( [ID] )
                                && 'Table'[CaseNumber] = EARLIER ( [CaseNumber] )
                        )
                    )
                )
        )
    RETURN
        _ASSET
    

     

     

    Output:

     

     

     

    Sample file attached.

    If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
    Appreciate your Kudos!!
    LinkedIn: 
    www.linkedin.com/in/vahid-dm/

     

     

    • mariajuliao's avatar
      mariajuliao
      Frequent Visitor

      This was very helpful!!

       

      One other question -- Some of the values under the Asset1, Asset2 columns are blank. Is there any way that I can ommit those? 

    • mariajuliao's avatar
      mariajuliao
      Frequent Visitor

      This was very helpful!!

       

      One other question -- Some of the values under the Asset1, Asset2 columns are blank. Is there any way that I can ommit those? 

      • AlexisOlson's avatar
        AlexisOlson
        Super User

        You can wrap a FILTER around the whole thing.

         

        FILTER ( 
            UNION ( 
                [...]
            ),
            NOT ISBLANK ( [Asset] )
        )