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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Haripriya_9
Frequent Visitor

Apply Transformations

Hi All, 

What kind of transformations we need to apply  on Input table to get Output table format : 
Input :
Column1Column2Column3Column4

 Jun-19Jul-19Aug-19
CountryA   
Category1111213
Category2222324
Category3333435
CountryB   
Category1101102103
Category2122123124
Category3132133134


to get desired format output as shown below

Output :

Categorization DateCountry ACountry B
Category 119-Aug13103
Category 119-Jul12102
Category 119-Jun11101
Category 219-Aug24124
Category 219-Jul23123
Category 219-Jun22122
Category 319-Aug35134
Category 319-Jul34133
Category 319-Jun33132
3 ACCEPTED SOLUTIONS
edhans
Super User
Super User

Here is the code for one way to do it:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRMrTU9SrNgzFyIAzH0nSlWJ1oJef80rySokpHoCgUgUUTS1LT84sqDUGKwYQRiDBGkQQJGYEJYxBhgiIJEjIGEyYgwhTZMic8lhlASLB1Bpj2GRpBSGMwiWmloTHEoRA2UD4WAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Column1", "Column2", "Column3", "Column4"}),
    #"Transposed Table" = Table.Transpose(#"Replaced Value"),
    #"Filled Down" = Table.FillDown(#"Transposed Table",{"Column2", "Column6"}),
    #"Transposed Table1" = Table.Transpose(#"Filled Down"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Column1"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Country", each if (try Value.Is(Number.From(Text.Start([Value], 1)), Int64.Type))[HasError] then [Value] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "New Value", each if [Country] = null then [Value] else null),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom1",{{"New Value", Int64.Type}}),
    #"Filled Down1" = Table.FillDown(#"Changed Type",{"Country"}),
    #"Pivoted Column" = Table.Pivot(#"Filled Down1", List.Distinct(#"Filled Down1"[Country]), "Country", "New Value"),
    #"Filled Down2" = Table.FillDown(#"Pivoted Column",{"CountryB"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down2", each ([CountryA] <> null)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute", "Date"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Value"})
in
    #"Removed Columns"

 

This is the end result.

2023-05-23 08_28_17-Table - Power Query Editor.png



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

THere was no PBIX. Sorry - I did that in Excel's Power Query, but I forgot to tell you how to take my M code above and make use if it directly.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

Oh, one other clarification. For the Fill Down steps to work, the "cells" must be null, not empty. Null will show the word null in the cell, and empty will show a blank white space. I did a Find/Replace to get rid of the empty values. Just leave the "Find" box blank, and type the word null in the Replace With box if your data has empty instead of null. That is what my Replaced Values step is doing - ensuring the Fill Down works as expected later on.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

6 REPLIES 6
Haripriya_9
Frequent Visitor

Thank you for the quick response.  I tried above code and working as expected.

Excellent @Haripriya_9 - glad I was able to assist.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Haripriya_9
Frequent Visitor

Hi @edhans ,

Thank you for the response.. if possible could you please share the pbix file with above transformations. It wil be helpful.

Thanks in advance

THere was no PBIX. Sorry - I did that in Excel's Power Query, but I forgot to tell you how to take my M code above and make use if it directly.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Oh, one other clarification. For the Fill Down steps to work, the "cells" must be null, not empty. Null will show the word null in the cell, and empty will show a blank white space. I did a Find/Replace to get rid of the empty values. Just leave the "Find" box blank, and type the word null in the Replace With box if your data has empty instead of null. That is what my Replaced Values step is doing - ensuring the Fill Down works as expected later on.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
edhans
Super User
Super User

Here is the code for one way to do it:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRMrTU9SrNgzFyIAzH0nSlWJ1oJef80rySokpHoCgUgUUTS1LT84sqDUGKwYQRiDBGkQQJGYEJYxBhgiIJEjIGEyYgwhTZMic8lhlASLB1Bpj2GRpBSGMwiWmloTHEoRA2UD4WAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Column1", "Column2", "Column3", "Column4"}),
    #"Transposed Table" = Table.Transpose(#"Replaced Value"),
    #"Filled Down" = Table.FillDown(#"Transposed Table",{"Column2", "Column6"}),
    #"Transposed Table1" = Table.Transpose(#"Filled Down"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Column1"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Country", each if (try Value.Is(Number.From(Text.Start([Value], 1)), Int64.Type))[HasError] then [Value] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "New Value", each if [Country] = null then [Value] else null),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom1",{{"New Value", Int64.Type}}),
    #"Filled Down1" = Table.FillDown(#"Changed Type",{"Country"}),
    #"Pivoted Column" = Table.Pivot(#"Filled Down1", List.Distinct(#"Filled Down1"[Country]), "Country", "New Value"),
    #"Filled Down2" = Table.FillDown(#"Pivoted Column",{"CountryB"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down2", each ([CountryA] <> null)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute", "Date"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Value"})
in
    #"Removed Columns"

 

This is the end result.

2023-05-23 08_28_17-Table - Power Query Editor.png



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors