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

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

Reply
Bokchoy
Helper II
Helper II

Custom invoked function Looping with M query

Hi All,

im trying to create a new column in my table where it only index rows that have [Istradingday] = 1.
Im trying to archieve this by writing a invoked custom fucntion loop shown below:

Bokchoy_0-1629785081522.png

 

The current outcome of the custom column looks like this:

Bokchoy_1-1629785140204.png

 

I've never used M lanuage before and i need help with the following issues:

1. when [istradingday] = 0, i want to show blank. (currently showing "X")
2. Value + 1 only when [istradingday] = 1. (currently will + 1 for every loop regardless]

 

side question: is there a way to write for/while loops in M lanuage simuliar to python
  

 

 

 

1 ACCEPTED SOLUTION

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3sNA3MjAyVNJRMgDi4NK8lMRKIAMkoKAUqxOtZGCEpAaEffPzgKQRmAdWYIymIKQ0tRhIGYNVgVWYoKkIT00BkiZgRWAFpuhGZJQWASlTsCqwCjM0FW5FmUDSDKwIrMAc3SuJJUDSHOKPWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Istradingday = _t, Day = _t, Index = _t, #"Istradingindex (Desired outcome)" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Istradingday", Int64.Type}, {"Index", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "IsTradeIndex", each if [Istradingday]=0 then "" else List.Sum(Table.FirstN(#"Changed Type",[Index])[Istradingday]))
in
    #"Added Custom"

View solution in original post

4 REPLIES 4
lbendlin
Super User
Super User

M has built in iteration functions ("each" ), allows recursive calls (as you tried) and it also supports loops (and much more) via List.Generate().  Conceptually it is very unlike Python, so you need to adjust a bit.

 

Please provide sample data in usable format (not as a picture - maybe insert into a table?) and show the expected outcome.

Hi, thanks for answering my question, please see the attached for example data for 1 week.

Current table

DateIstradingdayDayIndexIstradingindex (Desired outcome)
1/08/20210Sunday1 
02/08/20211Mon21
03/08/20211Tues32
04/08/20211Wed43
05/08/20211Thur54
06/08/20211Fri65
07/08/20210Sat7 
...............

 

Anonymous
Not applicable

Hi @Bokchoy,

For your requirement, you can use the current index value as a condition to filter table records and extract and summary lstradingday field values. (these parts can refer to lbendlin 's formuals)

In my opinion, I think Dax functions should be more suitable for these calculations, M query may spend more resources for these loop calculations. You can try to add buffer functions to reduce the additional resource spends.

Nested functions and Table.Buffer() - Exceed
Regards,

Xiaoxin Sheng

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3sNA3MjAyVNJRMgDi4NK8lMRKIAMkoKAUqxOtZGCEpAaEffPzgKQRmAdWYIymIKQ0tRhIGYNVgVWYoKkIT00BkiZgRWAFpuhGZJQWASlTsCqwCjM0FW5FmUDSDKwIrMAc3SuJJUDSHOKPWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Istradingday = _t, Day = _t, Index = _t, #"Istradingindex (Desired outcome)" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Istradingday", Int64.Type}, {"Index", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "IsTradeIndex", each if [Istradingday]=0 then "" else List.Sum(Table.FirstN(#"Changed Type",[Index])[Istradingday]))
in
    #"Added Custom"

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Kudoed Authors