Forum

Power Query to unst...
 
Notifications
Clear all

Power Query to unstack data when it doesn't recur regularly

9 Posts
3 Users
0 Reactions
336 Views
(@cpendletondow-com)
Posts: 7
Active Member
Topic starter
 

I am using Excel in Office365 on a PC. I have attached a sample workbook with dummy data that shows the issue I have.

I get my data by copying and pasting it from a system into a worksheet. The data is in ten regular columns named Item, Date, Type, Category, TK (timekeeper name), Rate, Units (hours), Desc, Adj, Amt, and an additional Column1. In addition, sometimes there are full blank rows interspersed within the data (I know how to fix that).

My problem is that the Date column has stacked data I want to unstack. The problem is that the stacked data can vary: although it always has the actual date (on the same row as the Item number) and then rows containing Project, Activity, and Description information, an Item sometimes has another row that starts with Warning (e.g., if a timekeeper has billed more than 12 hours). So, sometimes an Item has four rows under Date and sometimes it has five rows (if an Item has a Warning).

I would like to use Power Query to clean this up: leaving the actual Date by itself in the Date column, moving the Project, Activity, Description and (when it occurs) Warning data from rows under Date into their own columns, all lined up so that for each Item all invoice information is on a single row. 

 
Posted : 09/02/2022 12:27 pm
(@mynda)
Posts: 4761
Member Admin
 

Hi Cullen,

Welcome to our forum! Obviously this requires a different approach which has quite a few steps, but most importantly, the warnings need to be extracted to their own query, which you then merge back to the original data. I've attached your file with the solution. Hopefully you can follow the 'applied steps' to understand how to approach it.

Hope that helps.

Mynda

 
Posted : 09/02/2022 9:48 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Cullen,

Seems that Mynda and I were both working on this and didn't know the other was doing so!  At least you get 2 different solutions to your problem.

My query involves:

- Adding an Index column

- Splitting the Date column to separate data from its description

- Pivoting that new data column

- Sorting and filling the other columns

- Adding a Custom Column that counts the number of rows for each Item.  5 rows means there's a warning so that warning message goes into the Custom Column otherwise it's null.

- Filtering out rows in the Type column that are null, leaving you with your final data table

regards

Phil

 
Posted : 09/02/2022 10:26 pm
(@cpendletondow-com)
Posts: 7
Active Member
Topic starter
 

Hi Phil and Mynda, 

Thank you both very much for the solutions. I have gone through Phil's so far, but it's taking me some time to appreciate what each step does and why (which I think is necessary to really get all the benefit of his explanation and hopefully be able to apply it to other problems I may face in future).

Looks like the key step was "Added Custom3" (this took me a while to figure out since I was looking for a column by that name . . .), which counts the number of times each number appears in Item and either fills in null (if #<5) or the value from the Warning column - if/then/else logic that's perfect for my situation where I either have four or five rows in the original data, never more or less. The only problem with this solution is that there is no data in the Project, Activity, or Description columns, only Warning. missing-data-in-columns.JPG

Now I will go look at Mynda's solution. Thanks again to both of you for your help!

Cullen

 
Posted : 10/02/2022 9:51 am
(@cpendletondow-com)
Posts: 7
Active Member
Topic starter
 

I have opened Mynda's file, but don't see a solution in it (there is no query) - it looks to be just the original data. Maybe I should look up this idea of extracting rows to their own query and then merging?

 
Posted : 10/02/2022 10:13 am
(@mynda)
Posts: 4761
Member Admin
 

Hi Cullen,

Go to the Data tab > Queries and Connections. In the Queries and Connections pane that opens you'll see two 'connection only' queries. It's the Details query that contains the final result.

Mynda

 
Posted : 10/02/2022 5:56 pm
(@cpendletondow-com)
Posts: 7
Active Member
Topic starter
 

Sorry for not finding that sooner, Mynda - noobie error!

 
Posted : 11/02/2022 9:49 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Cullen,

Yes the #"Added Custom3" step does as you describe.

Not sure how the Project, Activity and Description columns ended up blank in my first file.  I've fixed that in this file.

Regards

Phil

 
Posted : 18/02/2022 1:34 am
(@cpendletondow-com)
Posts: 7
Active Member
Topic starter
 

Sorry to jump back to this, but I have found an issue with Mynda's solution: although the original data only had Warnings for three of the items (2, 7, & 9), and Mynda's Warnings table only shows 3 rows (consistent with that), the Merged Queries (combining Details and Warning from Mynda's solution) have Warnings appearing in five rows (1, 2, 6, 7, & 9).

I have attached a couple of pictures. Merged-Queries-return-5-items-with-Warnings.JPGOriginal-data-has-only-3-items-with-Warnings.JPG

 
Posted : 26/02/2022 4:06 pm
Share: