Power Query Field Access Error

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I'm trying to use Power Query to fill in a list of department numbers and am getting an unfamiliar error.

I have a table with a listing of numbers like this,

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Dept Category[/TD]
[TD]Department Range Start[/TD]
[TD]Department Range End[/TD]
[/TR]
[TR]
[TD]03[/TD]
[TD]300[/TD]
[TD]399[/TD]
[/TR]
[TR]
[TD]04[/TD]
[TD]400[/TD]
[TD]499[/TD]
[/TR]
[TR]
[TD]05[/TD]
[TD]500[/TD]
[TD]599[/TD]
[/TR]
[TR]
[TD]06[/TD]
[TD]600[/TD]
[TD]699[/TD]
[/TR]
</tbody>[/TABLE]

And the ultimate goal is to create a list of values with everything between Start Number and End Number, so for the above, a list of values 300-699.

I'm trying to insert the following calculated column

Code:
= Table.AddColumn(#"Changed Type1", "Department Range", each List.Generate( () => [Department Range Start], each _ <= [Department Range End], each _ +1))

I get the following error on the row with 300-399 (same error for the others but the Value changes)

Expression.Error: We cannot apply field access to the type Number.
Details:
Value=300
Key=Department Range End

What am I missing here... or is there a better way?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The cause of the error is that field [Department Range End] is not known in the context of List.Generate, as it is not included in the first argument.
Using List.Generate here is like "shooting a mosquito with a canon".

A better solution:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Dept Category", Int64.Type}, {"Department Range Start", Int64.Type}, {"Department Range End ", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Department Range", each {[Department Range Start]..[#"Department Range End "]})
in
    #"Added Custom"
 
Upvote 0
Just to illustrate, this code is a working version of using List.Generate in this case.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Dept Category", Int64.Type}, {"Department Range Start", Int64.Type}, {"Department Range End", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", 
                                      "Department Range", 
                                      each List.Generate( () => [Counter = _[Department Range Start], End = _[Department Range End]], 
                                                         each [Counter] <= [End], 
                                                         each [Counter = [Counter] + 1, End = [End]], 
                                                         each [Counter]))
in
    #"Added Custom"
 
Upvote 0
And another List.Generate solution, closer to the original code (I'm still discovering the secrets of List.Generate....)

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Dept Category", Int64.Type}, {"Department Range Start", Int64.Type}, {"Department Range End", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Department Range", (CR) => List.Generate( () => CR[Department Range Start], each _ <= CR[Department Range End], each _ +1))
in
    #"Added Custom"
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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