Power Query help

Rodel Geronimo

New Member
Joined
Nov 18, 2009
Messages
23
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
hello all, kindly help me to add a custom column in my table "REV" and a column named "Document Number". This column can have multiple duplicates so I want to keep the original and append any duplicates by "-01" and so on.

Thank you so much for your help as usual. :)
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
The main mCode could go as follows:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="REV"]}[Content],
    GroupedByDocNo = Table.Group(Source, {"Document Number"}, {{"SingleDoc", each _, type table [index=nullable number, Document Number=nullable text]}}),
    AddedIndexesForEachDoc = Table.AddColumn(GroupedByDocNo, "SingleDocIndexed", each Table.AddIndexColumn([SingleDoc], "Revisions", 0, 1)),
    ExpandTablesInSingleDocIndexed = Table.ExpandTableColumn(AddedIndexesForEachDoc, "SingleDocIndexed", {"Revisions"}, {"Revisions"}),
    AddedConditionalColumn = Table.AddColumn(ExpandTablesInSingleDocIndexed, "Document Number with Rev", each if [Revisions] = 0 then [Document Number]  else [Document Number] & "-" & Number.ToText([Revisions],"00")),
    RemoveUnneded = Table.SelectColumns(AddedConditionalColumn,{"Document Number with Rev"})
in
    RemoveUnneded

The only less intuitive (semi-manual) part is Table.AddColumn where you manually add a function which would normally be clicked-out in the menu: Table.AddIndexColumn(
Normally the first argument here wouuld be a table name, but we write a column which contains tables obtained after grouping - [SingleDoc],

You may see similar work here

Probably you shal revise also which columns are removed - with the code above only number with revisions remains. Also - if column with doc numbers is a number column it shall be either changed to text or to obtained mixed type final column use changed line

Power Query:
   AddedConditionalColumn = Table.AddColumn(ExpandTablesInSingleDocIndexed, "Document Number with Rev", each if [Revisions] = 0 then [Document Number]  else Number.ToText([Document Number]) & "-" & Number.ToText([Revisions],"00")),
 
Upvote 0
Thank you so much, that worked though , added only an ascending order sort in the beginning.
 
Upvote 0
Wow, not the quickest answer :-)

but I'm glad it worked for you
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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