Power Query Value from Named Range based on Another Column

jeff4smith

New Member
Joined
Jan 4, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have a Query that includes a column with list of last names, i.e. Smith, Jones, etc. In my worksheet there is a named range of Week_Smith, Week_Jones, etc and this named range is a single cell, which contains a date. I would like to add a new column to the Query to return the named range of each person. I was able to create a conditional column that has the correct named range using the code below, but I am not sure how to instead return the value of that named range.

#"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({"Week_", [Name]}), type text),


Named_RangeNameHighlights
Week_SmithSmithFirst highlight
Week_SmithSmithSecond highlight
Week_SmithSmithThird highlight
Week_JonesJonesFirst highlight
Week_JonesJonesSecond highlight
Week_JonesJonesThird highlight
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
When in Power Query go to New Query>New Source>Other Sources>Blank Query.

Open the new query in the Advanced Editor and add this code.
VBA Code:
let GetNamedRange=(NamedRange) =>
 
let
    name = Excel.CurrentWorkbook(){[Name=NamedRange]}[Content],
    value = name{0}[Column1]
in
    value

in GetNamedRange
Go back to the original query, go to Add Column>Invoke Custom Function.

In the Invoke Custom Function select the function you created above as the Function Query and select the appropriate column from the Named Range dropdown.
 
Upvote 0
Solution
Excellent. Thanks for the quick reply and simple solution! I appreciate it!
 
Upvote 0
No problem.:)

By the way, you could alter the code I posted so it works when you pass it the name rather than the value from the column you created.

That would save you having to create a new column.
VBA Code:
let GetNamedRange=(Person) =>
 
let
    name = Excel.CurrentWorkbook(){[Name=Text.Combine({"Week_", Person}) ]}[Content],
    value = name{0}[Column1]
in
    value

in GetNamedRange
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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