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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Calvin69
Helper III
Helper III

Generated Income based on a Variable

Hi everyone,😊

Thank you in advance for attempting to read through this and for any help that you could provide me with.

I am trying to build a measure or a column that would allow me to do the following:

Table:

DateTenantStart ReviewEndRent
01/01/2019Vacant01/10/2019 01/10/20210
01/01/2020Voda01/01/2020 01/10/202010000
01/01/2020Hitash15/11/202001/06/202101/10/202115000
01/01/2020Sports ltd01/01/202015/06/202131/12/20215000
01/07/2021Vacant01/10/2019 01/10/20210
01/07/2021Voda15/11/2020 01/10/202110000
01/07/2021Hitash15/11/2020 01/10/202117000
01/07/2021Sports ltd01/01/202001/11/202131/12/20217000

Target: Total of "4000" should appear based on the following logic

 On the 01/07/2021
 If new review date is Blank or Greater than previous review date and the Tenant Name is the same then the following should take place
HitashNew Rent Value (17000) - Old Rent Value (15000) = 2000
Sports ltdNew Rent Value (7000) - Old Rent Value (5000) = 2000
 Total Rent difference = 2000 + 2000 = 4000

 

Thanks

H

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@Calvin69 Made a few assumptions along the way but maybe this:

Measure = 
    VAR __Table = 'Table4'
    VAR __MaxDate = MAX('Table4'[Date])
    VAR __PreviousDate = MAXX(FILTER('Table4',[Date]<__MaxDate),[Date])
    VAR __MaxDateTable = FILTER(__Table,[Date]=__MaxDate)
    VAR __PreviousDateTable = FILTER(__Table,[Date]=__PreviousDate)
    VAR __FinalTable = 
        ADDCOLUMNS(
            ADDCOLUMNS(
                __MaxDateTable,
                "__Include",
                        VAR __Review = [Review]
                    RETURN
                        IF(ISBLANK([Review]) || COUNTROWS(FILTER(__PreviousDateTable,[Review]<__Review))>0,1,0)
            ),
            "__Diff",
                    VAR __Tenant = [Tenant]
                RETURN
                    IF([__Include]=1,[Rent] - MAXX(FILTER(__PreviousDateTable,[Tenant]=__Tenant),[Rent]),0)
        )
RETURN
    SUMX(FILTER(__FinalTable,[__Include]=1),[__Diff])

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

@Calvin69 Made a few assumptions along the way but maybe this:

Measure = 
    VAR __Table = 'Table4'
    VAR __MaxDate = MAX('Table4'[Date])
    VAR __PreviousDate = MAXX(FILTER('Table4',[Date]<__MaxDate),[Date])
    VAR __MaxDateTable = FILTER(__Table,[Date]=__MaxDate)
    VAR __PreviousDateTable = FILTER(__Table,[Date]=__PreviousDate)
    VAR __FinalTable = 
        ADDCOLUMNS(
            ADDCOLUMNS(
                __MaxDateTable,
                "__Include",
                        VAR __Review = [Review]
                    RETURN
                        IF(ISBLANK([Review]) || COUNTROWS(FILTER(__PreviousDateTable,[Review]<__Review))>0,1,0)
            ),
            "__Diff",
                    VAR __Tenant = [Tenant]
                RETURN
                    IF([__Include]=1,[Rent] - MAXX(FILTER(__PreviousDateTable,[Tenant]=__Tenant),[Rent]),0)
        )
RETURN
    SUMX(FILTER(__FinalTable,[__Include]=1),[__Diff])

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler ,

 

Spot on mate, wonderful and thank you so much.

 

Just wondering why do you add __ before each variable name?

 

H

@Calvin69 Best explained as old habit I suppose. I found it advantageous to flag things I created versus what is actually in the data model. Comes in particularly handly when using ADDCOLUMNS and more complex measures as it can become a bit confusing to figure out what is being referred to in what context. I use a double underscore because it fits the rhythm of my typing better. 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.