— CAN YOU SOLVE THIS - EXCEL CHALLENGE 196 —
(Solutions in any language are also welcome for Excel Challenges)
Repeat a character as per its position in the string and insert spaces between them.
Ex - Mango
M-aa-nnn-gggg-ooooo
Post answers in Comment.
(Your formula need not be a single formula. You can write multiple formulas to arrive at a solution. Also your formula need not be different from others as long as you have worked out your formula independently)
Download Practice File - https://lnkd.in/dMsEVhs9
#excel, #excelchallenge, #powerquerychallenge, #daxchallenge, #m, #dax, #powerbi, #powerquery, #powerpivot, #sheets, #googlesheets, #data, #analytics, #businessintelligence, #finance, #office365, #python, #r
Excel BI’s LinkedIn post
LinkedIn Post by
Henriette Hamer
=TEXTJOIN("-";TRUE;REPT(MID(A2;SEQUENCE(1;LEN(A2);1;1);1);SEQUENCE(1;LEN(A2);1;1)))
I didn’t get the expected answer in B6, but I feel that that was a typo in the exercise?
Formula is just for the row, couldn’t get it to work for the whole range in column A.
=MAP(A2:A6,LAMBDA(a,LET(b,LEN(a),TEXTJOIN("-",1,(REPT(MID(a,SEQUENCE(b),1),SEQUENCE(b)))))))
where sky
in the example returns 3 y’s of course.
My #powerquery solution. Works, but feels overly verbose given the straightforwardness of this challenge…
let
Source = Table.DuplicateColumn( Excel.CurrentWorkbook(){[Name="Table1"]}[Content], "String", "Letter"),
SplitToRows = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Letter", Splitter.SplitTextByRepeatedLengths(1), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Letter"),
Group = [Table.Group](http://table.group/)(SplitToRows, {"String"}, {{"All", each _, type table [String=text, Letter=nullable text]}}),
AddPositionIndex = Table.RemoveColumns( Table.AddColumn(Group, "Position", each Table.AddIndexColumn([All], "Position", 1, 1)), "All"),
AddRepeatText = Table.RemoveColumns( Table.AddColumn(AddPositionIndex, "Repeat", each Table.AddColumn( [Position], "Repeat", each Text.Repeat([Letter], [Position]))), "Position"),
SelCols = Table.RemoveColumns( Table.AddColumn(AddRepeatText, "Repeats", each Table.SelectColumns( [Repeat], "Repeat")), "Repeat"),
CombineText = Table.RemoveColumns( Table.AddColumn(SelCols, "Answer", each Text.Combine([Repeats][Repeat], "-")), "Repeats")
in
CombineText
Repeating characters w/ #powerquery. #bitanbit #powebi
let
Source = Excel.CurrentWorkbook(){[Name="Input"]}[Content],
Solution = Table.TransformRows(Source, each [
l = Text.ToList([String]),
t = List.Accumulate(List.Positions(l), "", (s, c) => s & (if s = "" then s else "-") & Text.Repeat(l{c}, c + 1))][t])
in
Solution
TEXTJOIN("-",,REPT(MID(B3,ROW(INDIRECT("1:" & LEN(B3))),1),ROW(INDIRECT("1:" & LEN(B3)))))
Press #CSE ===> Ctrl+Shift+Enter
LinkedIn post by:
Tyler N.
let
Src = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Res = Table.AddColumn(Src,"R",each Text.Combine(Table.AddColumn(Table.AddIndexColumn(Table.FromList(Text.ToList([String]),null,{"a"}),"b",1,1),"x",each Text.Repeat([a],[b]))[x],"-"))
in
Res
Here is a solution that does not use the LAMBDA function (it assumes the text will not be longer than 26 character - if it could be, just change the Z’s to a column designation whose column number is larger than the longest text string)…
=DROP(TEXTSPLIT(ARRAYTOTEXT(REPT(MID(A2:A6,COLUMN(A:Z),1),COLUMN(A:Z)),),,", , ",1),-1)
Although mine is similar to many here, I will still post as more of a thanks to this community for all the things that it is teaching me.
=MAP(A2:A6,LAMBDA(c,LET(a,SEQUENCE(LEN(c)),b,REPT(MID(c,a,1),a),TEXTJOIN("-",,b))))