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
PurpleTrousers
New Member

Complex (in my opinion!) data transpose and filter

Hello - I am a beginner in PowerBi, I have watched a number of videos and searched for the solution to my issue with no luck. I am trying to manipulate my input data which is of a similar format to the dummy data below (but with many more columns):

image.png

I would like to be able to filter on the date, option and category and return the associated volume and price in a format to plot a line graph with the output, which I assume should look something like this:

 

image.png

(I would also like to sort by vol)

I have spent some time trying to figure out how to do this without any luck so was hoping someone will take pity and point me in the right direction 🤔

 

Thanks in advance 

1 ACCEPTED SOLUTION
rohit_singh
Solution Sage
Solution Sage

Hi @PurpleTrousers ,

Please try the steps given below in Power Query :

1) Select all columns except Date, Option and Category, and then click on "Transform" --> "Unpivot columns"

rohit_singh_0-1652972009169.png

rohit_singh_1-1652972038921.png

You will see that the column headers for price and volume are now in rows.

2) Split the attribute column from non-digit to digit. This will split the column into a text column and a number column. We only need the text column named "Attribute.1" since that has price and volume headers

 

rohit_singh_3-1652972174346.png

 

3) Finally, select the "Attribute.1" column and pivot it over the "Value" column using the sum aggregation. 

rohit_singh_5-1652972625745.png

Here is the final result :

rohit_singh_2-1652972154072.png

You can now load this table to the report view and filter by date, option and category.

 

Here is the sample M-code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyMjJR0lQyB2hNJGRNOxOliMcaLcGCPquMaIOq4xpo5rjMlzTSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Option = _t, Category = _t, Price1 = _t, Volume1 = _t, Price2 = _t, Volume2 = _t, Price3 = _t, Volume3 = _t, Price4 = _t, Volume4 = _t, Price5 = _t, Volume5 = _t, Price6 = _t, Volume6 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Option", Int64.Type}, {"Category", type text}, {"Price1", Int64.Type}, {"Volume1", Int64.Type}, {"Price2", Int64.Type}, {"Volume2", Int64.Type}, {"Price3", Int64.Type}, {"Volume3", Int64.Type}, {"Price4", Int64.Type}, {"Volume4", Int64.Type}, {"Price5", Int64.Type}, {"Volume5", Int64.Type}, {"Price6", Int64.Type}, {"Volume6", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Option", "Category"}, "Attribute", "Value"),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Attribute.1", "Attribute.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Character Transition",{"Attribute.2"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute.1]), "Attribute.1", "Value", List.Sum)
in
    #"Pivoted Column"

 

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

View solution in original post

3 REPLIES 3
PurpleTrousers
New Member

Perfect thank you

rohit_singh
Solution Sage
Solution Sage

Hi @PurpleTrousers ,

Please try the steps given below in Power Query :

1) Select all columns except Date, Option and Category, and then click on "Transform" --> "Unpivot columns"

rohit_singh_0-1652972009169.png

rohit_singh_1-1652972038921.png

You will see that the column headers for price and volume are now in rows.

2) Split the attribute column from non-digit to digit. This will split the column into a text column and a number column. We only need the text column named "Attribute.1" since that has price and volume headers

 

rohit_singh_3-1652972174346.png

 

3) Finally, select the "Attribute.1" column and pivot it over the "Value" column using the sum aggregation. 

rohit_singh_5-1652972625745.png

Here is the final result :

rohit_singh_2-1652972154072.png

You can now load this table to the report view and filter by date, option and category.

 

Here is the sample M-code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyMjJR0lQyB2hNJGRNOxOliMcaLcGCPquMaIOq4xpo5rjMlzTSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Option = _t, Category = _t, Price1 = _t, Volume1 = _t, Price2 = _t, Volume2 = _t, Price3 = _t, Volume3 = _t, Price4 = _t, Volume4 = _t, Price5 = _t, Volume5 = _t, Price6 = _t, Volume6 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Option", Int64.Type}, {"Category", type text}, {"Price1", Int64.Type}, {"Volume1", Int64.Type}, {"Price2", Int64.Type}, {"Volume2", Int64.Type}, {"Price3", Int64.Type}, {"Volume3", Int64.Type}, {"Price4", Int64.Type}, {"Volume4", Int64.Type}, {"Price5", Int64.Type}, {"Volume5", Int64.Type}, {"Price6", Int64.Type}, {"Volume6", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Option", "Category"}, "Attribute", "Value"),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Attribute.1", "Attribute.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Character Transition",{"Attribute.2"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute.1]), "Attribute.1", "Value", List.Sum)
in
    #"Pivoted Column"

 

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

Hi @PurpleTrousers ,

If this solution worked for you, please mark this answer as the solution to help others as well. 
Appreciate your kudos!

Kind regards,

Rohit

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.