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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
J_o_n_a_s
Helper I
Helper I

Pivot / Unpivot?

Hi. I got this table below and i want to convert it to the table underneath it. I'm assuming to use Pivot/unpivot but can't figure out how. Thanks for your input!

 

PMPro_KeyInv_NoAdj_Due _Date1Adj_Sum1Adj_Due _Date2Adj_Sum2
JamesProject110101/07/20251882501/08/20252000
JohnProject210201/06/20252008001/07/20255020
       
PMPro_KeyInv_NoDue DateSum  
JamesProject110101/07/202518825  
JamesProject110101/08/20252000  
JohnProject210201/06/202520080  
JohnProject210201/07/20255020  
1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    tm = List.TransformMany(
        Table.ToList(Source, (x) => x), 
        (x) => List.Split(List.Skip(x, 3), 2), 
        (x, y) => List.FirstN(x, 3) & y
    ), 
    result = Table.FromList(tm, (x) => x)
in
    result

View solution in original post

10 REPLIES 10
v-pbandela-msft
Community Support
Community Support

Hi @J_o_n_a_s,

Thank you for reaching out in Microsoft Community Forum.

Thank you @AntrikshSharma , @SundarRaj , @AlienSx , @uzuntasgokberk    for the helpful response.

As suggested by AntrikshSharma, SundarRaj, AlienSx, uzuntasgokberk.,  I hope this information was helpful. Please let me know if you have any further questions or you'd like to discuss this further. If this answers your question, please "Accept as Solution" and give it a 'Kudos' so others can find it easily.

Please continue using Microsoft community forum.

Regards,
Pavan.

AntrikshSharma
Super User
Super User

@J_o_n_a_s 

let
    Source = Table, 
    A = Table.CombineColumns (
        Source, 
        { "Adj_Due _Date1", "Adj_Due _Date2", "Adj_Sum1", "Adj_Sum2" }, 
        ( x ) as table =>
            Table.FromColumns (
                { 
                    List.Select ( x, ( y ) => y is date ), 
                    List.Select ( x, ( y ) => y is number ) 
                }, 
                type table [ Due Date = date, Sum = number ]
            ), 
        "x"
    ), 
    B = Table.ExpandTableColumn ( A, "x", { "Due Date", "Sum" }, { "Due Date", "Sum" } )
in
    B

 

let
    Source = Table,
    Unpivot = Table.UnpivotOtherColumns ( Source, { "PM", "Pro_Key", "Inv_No" }, "A", "V" ),
    Group = Table.Group (
        Unpivot,
        { "PM", "Pro_Key", "Inv_No" },
        {
            "T",
            ( x ) =>
                Table.FromColumns (
                    {
                        Table.SelectRows ( x, ( y ) => y[V] is date )[V],
                        Table.SelectRows ( x, ( y ) => y[V] is number )[V]
                    },
                    type table [ Due Date = date, Sum = number ]
                )
        }
    ),
    Expand = Table.ExpandTableColumn ( Group, "T", { "Due Date", "Sum" }, { "Due Date", "Sum" } )
in
    Expand

 

J_o_n_a_s
Helper I
Helper I

Thanks all. Will test them on monday 🙂

SundarRaj
Solution Supplier
Solution Supplier

Hi @J_o_n_a_s , here's another approach you can test out. I'll leave the code used below. Thanks!

SundarRaj_0-1747984773694.png

Here's the code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"PM", type text}, {"Pro_Key", type text}, {"Inv_No", Int64.Type}, {"Adj_Due _Date1", type datetime}, {"Adj_Sum1", Int64.Type}, {"Adj_Due _Date2", type datetime}, {"Adj_Sum2", Int64.Type}}),
Cols = Table.ColumnNames ( #"Changed Type" ),
ReqCols = List.Select ( Cols , each not (try Number.From ( Text.End ( _ , 1 ) ) )[HasError] ),
Count = List.Count ( List.Distinct ( List.Transform ( ReqCols , each Text.End ( _ , 1 ) ) ) ),
List = Table.ToColumns ( Table.SelectColumns ( #"Changed Type" , ReqCols ) ),
Table = List.Transform ( List.Split ( List , Count ) , each Table.FromColumns ( _ , { "Due Date" , "Sum" } ) ),
FixedCols = Table.RemoveColumns ( #"Changed Type" , ReqCols ),
Index = Table.TransformColumns ( Table.AddIndexColumn ( FixedCols , "Index" ,0 , 1 ) , { "Index" , each Table{_} } ),
Expand = Table.ExpandTableColumn(Index, "Index", {"Due Date", "Sum"}, {"Due Date", "Sum"})
in
Expand

Sig,

Sundar Rajagopalan
uzuntasgokberk
Super User
Super User

Hello @J_o_n_a_s ,

You can implement this way. İf it is suitable for you.
I created a dummy data and shared m code you can implement easily.
Table1:
'

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMTS1W0lEKKMrPSk0uMQQyDQ1ApIGhvoG5vpGBkSlIyMICTIMELWCCRgYGBkqxOkAz8jPyEEYYgY0wgqo2Q1JtYYBurqmBEdCIWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [PM = _t, Pro_Key = _t, Inv_No = _t, #"Adj_Due _Date1" = _t, Adj_Sum1 = _t, #"Adj_Due _Date2" = _t, Adj_Sum2 = _t]),
#"Changed column type" = Table.TransformColumnTypes(Source, {{"PM", type text}, {"Pro_Key", type text}, {"Inv_No", Int64.Type}, {"Adj_Due _Date1", type date}, {"Adj_Sum1", Int64.Type}, {"Adj_Due _Date2", type date}, {"Adj_Sum2", Int64.Type}}),
#"Removed other columns" = Table.SelectColumns(#"Changed column type", {"PM", "Pro_Key", "Inv_No", "Adj_Due _Date1", "Adj_Sum1"}),
#"Renamed columns" = Table.RenameColumns(#"Removed other columns", {{"Adj_Due _Date1", "Adj_Due _Date"}, {"Adj_Sum1", "Adj_Sum"}})
in
#"Renamed columns"
'

Table2:
'
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMTS1W0lEKKMrPSk0uMQQyDQ1ApIGhvoG5vpGBkSlIyMICTIMELWCCRgYGBkqxOkAz8jPyEEYYgY0wgqo2Q1JtYYBurqmBEdCIWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [PM = _t, Pro_Key = _t, Inv_No = _t, #"Adj_Due _Date1" = _t, Adj_Sum1 = _t, #"Adj_Due _Date2" = _t, Adj_Sum2 = _t]),
#"Changed column type" = Table.TransformColumnTypes(Source, {{"PM", type text}, {"Pro_Key", type text}, {"Inv_No", Int64.Type}, {"Adj_Due _Date1", type date}, {"Adj_Sum1", Int64.Type}, {"Adj_Due _Date2", type date}, {"Adj_Sum2", Int64.Type}}),
#"Removed other columns" = Table.SelectColumns(#"Changed column type", {"PM", "Pro_Key", "Inv_No", "Adj_Due _Date2", "Adj_Sum2"}),
#"Renamed columns" = Table.RenameColumns(#"Removed other columns", {{"Adj_Due _Date2", "Adj_Due _Date"}, {"Adj_Sum2", "Adj_Sum"}})
in
#"Renamed columns"
'
You can disable enable load.

uzuntasgokberk_0-1747942221992.png

Then, you can click append queries as new.

uzuntasgokberk_1-1747942250674.png

Append table:
'

let
Source = Table.Combine({Table, #"Table (2)"})
in
Source
'
uzuntasgokberk_2-1747942273070.png

 

Kind Regards,

Gökberk Uzuntaş

AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    tm = List.TransformMany(
        Table.ToList(Source, (x) => x), 
        (x) => List.Split(List.Skip(x, 3), 2), 
        (x, y) => List.FirstN(x, 3) & y
    ), 
    result = Table.FromList(tm, (x) => x)
in
    result

Thanks. This worked. And so did SundarRaj's reply. Got a quesition for @AlienSx.

 

I would like to add more columns. So instead of only the first 3 columns being repeated on each row (like in my initial example) I would like 9 columns to be repeated on each row. So i will add 6 more columns before column Adj_Due _Date1. What would be the solution for that?

 

And what if then i added another column and made it 10?

 

Thanks once again

Calculate position of Adj_Due_Date1 in the list of column names and replace 3 in my code with that number. 

Hi @AlienSx 

 

Sorry i am slow in this. In my original table, Adj_Due_Date1, is in position 4 not 3.

 

I replaced both 3s with 10. Then also with 11 and it didnt work. Got an error. THIS TABLE IS EMPTY.

List items in M are positioned starting with zero - maybe that's why. I have everything working correctly 

let
    Source = #table(
        {"0".."9"} & {"Adj_Due_Date1", "Adj_Sum1", "Adj_Due_Date2", "Adj_Sum2"},
        {{0..13}, {100..113}}
    ), 
    tm = ((position) => List.TransformMany(
        Table.ToList(Source, (x) => x), 
        (x) => List.Split(List.Skip(x, position), 2), 
        (x, y) => List.FirstN(x, position) & y
    ))(List.PositionOf(Table.ColumnNames(Source), "Adj_Due_Date1")), 
    result = Table.FromList(tm, (x) => x)
in
    result

Take care of correct column names parameter in Table.FromList yourself please.

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors