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
Anonymous
Not applicable

Split row into different categories from colum name

I have a table that contains multiple columns with their named having either the suffix `_EXPECTED` or `_ACTUAL`. For example, I'm looking at my sold items from my `SoldItems` Table and I have the following columns: `APPLES_EXPECTED`, `BANANAS_EXPECTED`, `KIWIS_EXPECTED`, `APPLES_ACTUAL`, `BANANAS_ACTUAL`, `KIWIS_ACTUAL` (The Identifier of the table is the date, so we have results per date). I want to show that data in a table form, something like this (For example, if the selected date is '2019-12-23'):

 

Sold ItemsExpectedActual
Apples1015
Kiwis21
Bananas85

 

Here is what the datasource table looks like:

DateAPPLES_EXPECTEDKIWIS_EXPECTEDBANANAS_EXPECTEDAPPLES_ACTUALKIWIS_ACTUALBANANAS_ACTUAL
2019-12-2310281515
2019-12-225145482

 

How can I manage something like this in Power BI Desktop ? I tried playing with the matrix/table visualization, however, I can't figure out a way to merge all the expected and actual columns together.

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@Anonymous you can use following script to transform the data , go to query editor, add blank query and paste following code

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ1NNI1MlbSUTI0ABJGQGwB4piCCCA2VYrVQVJoBBYCSplAGSZQDUZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, APPLES_EXPECTED = _t, KIWIS_EXPECTED = _t, BANANAS_EXPECTED = _t, APPLES_ACTUAL = _t, KIWIS_ACTUAL = _t, BANANAS_ACTUAL = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"APPLES_EXPECTED", Int64.Type}, {"KIWIS_EXPECTED", Int64.Type}, {"BANANAS_EXPECTED", Int64.Type}, {"APPLES_ACTUAL", Int64.Type}, {"KIWIS_ACTUAL", Int64.Type}, {"BANANAS_ACTUAL", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "Attribute", "Value"),
    #"Inserted Text Before Delimiter" = Table.AddColumn(#"Unpivoted Other Columns", "Fruit", each Text.BeforeDelimiter([Attribute], "_"), type text),
    #"Inserted Text After Delimiter" = Table.AddColumn(#"Inserted Text Before Delimiter", "Type", each Text.AfterDelimiter([Attribute], "_"), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Text After Delimiter",{"Attribute"})
in
    #"Removed Columns"

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

7 REPLIES 7

Hi @Anonymous ,

 

use Power Query.

1. Unpivot Columns

2. Split Column by Delimiter

3. Pivot Columns

 

You may download my PBIX file from here.
Hope this helps.

 

If I answered your question, please mark my post as solution, this will also help others.

Please give Kudos for support.

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


Anonymous
Not applicable

Will this work if I have more columns other than the ones mentionned above ?

parry2k
Super User
Super User

@Anonymous you can use following script to transform the data , go to query editor, add blank query and paste following code

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ1NNI1MlbSUTI0ABJGQGwB4piCCCA2VYrVQVJoBBYCSplAGSZQDUZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, APPLES_EXPECTED = _t, KIWIS_EXPECTED = _t, BANANAS_EXPECTED = _t, APPLES_ACTUAL = _t, KIWIS_ACTUAL = _t, BANANAS_ACTUAL = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"APPLES_EXPECTED", Int64.Type}, {"KIWIS_EXPECTED", Int64.Type}, {"BANANAS_EXPECTED", Int64.Type}, {"APPLES_ACTUAL", Int64.Type}, {"KIWIS_ACTUAL", Int64.Type}, {"BANANAS_ACTUAL", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "Attribute", "Value"),
    #"Inserted Text Before Delimiter" = Table.AddColumn(#"Unpivoted Other Columns", "Fruit", each Text.BeforeDelimiter([Attribute], "_"), type text),
    #"Inserted Text After Delimiter" = Table.AddColumn(#"Inserted Text Before Delimiter", "Type", each Text.AfterDelimiter([Attribute], "_"), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Text After Delimiter",{"Attribute"})
in
    #"Removed Columns"

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

I do not want to add a new table however, I want to keep it in my own table to keep the filters set by the rest of the visuals

@Anonymous you are not adding new table, but you are transforming your existing table for better design, and get you what you are looking for. It is all about best practices, don't get caught into just getting the work done but think about how it will scale.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@Anonymous yes solution will work with new products, as far as the format of your data stays the same, it doesn't matter if there are new products, more rows etc.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@Anonymous you will get following output

 

image.png



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.