• 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
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Messy excel file with stacked data in non excel tables|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Messy excel file with stacked data in non excel tables|Power Query|Excel Forum|My Online Training Hub

vba course banner

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 QueryMessy excel file with stacked data …
sp_PrintTopic sp_TopicIcon
Messy excel file with stacked data in non excel tables
Avatar
John Langham-Service
Member
Members
Level 0
Forum Posts: 51
Member Since:
July 17, 2018
sp_UserOfflineSmall Offline
1
February 10, 2021 - 1:51 am
sp_Permalink sp_Print

I started a new job and if I'd known what a mess their data is in I'd have had second thoughts

 

Latest issue is sales data actual and estimated in weekly stacked tables product code and Sun-Sat in columns then repeated underneath for a full 52 weeks of individual sections of data.

As the attachment

I think maybe its possible to separate at the 2 blank cells between each block of headers but not sure how to start.

 

I'll fix the way they store data eventually but I have 4 years of data to clean as well

John

sp_AnswersTopicSeeAnswer See Answer
Avatar
Mynda Treacy
Admin
Level 10
Forum Posts: 4517
Member Since:
July 16, 2010
sp_UserOfflineSmall Offline
2
February 10, 2021 - 9:59 am
sp_Permalink sp_Print

Hi John,

I'm not sure there's an easy way to fix this data because there's a different number of rows in each group of weeks. i.e. there's no consistent pattern. My approach would be to create a separate table for each Sun-Sat range of rows, unpivot and then append.

Someone else might have a better idea.

Mynda

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
3
February 10, 2021 - 5:30 pm
sp_Permalink sp_Print sp_EditHistory

Hi John,

It is possible, but not easy.
To separate weeks, you can add an index column and a custom column to identify the beginning of another section, based on text "IPS CODE" from column 1.

If you fill down the Custom column, you will be able to group rows into different tables.

That was the easy part...

Each table must be now converted.

I used a double loop in the custom function that converts the tables:
Loops = List.Accumulate({8..21},Data1,(L1State,i)=>
                                    List.ReplaceRange(L1State,i-1,1,

                                {List.Accumulate({0..RowsCount},{},(L2State,j)=>
                                            if Text.Contains(if Data1{0}{j}=null then "" else Data1{0}{j},"CODE") then
                                           L2State & {Text.From(L1State{i-1}{j}) & "|" & Text.From(L1State{i-1}{j+2})}
                                           else L2State & {L1State{i-1}{j}})})),

The double loop is similar to what is usually done in vba:

For i=column8 to column21

     For j=0 to tableRowsCount-1

          if column 1 value contains "CODE" then replace date with Date & "|" & the value located 2 rows down (where we have EST or ACT)

     Next j

Next i

There can be other solutions, it's just an idea.

sp_AnswersTopicAnswer
Answers Post

The following users say thank you to Catalin Bombea for this useful post:

Mynda Treacy
Avatar
John Langham-Service
Member
Members
Level 0
Forum Posts: 51
Member Since:
July 17, 2018
sp_UserOfflineSmall Offline
4
February 11, 2021 - 8:16 pm
sp_Permalink sp_Print

Thanks Catalin, once again your awesome grasp of M comes to the rescue, next time I'm allowed into Ro I'll buy you a beer or two.

 

John

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
5
February 11, 2021 - 11:35 pm
sp_Permalink sp_Print sp_EditHistory

You're welcome.
Anyway, it's not about M knowledge. In fact, the double loop is an unnecessary complication, the function can be created without editing M code, using user interface tools.

It's just imagination I think, to let imagination fly, you have to think different than normal excel tools.

Here is a function built with interface, will do the same thing (Est and ACT are not independent columns, they are attributes in the same column. It can be pivoted to make 2 columns though):

(tbl)=>
let
Source = tbl,
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Column1] <> "")),
#"Promoted Headers" = Table.PromoteHeaders(#"Filtered Rows", [PromoteAllScalars=true]),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"IFS#(lf)CODE", "Column2", "Column3", "COMMENTS", "Q#(lf) BOX FORMAT", "Column6", "Column7"}, "Attribute", "Value"),
#"Added Index1" = Table.AddIndexColumn(#"Unpivoted Other Columns", "Index", 0, 1, Int64.Type),
#"Checked Is Even" = Table.TransformColumns(#"Added Index1",{{"Index", Number.IsEven, type logical}}),
#"Added Custom1" = Table.AddColumn(#"Checked Is Even", "Type", each if [Index]=true then "EST" else "ACT"),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Index"}),
#"Extracted Text Before Delimiter" = Table.TransformColumns(#"Removed Columns1", {{"Attribute", each Text.BeforeDelimiter(_, "_"), type text}}),
#"Changed Type" = Table.TransformColumnTypes(#"Extracted Text Before Delimiter",{{"Attribute", type date}})
in
#"Changed Type"
sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Kumud Patel, Bright Asamoah
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 218
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Gilbert Lemek
Ashleigh Farquharson
Jayz Luu
Fred Smith
Charles DeGraffenreaid
Cathi Giard
Sarah Young
Henry Delgado
Alita Nieuwoudt
KL KOH
Forum Stats:
Groups: 3
Forums: 24
Topics: 6360
Posts: 27812

 

Member Stats:
Guest Posters: 49
Members: 32340
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.