Forum Discussion

Jlbaenlo's avatar
Jlbaenlo
Frequent Visitor
2 years ago

DAX create a table pivot like

I have the following case,

I have a table containing the following info :

BusField, Region, Sales1, Sales2, Sales3, Year

B1, R1, 100, 200, 300, 2020

B1, R1, 200, 300, 200, 2021 

B1, R1, 100, 200, 300, 2022

B1, R1, 200, 300, 200, 2023 

B1, R2, 200, 200, 100, 2020

B1, R2, 300, 300, 200, 2021 

B1, R2, 100, 100, 200, 2022

B1, R2, 200, 200, 100, 2023 

 

and I want to pivot it using DAX to look like

BusField, Region, Type, 2020,2021,2022,2023 etc..

where in the column type, we'll find Sales1, Sales2, Sales3

and under each year, we'll find the sum per BusField, Region, Type

 

How can I do that ?

 

Many thanks,

 

5 Replies

  • Hi Jlbaenlo ,

     

    Im unsure how you want your final table to look like as your description is not enought but if you want something like below, here's a sample M code you can paste into a blank query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUorViVZyMtRRCAJiQwMDHQUjEGEMYRkZgOVRFCHLQxQZKmCqwjTKiBijjNGNMoLLGcANxXCVEdQU/K4yghpgiKwK3VVY7QO5KhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"BusField, Region, Sales1, Sales2, Sales3, Year" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"BusField, Region, Sales1, Sales2, Sales3, Year", type text}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each [#"BusField, Region, Sales1, Sales2, Sales3, Year"] <> null and [#"BusField, Region, Sales1, Sales2, Sales3, Year"] <> ""),
        #"Split Column by Delimiter" = let oldcolumn  = Table.ColumnNames(#"Filtered Rows"){0},
    newcolumn = Text.Split(oldcolumn, ","),
    newcolumn2 = List.Transform(newcolumn, Text.Trim)
    in
    Table.SplitColumn(#"Filtered Rows", "BusField, Region, Sales1, Sales2, Sales3, Year", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), newcolumn2),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Split Column by Delimiter", {"BusField", "Region", "Year"}, "Attribute", "Value"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", Int64.Type}})
    in
        #"Changed Type1"

     

     

  • Jlbaenlo's avatar
    Jlbaenlo
    Frequent Visitor

    I found a way in the end :

    TransformedTable =
    VAR __T1 =ADDCOLUMNS(
    SUMMARIZE(Test,'Test'[Business Field],'Test'[SD Region BF]),
    "Type", "Sales1",
    "2023" , CALCULATE( MAX('Test'[Sales1]) , TREATAS( {2023},Test[YEAR])),
    "2024" , CALCULATE( MAX('Test'[Sales1]) , TREATAS( {2024},Test[YEAR])),
    "2025" , CALCULATE( MAX('Test'[Sales1]) , TREATAS( {2025},Test[YEAR]))
    )
    VAR __T2 =ADDCOLUMNS(
    SUMMARIZE(Test,'Test'[Business Field],'Test'[SD Region BF]),
    "Type", "Sales2",
    "2023" , CALCULATE( MAX('Test'[Sales2]) , TREATAS( {2023},Test[YEAR])),
    "2024" , CALCULATE( MAX('Test'[Sales2]) , TREATAS( {2024},Test[YEAR])),
    "2025" , CALCULATE( MAX('Test'[Sales2]) , TREATAS( {2025},Test[YEAR]))
    )
    VAR __T2 =ADDCOLUMNS(
    SUMMARIZE(Test,'Test'[Business Field],'Test'[SD Region BF]),
    "Type", "Sales3",
    "2023" , CALCULATE( MAX('Test'[Sales3]) , TREATAS( {2023},Test[YEAR])),
    "2024" , CALCULATE( MAX('Test'[Sales3]) , TREATAS( {2024},Test[YEAR])),
    "2025" , CALCULATE( MAX('Test'[Sales3]) , TREATAS( {2025},Test[YEAR]))
    )
    RETURN
    UNION(__T1,__T2, __T3)

     

    The only issue now is that I need to hard code the year in my DAX... is there any clever way to treat Year in a list and use it there ?

    Many thanks,

    BR,

    • gmsamborn's avatar
      gmsamborn
      Super User

      Hi Jlbaenlo 

       

      I'm not sure how to get around your hard-coded Year problem in DAX.

       

      With the Power Query solution provided by danextian , this isn't a problem.

       

      Can I ask why you specified that this be done in DAX?

  • Jlbaenlo's avatar
    Jlbaenlo
    Frequent Visitor

    Original TEST table

    Business FieldRegionYearSales1Sales2Sales3
    THYB2023102030
    THYB20245101
    THYB2025689
    THYB20267510
    CARB2023112131
    CARB20246112
    CARB20257910
    CARB2026885
          
          
    after transform     
    Business FieldSD Region BFType202320242025
    CARBSales1    1167
    CARBSales221119
    CARBSales331210
    THYBSales11056
    THYBSales220108
    THYBSales33019