Forum Discussion

suparnababu8's avatar
suparnababu8
Icon for Super User rankSuper User
2 years ago
Solved

Unpivoting and Pivoting Table by using DAX

Hi Fabric Community, I have a table as metioned below...   This is my Input table to Power BI   Region_East Region_North Region_South Region_West Country_East Country_North Country_Sout...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi suparnababu8 ,

    I created a sample pbix fie(see the attachment), it include both DAX and Power Query Editor methods. Please check if that is what you want.

    DAX:

    ResultTable =
    VAR _tab =
        UNION (
            SELECTCOLUMNS (
                InputTable,
                "Directions", "East",
                "Locations", "Region",
                "Sales", [Region_East]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "East",
                "Locations", "Country",
                "Sales", [Country_East]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "East",
                "Locations", "State",
                "Sales", [State_East]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "East",
                "Locations", "City",
                "Sales", [City_East]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "East",
                "Locations", "Street",
                "Sales", [Street_East]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "North",
                "Locations", "Region",
                "Sales", [Region_North]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "North",
                "Locations", "Country",
                "Sales", [Country_North]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "North",
                "Locations", "State",
                "Sales", [State_North]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "North",
                "Locations", "City",
                "Sales", [City_North]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "North",
                "Locations", "Street",
                "Sales", [Street_North]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "South",
                "Locations", "Region",
                "Sales", [Region_South]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "South",
                "Locations", "Country",
                "Sales", [Country_South]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "South",
                "Locations", "State",
                "Sales", [State_South]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "South",
                "Locations", "City",
                "Sales", [City_South]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "South",
                "Locations", "Street",
                "Sales", [Street_South]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "West",
                "Locations", "Region",
                "Sales", [Region_West]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "West",
                "Locations", "Country",
                "Sales", [Country_West]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "West",
                "Locations", "State",
                "Sales", [State_West]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "West",
                "Locations", "City",
                "Sales", [City_West]
            ),
            SELECTCOLUMNS (
                InputTable,
                "Directions", "West",
                "Locations", "Street",
                "Sales", [Street_West]
            )
        )
    RETURN
        SUMMARIZE (
            _tab,
            [Directions],
            [Locations],
            "Sales",
                SUMX (
                    FILTER (
                        _tab,
                        [Directions] = EARLIER ( [Directions] )
                            && [Locations] = EARLIER ( [Locations] )
                    ),
                    [Sales]
                )
        )

    Power Query Editor:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lVBBDsAgCPuLZw8K4txbjP//xoLo4EDMlhiipZbS3gOkFGIoswJxxeIjcv/L9/ER++rgrFkq+Eg2M6UL6PPB8Kl6OjwZkN5NMqreUrW+jfvFt5sks/9SUGW6lF/3zqSO0ag6iLj4zC8HHZ6coZEEwWibbtst2ciDJKKZ2r2zSfrndMZ4AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Region_East = _t, Region_North = _t, Region_South = _t, Region_West = _t, Country_East = _t, Country_North = _t, Country_South = _t, Country_West = _t, State_East = _t, State_North = _t, State_South = _t, State_West = _t, City_East = _t, City_North = _t, City_South = _t, City_West = _t, Street_East = _t, Street_North = _t, Street_South = _t, Street_West = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Region_East", Int64.Type}, {"Region_North", Int64.Type}, {"Region_South", Int64.Type}, {"Region_West", Int64.Type}, {"Country_East", Int64.Type}, {"Country_North", Int64.Type}, {"Country_South", Int64.Type}, {"Country_West", Int64.Type}, {"State_East", Int64.Type}, {"State_North", Int64.Type}, {"State_South", Int64.Type}, {"State_West", Int64.Type}, {"City_East", Int64.Type}, {"City_North", Int64.Type}, {"City_South", Int64.Type}, {"City_West", Int64.Type}, {"Street_East", Int64.Type}, {"Street_North", Int64.Type}, {"Street_South", Int64.Type}, {"Street_West", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
        #"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Index] <> 5)), 
        #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Filtered Rows", {"Region_East", "Region_North", "Region_South", "Region_West", "Country_East", "Country_North", "Country_South", "Country_West", "State_East", "State_North", "State_South", "State_West", "City_East", "City_North", "City_South", "City_West", "Street_East", "Street_North", "Street_South", "Street_West"}, "Attribute", "Sales"),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Only Selected Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Locations", "Directions"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Locations", type text}, {"Directions", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type1", {"Directions", "Locations"}, {{"Sales", each List.Sum([Sales]), type number}})
    in
        #"Grouped Rows"

    Best Regards