Power Query Transformation Help

VegaOne16

New Member
Joined
Sep 6, 2009
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am able to load the following information into the data module in power query:

datetimetimezonePlantCodeDescriptionQuantityUoMP/C/U/F
1/15/202212:00:00 AMPSTZ9991A1,206.63MMP
1/15/202212:00:00 AMPSTZ9992B-51.96MMP
1/15/202212:00:00 AMPSTZ9993C289.85MMP
1/15/202212:00:00 AMPSTZ9994D6,115.49MMP
1/15/202212:00:00 AMPSTZ9995E-165.91MMP
1/15/202212:00:00 AMPSTZ9996F5,988.26MMP
1/15/202212:00:00 AMPSTZ9997G1,307.79MMP
1/15/202212:00:00 AMPSTZ9998H410.18MMP
1/15/202212:00:00 AMPSTZ9999I0.33MMP
1/15/202212:00:00 AMPSTZ99910J6,896.93MMP
1/15/202212:00:00 AMPSTZ99911K1,555.67MMP
1/15/202212:00:00 AMPSTZ99912L2,406.54MMP
1/15/202212:00:00 AMPSTZ99913M1,867.28MMP
1/15/202212:00:00 AMPSTZ99914N4,001.44MMP
1/15/202212:00:00 AMPSTZ99915O8,248.26MMP
1/15/202212:00:00 AMPSTZ99916P40,151.23MMC
1/15/202212:00:00 AMPSTZ99917Q1,555.67MMF
1/15/202212:00:00 AMPSTZ99918R-MMP

I'm wanting to automatically transform the data as follows:

1648508331607.png


Could someone help me with the code to automatically make these changes in my query via the editor, along with a step-by-step in PQEditor on how to add the code in.

These are the steps that I am manually performing:
  • If [Quantity] is "-" then delete the row.
  • If [P/C/U/F] is "P" AND [Quantity] is negative, then change "P" to "C", and then change quantity from negative to positive.

Thank you for your assistance.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Click anywhere in your source table and convert it to a formal Excel table with Ctrl-t and indicate that the table has headers. While still somewhere on the table, launch Power Query (one way is from the menu bar...select Data > From Table/Range). Once PQ launches, you should see your table loaded into PQ. Launch the Advanced Editor (menu bar in PQ... View > Advanced Editor) and paste the code below (you may need to adjust the Source line...basically keeping the initial Source line already there). These steps are somewhat kludgy (there are probably better ways to do this but I ran into some issues), but they perform the steps you described. Once editing is done in the Advanced Editor, confirm with Done and then, if you'd like, you can step through the Applied Steps on the right-side window to see what each step does. Confirm at the last step that the table looks as you wish and then load in back into Excel using File > Close & Load.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([Quantity] <> "-")),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Quantity", type number}, {"date", type date}, {"time", type time}, {"timezone", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Check", each if [#"P/C/U/F"] = "P" and [Quantity] < 0 then "change" else null),
    #"Added Custom" = Table.AddColumn(#"Added Conditional Column", "Abs.Qty", each Number.Abs([Quantity]), type number),
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom", each [#"P/C/U/F"], each if [Check] = "change" then "C" else [#"P/C/U/F"],Replacer.ReplaceText,{"P/C/U/F"}),
    #"Added Custom1" = Table.AddColumn(#"Replaced Value", "Custom", each if [Check]="change" then [Abs.Qty] else [Quantity]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Quantity", "Check", "Abs.Qty"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"date", "time", "timezone", "Plant", "Code", "Description", "Custom", "UoM", "P/C/U/F"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "Quantity"}})
in
    #"Renamed Columns"
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,552
Members
453,052
Latest member
ezzat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top