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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
David_S
New Member

Iteration inside a Measure with ADDCOLUMNS & CALCULATE

Hi BI Users,

 

 Interesting question which may be lacking some conceptual understanding 🙄

 

I need to measure the Net Revenue Retention, we define it as the amount retained by a customer over a period of time withouth including upsales, so if a client purchased 100 on the previous period and purchased 150 in the current period we only consider the 100 as a retained amount. 

So my aproach to the problem is... add a column to a table with key = the DISTINCT CompanyID which will sum the amount purchsed in the last period and another column with the amount purchased in the current period to then use the logic above. Below is the code for the DISTINCT. (source code for some variables like "prevmindate" is not relevant)

 

Var mytable = 
    ADDCOLUMNS(
        DISTINCT(fac_cs_churn_renewables[account_id]),
        "Prev Period",
        CALCULATE(SUM(fac_cs_churn_renewables[amount_converted]),
            ALLEXCEPT(  fac_cs_churn_renewables,
                        fac_cs_churn_renewables[close_date],
                        fac_cs_churn_renewables[account_id],
                        fac_cs_churn_renewables[account_segment]
            ),
            fac_cs_churn_renewables[close_date] > prevmindate && fac_cs_churn_renewables[close_date] <= mindate
        ),
        "Curr Period",
        CALCULATE(SUM(fac_cs_churn_renewables[amount_converted]),  
            ALLEXCEPT(  fac_cs_churn_renewables,
                        fac_cs_churn_renewables[close_date],
                        fac_cs_churn_renewables[account_id],
                        fac_cs_churn_renewables[account_segment]
            ),
            fac_cs_churn_renewables[close_date] > mindate && fac_cs_churn_renewables[close_date] <= maxdate
        )
    )
Var newcol = 
ADDCOLUMNS(
    mytable,"final_amount",
    IF([Prev Period] >= [Curr Period] , [Curr Period] , [Prev Period])
)


RETURN
newcol

 

 

So if I run this code to create a table then it works, however, when I try to change the returning value to sum the "final amount"... example below

 

RETURN
SUM(test[Prev Period])

 

and plug it into a measure, instead of a new table, the code doesn't work properly.

 

Any help is highly apriciated, thanks in advance!!!

1 ACCEPTED SOLUTION

Yourpattern suggestion is the rigth aproach, however, I was having conflicting issues with the ALLEXEPT when including it in the SUMMARIZE.  The solution was to create a CALCULATEDTABLE with the ALLEXCEPT and use that table in the SUMMARIZE

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

Share some data (in a format the can be pasted in an MS Excel file) and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish, thanks for your responce...  this set of data should work:

account_idmyyearamount_converted
a12021$9,867.
a12021$10,506.
a22021$24,064.
a22022$17,928.
b12021$2,901.
b12022$31,824.
b12022$1,500.
b22021$441.
b22021$17,360.
b22022$18,677.

 

The result in Power BI should be a single value of the total sum of the revenue retained in the "Result Column" = $38,630 shown below

Unique AccountsAmount in last periodAmount Current PeriodResult 
#=unique()sum if in 2021sum if in 2022#=IF(last period>=curr period,curr period,last period)
a12037300
a2240641792817928
b12901333242901
b2178011867717801

The calculation should first agregate the amounts by company for the previous period and the current period (periods are always current period = trailing 12 months previous period 13-24 months ago, so I just removed all filters and used varibles to assign a new filter context, see below)

 

 

VAR mindate = 
EOMONTH(
MAX(fac_cs_churn_renewables[close_date]),-12)

VAR maxdate = 
EOMONTH(
MAX(fac_cs_churn_renewables[close_date]),0)

VAR prevmindate = 
EOMONTH(
MAX(fac_cs_churn_renewables[close_date]),-24)

 

 

and then I performed the calculation shown in the original question posted.. Thanks

 

ps: actually, if you tried this code to produce a table it should work, but it's not working on the measure

Hi,

I believe Greg has already answered your question.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Super User
Super User

@David_S It's hard to figure out what you need. It could be Measure Aggregation, a For Loop or a While Loop but not sure.

 

If it is a measure aggregation problem. See my blog article about that here: https://community.powerbi.com/t5/Community-Blog/Design-Pattern-Groups-and-Super-Groups/ba-p/138149

The pattern is:
MinScoreMeasure = MINX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
MaxScoreMeasure = MAXX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
AvgScoreMeasure = AVERAGEX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
etc.

 

For Loop

 

While Loop



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Yourpattern suggestion is the rigth aproach, however, I was having conflicting issues with the ALLEXEPT when including it in the SUMMARIZE.  The solution was to create a CALCULATEDTABLE with the ALLEXCEPT and use that table in the SUMMARIZE

Hi Greg,

 

Thanks for your responce, I actually tried that before but I am getting some weird results which I have tried to debug... but you know what the community thinks about SUMMARIZE, so I tried to go another way unsuccessful.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.