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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Anonymous
Not applicable

Power Query M function - LOOP??

Hi,

 

I need to do some ETL with M function. I'm still learnig how to deal with it. Can anybody help me?

 

I Need to create two adittional columns [count] and [step] and fill these columns based on Rules bellow. How can I do this on power query?

 

thank you all

 

 

DatanumberRulesCountstep
Volume de Utilização1if 'First Record or previous [count] = 0 --> [Count]= [number] Step=010
Negócio1If previous count<>0; step = - [number]  ; [count] = previous [count] + step0-1
   00
Notebook básico2 20
xxxxxxxxxxx1 1-1
yyyyyyyyyyy1 0-1
Acrobat Professional por equipamento1 10
xxxxxxxxxxx1 0-1
Project Professional por equipamento1 10
xxxxxxxxxxx1 0-1
2 ACCEPTED SOLUTIONS
MarcelBeug
Community Champion
Community Champion

You can use List.Accumulate, like in the code below.

Remark: the step values are not really required to calculate the Count, so if you don't need it for something else, you can leave "step" out.

 

let
    Source = Table1,
    CountAndStep = List.Skip(List.Accumulate(List.Transform(Source[number], each if _ = null then 0 else _),
                                             {[Count = 0,step = 0]},
                                             (Result, Number) => 
                                                Result & {if List.Last(Result)[Count] = 0 
                                                          then [Count = Number,                           step = 0] 
                                                          else [Count = List.Last(Result)[Count] - Number,step = -1 * Number]})),
    AddedCountAndStepToSource = Table.FromColumns(Table.ToColumns(Source)&{CountAndStep}),
    Expanded = Table.ExpandRecordColumn(AddedCountAndStepToSource, "Column3", {"Count", "step"}),
    NewTableType = Value.ReplaceType(Expanded,Value.Type(Table.AddColumn(Table.AddColumn(Source,"Count",each 0, Int64.Type),"step",each 0, Int64.Type)))
in
    NewTableType
Specializing in Power Query Formula Language (M)

View solution in original post

The example file is empty...

 

Make sure that you first have your table in Power Query, next it s used in my query (in this case "Table1", adapt to tour table name).

The number must be in column "number" (all lower case).

Specializing in Power Query Formula Language (M)

View solution in original post

6 REPLIES 6
MarcelBeug
Community Champion
Community Champion

You can use List.Accumulate, like in the code below.

Remark: the step values are not really required to calculate the Count, so if you don't need it for something else, you can leave "step" out.

 

let
    Source = Table1,
    CountAndStep = List.Skip(List.Accumulate(List.Transform(Source[number], each if _ = null then 0 else _),
                                             {[Count = 0,step = 0]},
                                             (Result, Number) => 
                                                Result & {if List.Last(Result)[Count] = 0 
                                                          then [Count = Number,                           step = 0] 
                                                          else [Count = List.Last(Result)[Count] - Number,step = -1 * Number]})),
    AddedCountAndStepToSource = Table.FromColumns(Table.ToColumns(Source)&{CountAndStep}),
    Expanded = Table.ExpandRecordColumn(AddedCountAndStepToSource, "Column3", {"Count", "step"}),
    NewTableType = Value.ReplaceType(Expanded,Value.Type(Table.AddColumn(Table.AddColumn(Source,"Count",each 0, Int64.Type),"step",each 0, Int64.Type)))
in
    NewTableType
Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

HI @MarcelBeug,

 

Thanks for helping. I got an error when trying your suggestion. Can you help again?

 

I am attaching the file so you can have a better picture.

 

https://drive.google.com/open?id=0B6oYowKqdZzqWlpOSFFWSThQYW8

 

I believe the error is on step "CountandStep"

Hi @Anonymous,

 

Did the problem get resolved? If yes, could you accept the helpful reply as solution to close this thread?

 

If you still have any question on this issue, feel free to post here. Smiley Happy

 

Regards

The example file is empty...

 

Make sure that you first have your table in Power Query, next it s used in my query (in this case "Table1", adapt to tour table name).

The number must be in column "number" (all lower case).

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Sorry!

I wasn't able to work on this until yesterday. The source file was missing, my intetion was to send the code.

 

Anyway, back to work I found out that my isseu was regarding column type on the source file. I made some adjustments and it is working!!!

 

thank you @MarcelBeug!!

 

 

 

 

Hi @Anonymous,

 

Great to hear the problem got resolved! Could you accept the helpful reply as solution to close this thread? Smiley Happy

 

Regards

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.