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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
kirbynguyen
Helper II
Helper II

To Pivot or not to Pivot

Hello,

 

I am wondering what is more efficient for Power BI.

I have a table that has one row for every date and each column is a numerical value. Some columns are just price columns and some columns are region specific. This is just an example and there are going to be more columns than just the region price that are region specific, such as volume, temperature, etc.

 

Date                  Price  NA Price  EU Price  FE Price
1/1/20211.52.33.36.6
1/2/20211.42.63.66.3
1/3/20211.62.73.26.9
1/4/20211.42.53.67.3
1/5/20211.52.23.67.1
1/6/20211.62.73.46.5
1/7/20211.62.73.66.8
1/8/20211.62.43.27.3
1/9/20211.42.63.36.9

 

Would it be better to pivot the data so there is a date column, a region column and the price column? Doing it this way creates more rows, but reduces the amount of columns. Like this:

Date                 Region  Region Price  Price
1/1/2021NA2.31.5
1/1/2021EU3.31.5
1/1/2021FE6.61.5
1/2/2021NA2.61.4
1/2/2021EU3.61.4
1/2/2021FE6.31.4

 

Or how about if I create 3 separate calculated tables for each region instead? So each calculated table will be just 2 columns, with the date and the price for that region.

 

Which would be the best data model from these, or is there an even better way that I didn't mention?

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@kirbynguyen , the second one as a single table is much better.

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

I'd recommend the following version:

 

DateRegion  Price
1/1/2021NA2.3
1/1/2021EU3.3
1/1/2021FE6.6
1/1/2021None1.5
1/2/2021NA2.6
1/2/2021EU3.6
1/2/2021FE6.3
1/2/2021  None1.4
amitchandak
Super User
Super User

@kirbynguyen , the second one as a single table is much better.

@amitchandak 

Follow Up Question:

 

Is there a way to easily pivot this into the desired output without having to create a lot of extra steps to get rid of duplicate rows:

Date         NA Price  EU Price  FE Price  NA Temp  EU Temp  FE Temp
1/1/2021    2.3             3.3         6.6            10             40            60
1/2/2021    2.6             3.6         6.3            20             50            60
1/3/2021    2.7             3.2         6.9            30             40            60
1/4/2021    2.5             3.6         7.3            10             50            60
1/5/2021    2.2             3.6         7.1            20             40            60
1/6/2021    2.7             3.4         6.5            30             50            60
1/7/2021    2.7             3.6         6.8            10             40            60
1/8/2021    2.4             3.2         7.3            20             50            60
1/9/2021    2.6             3.3         6.9            30             40            60


Desired:

Date        Region   Region Price   Region Temp
1/1/2021  NA               2.3                    10
1/1/2021  EU                3.3                    40
1/1/2021  FE                 6.6                    60
1/2/2021  NA                2.6                    20
1/2/2021  EU                 3.6                   50
1/2/2021  FE                  6.3                   60


The way I am doing it now is that I'll unpivot the columns and then I'll create a calculated column to match the regions and then filter out the one's that don't match. Seems like a lot of extra steps, but I'm not sure if there is a better way to do it

Unpivot all except date, split the attribute column and then pivot on the Temp/Price column.

 

let
    Source = <....>,
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {"Date"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Region", "Column"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[Column]), "Column", "Value")
in
    #"Pivoted Column"

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.