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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Jlbaenlo
Frequent Visitor

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 5
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
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,

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?



Proud to be a Super User!

daxformatter.com makes life EASIER!
danextian
Super User
Super User

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"

 

danextian_0-1697795556235.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Ritaf1983
Super User
Super User

Hi @Jlbaenlo 
Can you please show how you need the result to look?

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

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.

Top Solution Authors