Forum Discussion

Haripriya_9's avatar
Haripriya_9
Frequent Visitor
3 years ago
Solved

Apply Transformations

Hi All,  What kind of transformations we need to apply  on Input table to get Output table format :  Input : Column1Column2Column3Column4   Jun-19 Jul-19 Aug-19 CountryA       Ca...
  • edhans's avatar
    3 years ago

    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.

  • edhans's avatar
    edhans
    3 years ago

    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.

  • edhans's avatar
    edhans
    3 years ago

    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.