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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Nilselmano
Frequent Visitor

How to avoid hardcoding names when using List.Generate with multiple dependent columns

Hi,
I will try to explain my issue as best as i can.

I want to perform a calculation that is basically a running total but subtracting an amount in each iteration that is dependent on the amount in the other value columns present. Basically it means that all value columns in the original table needs to be included in the List.Generate loop in order to get to the right result. 

I want to make this dynamic as the amount of value columns is not constant, so i want to build the logic but without hardcoding names along the way.

I managed to get the intended result with hardcoded names and the query is:

 

 

 

let
  Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQxUNJRMjQAk0YQtlKsTrSSMYgNw1AxioSMKDLMiFrOoL8QZR6nSoiZEKE1FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [no1 = _t, no2 = _t, no3 = _t, Factor = _t]),
  Tbl_Start = Table.TransformColumnTypes(Source, {{"no1", Int64.Type}, {"no2", Int64.Type}, {"no3", Int64.Type}, {"Factor", Int64.Type}}, "da"),
  BuffTbl = Table.Buffer(Tbl_Start),
  IndexCol = Table.AddIndexColumn(Tbl_Start, "Index", 0, 1, Int64.Type),
  RunningTotal = List.Generate (
  () => [ RT_no1 = Tbl_Start{0}[no1], 
          RT_no2 = Tbl_Start{0}[no2], 
          RT_no3 = Tbl_Start{0}[no3], 
          factor = Tbl_Start{0}[Factor], 
          factor_no1 = (RT_no1/(RT_no2+RT_no3+RT_no1))*factor, 
          factor_no2 = (RT_no2/(RT_no2+RT_no3+RT_no1))*factor, 
          factor_no3 = (RT_no3/(RT_no2+RT_no3+RT_no1))*factor, 
          Index = 0],                                 // Initial values

   each [Index] < List.Count( Tbl_Start[Factor] ),    // Do while

   each [                                             
    RT_no1 = List.Sum( { [RT_no1], Tbl_Start{[Index]+1}[no1] - [factor_no1] } ),
    RT_no2 = List.Sum( { [RT_no2], Tbl_Start{[Index]+1}[no2] - [factor_no2] } ),
    RT_no3 = List.Sum( { [RT_no3], Tbl_Start{[Index]+1}[no3] - [factor_no3] } ), 

    factor_no1 = (RT_no1/(RT_no2+RT_no3+RT_no1))*Tbl_Start{Index}[Factor],
    factor_no2 = (RT_no2/(RT_no2+RT_no3+RT_no1))*Tbl_Start{Index}[Factor],
    factor_no3 = (RT_no3/(RT_no2+RT_no3+RT_no1))*Tbl_Start{Index}[Factor],

    Index = [Index] + 1],                             // Calculations

   each [[RT_no1], [RT_no2], [RT_no3], [factor_no1], [factor_no2], [factor_no3]] ),
  // Records to return
  Output = Table.FromColumns(
     Table.ToColumns( Tbl_Start )   & { RunningTotal } ,    // Combine Tables
     Table.ColumnNames( Tbl_Start ) & { "RT" } ),
  Expanded = Table.ExpandRecordColumn(Output, "RT", Record.FieldNames(Output[RT]{0}), Record.FieldNames(Output[RT]{0}))
in
  Expanded

 

 

 

Expected result:

no1no2no3FactorRT_no1RT_no2RT_no3factor_no1factor_no2factor_no3
14010012010140,00100,00120,003,892,783,33
300010166,1197,22116,674,372,563,07
00010161,7494,66113,604,372,563,07
00010157,3792,11110,534,372,563,07
00010153,0089,55107,464,372,563,07
020010148,63106,99104,394,132,972,90
00010144,50104,02101,494,132,972,90
00010140,37101,0498,594,132,972,90
002010136,2498,07115,693,892,803,31
00010132,3595,27112,383,892,803,31

 

Any hints to how I can achieve will be greatly appreciated.

1 REPLY 1
lbendlin
Super User
Super User

Whenever you have volatile number of columns you need to unpivot. Here is an example of the RT computation

 

let
  Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQxUNJRMjQAk0YQtlKsTrSSMYgNw1AxioSMKDLMiFrOoL8QZR6nSoiZEKE1FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [no1 = _t, no2 = _t, no3 = _t, Factor = _t]),
  IndexCol = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(IndexCol, {"Factor", "Index"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Factor", type number}, {"Value", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "RT", (k)=> List.Sum(Table.SelectRows(#"Changed Type",each [Index]<=k[Index] and [Attribute]=k[Attribute])[Value]), type number)
in
    #"Added Custom"

I'll leave the factor computation up to you.

Frankly neither is necessary to do in Power Query - you can do these much easier in DAX.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.