cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
donatoath
New Member

How to do a double For/While?

I have a table with the following information:

IndexIterationPoints
13819
23916
34013
44115
54216
6

43

16

 

I need a double while/for to create the average of the last 3 iterations to a new table: (Example) 

IndexIterationAverage
140 = (19+16+13) / 3
241= (16+13+15) / 3
342= (13+15+16) / 3
443= (15+16+16) / 3

 

I think recursion is not possible in Dax Code but I've tried different things to see a workaround.

 

Please help

1 ACCEPTED SOLUTION
v-cazheng-msft
Community Support
Community Support

Hi @donatoath,

 

If you would like to achieve the desired result with a DAX formula, you may try this Measure.

Avg =
VAR avg_ =
    CALCULATE (
        AVERAGE ( 'Table'[Points] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Iteration] >= MAX ( 'Table'[Iteration] )
                && 'Table'[Iteration]
                    < ( MAX ( 'Table'[Iteration] ) + 3 )
        )
    )
VAR maxIndex =
    CALCULATE ( MAX ( 'Table'[Index] ), ALL ( 'Table' ) )
RETURN
    IF ( MAX ( 'Table'[Index] ) > maxIndex - 2, BLANK (), avg_ )

 

Then, the result looks like this.

vcazhengmsft_0-1654583801515.png

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please let me know. Thanks a lot!

 

Best Regards,

Community Support Team _ Caiyun

View solution in original post

2 REPLIES 2
v-cazheng-msft
Community Support
Community Support

Hi @donatoath,

 

If you would like to achieve the desired result with a DAX formula, you may try this Measure.

Avg =
VAR avg_ =
    CALCULATE (
        AVERAGE ( 'Table'[Points] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Iteration] >= MAX ( 'Table'[Iteration] )
                && 'Table'[Iteration]
                    < ( MAX ( 'Table'[Iteration] ) + 3 )
        )
    )
VAR maxIndex =
    CALCULATE ( MAX ( 'Table'[Index] ), ALL ( 'Table' ) )
RETURN
    IF ( MAX ( 'Table'[Index] ) > maxIndex - 2, BLANK (), avg_ )

 

Then, the result looks like this.

vcazhengmsft_0-1654583801515.png

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please let me know. Thanks a lot!

 

Best Regards,

Community Support Team _ Caiyun

amitchandak
Super User
Super User

@donatoath ,

A new column

Var _sum = sumx(filter(Table, [Index]>= earlier([Index]) && [Index]<= earlier([Index]) +2) , [Points] )

Var _cnt = countx(filter(Table, [Index]>= earlier([Index]) && [Index]<= earlier([Index]) +2) , [Points] )

return

divide(_sum,_cnt)

 

If needed you can check for _cnt > 2



Power BI Features || Datamarts: https://youtu.be/8tskWsJTEpg || Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Time Intelligence Decoded : https://youtu.be/aU2aKbnHuWs&t=145s
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
Dashboard of My Blogs !! Connect on Linkedin !! Subscribe to my youtube Channel !!
Want To Learn Power BI | Beginners !! Advance Concepts !! Power BI For Tableau User !!

Helpful resources

Announcements
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors