A function needed to create a runtime table from an initial value

Error 1004

New Member
Joined
May 15, 2022
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I need a function but i coudnt do so much about it. I want to pass a string initial value to the function and then function will create 5 rows x 3 columns table as output with simple algoritm.
Assume i will send "A to function and output will be like this:

initVal="A"
for i = 1 to 5
col1=initVal & format(i, "000")
col2= i
col3=i+1
next

Col1 Col2 Col3
A001 1 2
A002 2 3
A003 3 4
A004 4 5
A005 5 6


thanks in advanced
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Power Query:
(t as text) => Table.FromColumns({List.Generate(()=>1, each _<6, each _+1, each t & Text.PadStart(Number.ToText(_),3,"0")), {1..5},{2..6}},{"Col1","Col2","Col3"})

or

Power Query:
(t as text) => Table.FromColumns({List.Accumulate({1..5}, {}, (s,c)=> s & {t & Text.PadStart(Number.ToText(c),3,"0")}), {1..5},{2..6}},{"Col1","Col2","Col3"})
 
Upvote 0
Thank you very much JGordon11. It's working as i expected.
But can i ask you 1 more favor? You have wrote 1 long code line. can u divide it small steps? so it would be more clear for me and other beginers.

regards.
 
Upvote 0
Power Query:
(t as text, optional start as nullable number, optional end as nullable number) as table => 
let 
    a = if start = null then 1 else start,
    b = if end = null then 5 else end,
    col1 = List.Accumulate({a..b}, {}, (s,c)=> s & {t & Text.PadStart(Number.ToText(c),3,"0")}),
    col2 = {a..b},
    col3 = {a+1..b+1},
    ColList = {col1,col2,col3},
    ColNames = {"Col1","Col2","Col3"},
    Result = Table.FromColumns(ColList, ColNames)
in   
    Result
 
Upvote 0
Solution
Millions of thanks.
This language has a different logic of its own that I do not fully understand yet. but your solutions help me a lot.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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