Custom column question

Sniklfritz

New Member
Joined
Apr 3, 2017
Messages
17
Hello all,
I am stuck on something im not sure if its even possible...

I have 2 connection only queries in excel. The first is Cost Center and the 2nd is Flights.

In the Flights Query I am trying to reference the cost center in a new custom column and add the cost center number into the flight query. However i cant find how to actually reference the query?? E.g. CostCenter[Column D].

Merge is not an option for a few reasons...

Thx
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
While it might be possible to create a custom column to actually pull the data from the Cost Center table into the Flights table, the easy way is to merge the Cost table with (into) the Flights table. This can be done within the Flights query, or a new query can be created.
In the code below, the CostCtr table has a column of Cost Center numbers, and a column of their names. The Flights table has Flight Numbers and Cost Center numbers. This creates a new query from the Flights table merging the CostCtr table's Cost Center's rows that match the Fligth's Cost Center:
Power Query:
let
    Source = Table.NestedJoin(tblFlights, {"Cost Ctr"}, tblCostCtr, {"Cost Center"}, "tblCostCtr", JoinKind.LeftOuter),
    ExpandedtblCostCtr = Table.ExpandTableColumn(Source, "tblCostCtr", {"Name"}, {"tblCostCtr.Name"})
in
    ExpandedtblCostCtr
The Merge drop down on the Home tab of the PQ Editor allows you to create the merge within the Flights table or create a new Query/Table as shown above.
It wasn't needed here, but it's helpful if you update your profile to indicate which version of Excel and OS you're using. Also, using XL2BB to post sample data is preferable. This is what that looks like:

Book1
ABCDEFGHI
1tblCostCtrtblFlightsResult
2Cost CenterNameFlightCost CtrFlightCost CtrtblCostCtr.Name
3101Airplane30301033030109Doubtful Advertisement
4102Jade Authority30311083034109Doubtful Advertisement
5103Sandy Brown Earth30321043035109Doubtful Advertisement
6104Hobby30331103031101Airplane
7105Nondescript Science30341043032101Airplane
8106Late Wheel30351033039101Airplane
9107Science30361073036103Sandy Brown Earth
10108Fight30371013033105Nondescript Science
11109Doubtful Advertisement30381033037105Nondescript Science
12110Memory30391073038108Fight
Sheet1
 
Upvote 0
Sorry I missed your note, but would like to know why merging isn't an option!
That said, here's the query needed based on the tables in my original post:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblFlights"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Flight", Int64.Type}, {"Cost Ctr", Int64.Type}}),
    AddedCustom = Table.AddColumn(ChangedType, "Cost Ctr Name", each tblCostCtr[Name]{List.PositionOf(tblCostCtr[Cost Center],[Cost Ctr])}, type text)
in
    AddedCustom
The AddedCustom line basically looks up the Index (from 0) of the FIRST Match in tblCostCtr and returns that name. Worth noting that in either solution, if the Cost Center number isn't a UNIQUE list of values, a correct result is not insured. In the Merge you'd get all the matching rows and create the number of matching rows for each item looked up. In this solution you'd only get the first matching record which would be totally dependent on the sort of the Cost Center table. If there are multiple values for the Cost Center Number, neither solution is really correct. There should be a dimension table with unique values for a field to use for a lookup in either case.
 
Upvote 0
Match characters? That sounds like a fuzzy merge.
Can't you share your solution, @Sniklfritz ? Always nice to have it available for learning and knowledge sharing purposes.
 
Upvote 0

Forum statistics

Threads
1,223,950
Messages
6,175,582
Members
452,653
Latest member
craigje92

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