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
Brotedo
Helper I
Helper I

Table with Primary Key and Values Across Columns to values Down Columns

I have a data export that has a primary key and sales values for 12 months along columns, like so

KeyMonth1SalesMonth1DateMonth2SalesMonth2DateMonth3SalesMonth3Date
00134Jan 2022394Feb 2022346March 2022
00245Feb 202257March 20225668April 2022

and I want to make another table based on this that has multiple entries for the Key, and sales down 1 column, like this

KeyDateSales
001Jan 202234
001Feb 2022394
001March 2022346
002Feb 202245
002March 202257
002April 20225668

There are other columns in the original table that I want to keep there but not import to the new table.

How do I do this?

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwVNJRMjYBEl6JeQpGBkZGIL4lSMAtNQkuYGIGJH0TiyACsTognSAJE1NUhabmyOqAfDMzCyDlWADTGQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, Month1Sales = _t, Month1Date = _t, Month2Sales = _t, Month2Date = _t, Month3Sales = _t, Month3Date = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Key"}, "Attribute", "Date"),
    DateTbl = Table.SelectRows(#"Unpivoted Other Columns", each Text.EndsWith([Attribute],"Date")),
    DateTblIndex = Table.AddIndexColumn(DateTbl, "Index", 0, 1, Int64.Type),
    SalesTbl = Table.SelectRows(#"Unpivoted Other Columns", each Text.EndsWith([Attribute],"Sales")),
    SalesTblIndex = Table.AddIndexColumn(SalesTbl, "Index", 0, 1, Int64.Type),
    #"Merged Queries" = Table.NestedJoin(DateTblIndex, {"Index"}, SalesTblIndex, {"Index"}, "SalesTblIndex", JoinKind.LeftOuter),
    #"Expanded SalesTblIndex" = Table.ExpandTableColumn(#"Merged Queries", "SalesTblIndex", {"Date"}, {"Sales"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded SalesTblIndex",{"Attribute", "Index"})
in
    #"Removed Columns"

 

View solution in original post

1 REPLY 1
Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwVNJRMjYBEl6JeQpGBkZGIL4lSMAtNQkuYGIGJH0TiyACsTognSAJE1NUhabmyOqAfDMzCyDlWADTGQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, Month1Sales = _t, Month1Date = _t, Month2Sales = _t, Month2Date = _t, Month3Sales = _t, Month3Date = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Key"}, "Attribute", "Date"),
    DateTbl = Table.SelectRows(#"Unpivoted Other Columns", each Text.EndsWith([Attribute],"Date")),
    DateTblIndex = Table.AddIndexColumn(DateTbl, "Index", 0, 1, Int64.Type),
    SalesTbl = Table.SelectRows(#"Unpivoted Other Columns", each Text.EndsWith([Attribute],"Sales")),
    SalesTblIndex = Table.AddIndexColumn(SalesTbl, "Index", 0, 1, Int64.Type),
    #"Merged Queries" = Table.NestedJoin(DateTblIndex, {"Index"}, SalesTblIndex, {"Index"}, "SalesTblIndex", JoinKind.LeftOuter),
    #"Expanded SalesTblIndex" = Table.ExpandTableColumn(#"Merged Queries", "SalesTblIndex", {"Date"}, {"Sales"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded SalesTblIndex",{"Attribute", "Index"})
in
    #"Removed Columns"

 

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