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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
suparnababu8
Super User
Super User

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_EastRegion_NorthRegion_SouthRegion_WestCountry_EastCountry_NorthCountry_SouthCountry_WestState_EastState_NorthState_SouthState_WestCity_EastCity_NorthCity_SouthCity_WestStreet_EastStreet_NorthStreet_SouthStreet_West
200400250340400250340250200400250340400250340250400250340250
400300100120300100120100400300230120300100220100560100120100
235200130230230150400200250200150340200340430235200570150650
450300350200300350200350450300350200300350200400300350200350
1285120083089012308501060900130012009801000120010401190985146012708101350


I need result table as mentioned below

 

DirectionsLocationsSales
EastRegion1285
EastCountry1230
EastState1300
EastCity1200
EastStreet1460
NorthRegion1200
NorthCountry850
NorthState1200
NorthCity1040
NorthStreet1270
SouthRegion830
SouthCountry1060
SouthState980
SouthCity1190
SouthStreet810
WestRegion890
WestCountry900
WestState1000
WestCity985
WestStreet1350

 

I don't want to use Power Query editor for this excersize.. 

I need solution for this by using DAX table

 

Thanks in advance

 

@comms  @lbendlin @Daniel @cst_bi  @Anonymous  @JDavid 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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"

vyiruanmsft_0-1710403824104.png

Best Regards

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

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"

vyiruanmsft_0-1710403824104.png

Best Regards

suparnababu8
Super User
Super User

from Input table bottom row values representing Total of each column

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.