Saturday, November 4, 2023

How to Transpose Rows to Columns using Power Query

How to Transpose Rows to Columns using M-Query in Power BI
Scenario:
Suppose we have a Sample dataset as follows:

Now we can transpose the Rows to Columns using the following M-Query:
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Ncq5DQAgDMDAXVLTkGcCNqBF2X8NCFYqSyefI3uuKaOixCTHY4UVrnw22GDr22GHvTnggCuZFw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t]),

TransData
Table.FromColumns(Table.ToRows(Source), {"TransCol1","TransCol2","TransCol3", "TransCol4", "TransCol5"}),

MultiCustCols
    Table.AddColumn(TransData, "Stats", each 
                    [
                    RowsCount = Table.RowCount(TransData),
                    ColumnsCount = Table.ColumnCount(TransData)
                    ]
                ),
ExpandCols = Table.ExpandRecordColumn(MultiCustCols, "Stats", {"RowsCount", "ColumnsCount"}, {"RowsCount", "ColumnsCount"})
in
    ExpandCols

Result:

Thanks, TAMATAM

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog