• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Adding rows based on columnar data|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Adding rows based on columnar data|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryAdding rows based on columnar data
sp_PrintTopic sp_TopicIcon
Adding rows based on columnar data
Avatar
james paisley

New Member
Members
Level 0
Forum Posts: 1
Member Since:
July 9, 2019
sp_UserOfflineSmall Offline
1
July 9, 2019 - 4:00 pm
sp_Permalink sp_Print

Hi,

 

I am dealing with a table that is challenging to do any look ups. I have explored the Unpivot option in Power Query, but this isn't providing the desired outcome (or I'm doing it wrong).

Below is a sample of my source data:

CustomerIdName kd_product01idName KD_Qty01 KD_Price01 kd_product02idName KD_Qty02 KD_Price02 kd_product03idName KD_Qty03 KD_Price03
A XYZ 5 1       XXX 1 1
B XYZ 5 1 Y2K 3 1   0  
C Y2K 5 1   0     0  

Below is desired output:

  Product Quantity Price
A XYZ 5 1
A XXX 1 1
B XYZ 5 1
B XXX 3 1
C XYZ 5 1

 

So, essentially, we want to unpivot each product, quantity and price and keep them together in a row, replicating all other columns except the ones we are unpivoting.

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
July 10, 2019 - 2:18 pm
sp_Permalink sp_Print

Hi James,

It is possible, but there are some inconsistencies in your data. If product name is null, then qty and price should be null in all sections. If in your data you might have zero's instead of nulls, you will need to add a filter after unpivot, to filter out 0 values. See the file attached and the query below.

let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"CustomerIdName"}, "Attribute", "Value"),
#"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each if Text.Contains([Attribute],"Name") then "Name" else if Text.Contains([Attribute],"Qty") then "Qty" else if Text.Contains([Attribute],"Price") then "Price" else null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute"}),
#"Added Custom1" = Table.AddColumn(#"Removed Columns", "Custom.1", each if Text.Contains([Custom],"Name") then [CustomerIdName] & "|" & [Value] else null),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"Custom.1"}),
#"Removed Columns1" = Table.RemoveColumns(#"Filled Down",{"CustomerIdName"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Custom.1", "Value", "Custom"}),
#"Pivoted Column" = Table.Pivot(#"Reordered Columns", List.Distinct(#"Reordered Columns"[Custom]), "Custom", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"Custom.1", "CustomerName"}}),
#"Extracted Text Before Delimiter" = Table.TransformColumns(#"Renamed Columns", {{"CustomerName", each Text.BeforeDelimiter(_, "|"), type text}})
in
#"Extracted Text Before Delimiter"

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Jessica Stewart, Mark Carlson, Calvin Richardson, Debi Morgan
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Debi Morgan
yashal minahil
Oluwadamilola Ogun
Yannik H
dectator mang
Francis Drouillard
Orlando Inocente
Jovitha Clemence
Maloxat Axmatovna
Ricardo Freitas
Forum Stats:
Groups: 3
Forums: 24
Topics: 6201
Posts: 27185

 

Member Stats:
Guest Posters: 49
Members: 31861
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.