Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
Dicken
Post Prodigy
Post Prodigy

Power Query 'scoping' i think

 

Hello I have come across this in a few situations where grouping or working with nested let statements 
in these three example the first does not work, the other two , where i have a variable or a named function do, 
I wondered if anyone can give me more information or a link to a blog or  something on this subject, 
Just interested generally; 

 Table.Group( Source , {"Name"}, {{"Count", each 
 let 
 acc = List.Generate( ()=> [ x = 0 , y = [Unit] {0} ] , 
 each [x] < List.Count( [Unit]   ) , 
 each [ x = [x] + 1, y = [y] + [Unit]  {x} ] , 
 each [y] )
 in acc }} )  = [Unit] not found, 


= Table.Group( Source , {"Name"}, {{"Count", each 
 let unt = [Unit] , 
 acc = List.Generate( ()=> [ x = 0 , y = unt {0} ] , 
 each [x] < List.Count( unt  ) , 
 each [ x = [x] + 1, y = [y] + unt {x} ] , 
 each [y] )
 in acc }} 


= Table.Group( Source , {"Name"}, {{"Count", (z)=> 
 let 
 acc = List.Generate( ()=> [ x = 0 , y = z[Unit] {0} ] , 
 each [x] < List.Count( z[Unit]   ) , 
 each [ x = [x] + 1, y = [y] + z[Unit]  {x} ] , 
 each [y] ) 
in acc }} )



1 ACCEPTED SOLUTION
Shai_Karmani
Super User
Super User

When you use each inside List.Generate, _ refers to the state record, not the group table. That’s why [Unit] fails  it looks for Unit in the wrong scope. If you bind the column first (e.g., unt = t[Unit]) or pass the table explicitly ((t)=>t[Unit]), the reference resolves correctly.

Here's an interesting reference: The Each Keyword in Power Query – Excelguru

 

Shai Karmani | Data & Analytics

If it helped please mark as resolved & give a kudo so others can find it too.

Let’s connect on LinkedIn

View solution in original post

2 REPLIES 2
Shai_Karmani
Super User
Super User

When you use each inside List.Generate, _ refers to the state record, not the group table. That’s why [Unit] fails  it looks for Unit in the wrong scope. If you bind the column first (e.g., unt = t[Unit]) or pass the table explicitly ((t)=>t[Unit]), the reference resolves correctly.

Here's an interesting reference: The Each Keyword in Power Query – Excelguru

 

Shai Karmani | Data & Analytics

If it helped please mark as resolved & give a kudo so others can find it too.

Let’s connect on LinkedIn

Thanks for the link will have a read. 

RD

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.