Forum Discussion
Syntax for Looping through a List in an M function
- 6 years ago
Hi pelowski ,
My understanding was you just wanted to loop through each item in a list but not create tables out of it. The screenshot would have helped 😊. Anyway, the custom function below creates a table of name of day based on a list of cultures. I didn't use List.Accumulate as this could be slow if used on a large dataset.
let fnAddCultures = ( date as date, Cultures as list ) as table=> let list = List.Transform ( Cultures, each Date.DayOfWeekName ( date, _ ) ), table = Table.FromList(list, null), transpose = Table.Transpose(table), newname = List.Zip({Table.ColumnNames(transpose), Cultures}) in Table.RenameColumns(transpose, newname) in fnAddCulturesAnd here's a sample table generated from a list of dates and the custom function
let Dates = Table.FromList(List.Dates(#date(2010, 1, 1 ), 365.25 * 25, #duration(1,0,0,0)), Splitter.SplitByNothing(), {"Dates"}, null, ExtraValues.Error), #"Invoked Custom Function" = Table.AddColumn(Dates, "Day Name in Culture", each fnAddCultures([Dates], Cultures[Column1])), #"Expanded Day Name in Culture" = Table.ExpandTableColumn(#"Invoked Custom Function", "Day Name in Culture", {"en-US", "de-DE", "fr-FR", "pl-PL", "ru-RU", "es-ES", "zh-HK"}, {"en-US", "de-DE", "fr-FR", "pl-PL", "ru-RU", "es-ES", "zh-HK"}) in #"Expanded Day Name in Culture"
Yes, thank you! I would appreciate understanding how the code danextian could or would fit into an overall solution, (which I still don't understand) but that's just a nice-to-have; now I see how a List.Accumulate function can be used in combination with Table functions to achieve the looping I was trying to accomplish. Thanks again for your help!
Hello
So i would appreciate to mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
All the best
Jimmy