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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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