Power Query - trying to remove a dynamic column that's created/updated when I access external data source

Viewed 626

I have a table that's generated when I pull data from an accounting software - the example columns are months/years in the format as follows (It pulls all the way to current day, and the last month will be partial month data):

Nov_2020

Dec_2020

Jan_2021

Feb_1_10_2021 (Current month, column to remove)

... So on and so forth.

My goal I have been trying to figure out is how to use the power query editor to remove the last column (The partial month) - I tried messing around with the text length to no avail (The goal being to remove anything with text length >8, so the full months data would show but the last month shouldn't). I can't just remove based on a text filter, because if someone were to pull the data 1 year from now it would have to account for 2021/2022.

Is this possible to do in PQ? Sorry, I'm new to it so if I need to elaborate more I can.. Thanks!

3 Answers

Although both Alexis Olson's and Justyna MK's answers are valid, there is another approach. Since it appears that you're getting data for each month in a separate column, what you will surely want to do is unpivot your data, that is transform those columns into rows. It's the only sensible way to get a good material for analysis, therefore, I would suggest to unpivot the columns first, then simply filter out rows containing the last month.

To make it dynamic, I would use unpivot other columns option - you select columns and it will transform remaining columns into row in such a way that two columns will be created - one that will contain column names in rows and the other one will contain values.

enter image description here

To illustrate what I mean by unpivoting, when you have data like this:

enter image description here

You're automatically transforming that into this:

enter image description here

You can try to do it through Power Query's Advanced Editor. Assign the name of the last column to LastColumn variable and then use it in the last step (Removed Columns).

let
    Source = Excel.Workbook(File.Contents(Excel file path), null, true),
    tblPQ_Table = Source{[Item="tblPQ",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(tblPQ_Table,{{"Nov_2020", Int64.Type}, {"Dec_2020", Int64.Type}, {"Jan_2021", Int64.Type}, {"Feb_1_10_2021", Int64.Type}}),
    LastColumn = List.Last(Table.ColumnNames(#"Changed Type")),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{LastColumn})
in
    #"Removed Columns"

enter image description here

Related