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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
han_rj
Helper IV
Helper IV

Advanced unpivot data query

Hi Team,

 

I have a scenario where I need to flatten/unpivot Orders and Unit Price columns as Years,Orders,Unit price columns. Please may I have help know how this can be achieved in power query

han_rj_1-1738906012614.png

 

 

 

1 ACCEPTED SOLUTION

let
    Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
    rows = List.Buffer(Table.ToList(Source, each _)),
    years = List.Buffer(List.Range(rows{1}, 3, 3)),
    trx = List.TransformMany(
        List.Skip(rows, 2), 
        (x) => List.Zip({years, List.Range(x, 3, 3), List.Range(x, 7, 3)}),
        (x, y) => List.FirstN(x, 3) & {x{6}} & {x{10}} & y
    ), 
    result = Table.FromList(trx, each _, {"Product", "Prod Cat", "Prod Code", "Sales", "Inv", "Year", "Order", "Unit Price"})
in
    result

View solution in original post

8 REPLIES 8
SundarRaj
Super User
Super User

Here's the solution to your query. It might be a long solution but do have a look.

SundarRaj_0-1738964417642.pngSundarRaj_1-1738964453734.png

 

Sundar Rajagopalan
Akash_Varuna
Super User
Super User

Hi @han_rj , Could you try these 
Unpivot Columns: Select year-based columns, right-click, and choose unpivot columns.
Combine Name & Year: Add a custom column with this formula:
Text.BeforeDelimiter([Attribute], " ") & "_" & Text.AfterDelimiter([Attribute], " ")
Rename & Cleanup: Rename columns as needed and remove unnecessary columns.
If this post helped please do give a kudos and accept this as a solution
Thanks In Advance

Hi Akash, Thanks For the Respose, Please may I have help on the updated request

Could You specify ?

I would like to get an output like this with Year , Sales and Unit sales like this

han_rj_0-1738906657382.png

 

let
    Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
    rows = List.Buffer(Table.ToList(Source, each _)),
    years = List.Buffer(List.Range(rows{1}, 3, 3)),
    trx = List.TransformMany(
        List.Skip(rows, 2), 
        (x) => List.Zip({years, List.Range(x, 3, 3), List.Range(x, 7, 3)}),
        (x, y) => List.FirstN(x, 3) & {x{6}} & {x{10}} & y
    ), 
    result = Table.FromList(trx, each _, {"Product", "Prod Cat", "Prod Code", "Sales", "Inv", "Year", "Order", "Unit Price"})
in
    result

Thank You @AlienSx , This was  very helpful

Hi @han_rj, if the bottom table is your expected result - it does not make sense for me.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.