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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Anonymous
Not applicable

DAX formula to look at previous day

I cannot seem to work out the DAX for what should be a simple calculation. Can anyone suggest how I would create a DAX measure to do the below?

 

Given this table definition: AccountDaily(AccountID, Date, Balance, Score, Purchases)

 

I need a DAX measure that returns this functionality for a measure called EACC:

When yesterday's Score is between 0.01 and 2.99

   then EACC = the smaller of yesterday's Balance and today's Purchases

else

  EACC = 0

 

It is easy in SQL. Here is my SQL query to return the correct value for an example AccountID and Month:

 

 

DECLARE @AccountId INT = 12345
DECLARE @Date DATE = '2020-02-01'

SELECT EACC = SUM(
CASE
WHEN Y.Score NOT BETWEEN 0.01 AND 2.99 THEN 0
WHEN Y.Balance < T.Purchases THEN Y.Balance 
ELSE T.Purchases
END
)
FROM AccountDay T --Today
LEFT JOIN AccountDay Y --Yesterday
ON T.AccountID = Y.AccountID
AND Y.Date = DATEADD(DAY, -1, T.Date)
WHERE T.AccountID = @AccountId
AND DATEDIFF(MONTH, T.Date, @date) = 0

 

 

 

Here is a set of sample data:

AccountIdDateScoreBalancePurchases
1234531/01/20200.0473.650
1234501/02/20200.0473.65180
1234502/02/20200.931486.380
1234503/02/20200.931486.380
1234504/02/20200.931486.380
1234505/02/20200.931486.380
1234506/02/20200.931486.380
1234507/02/20200.931486.380
1234508/02/20200.931486.380
1234509/02/20200.931486.380
1234510/02/20200.931486.380
1234511/02/20200.931486.380
1234512/02/20200.931486.380
1234513/02/20200.931486.380
1234514/02/20200.931486.380
1234515/02/20200.931486.380
1234516/02/20200.931486.380
1234517/02/20200.931486.381000
1234518/02/20200.3486.380
1234519/02/20200.3486.380
1234520/02/20200.3486.380
1234521/02/20200.3486.380
1234522/02/20200.3486.380
1234523/02/20200.3486.380
1234524/02/20200.3486.380
1234525/02/20200.3486.380
1234526/02/20200.3486.380
1234527/02/20200.3486.380
1234528/02/20200.3486.380
1234529/02/20200.3486.38600
5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

Sample data would help. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

 

Yesterday would be Yesterday = (TODAY() - 1) * 1.



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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Sorry, when I refer to "yesterday" I mean "the day prior to the current day" i.e. the row in AccountDaily where Date = DATEADD(DAY, -1, Date)

MeasureEACC = VAR _yesScore = LOOKUPVALUE(AccountDaily[Score], AccountDaily[AccountId], MIN(AccountDaily[AccountId]), AccountDaily[Date], MIN(AccountDaily[Date]) -1) 
              VAR _yesBalance = LOOKUPVALUE(AccountDaily[Balance], AccountDaily[AccountId], MIN(AccountDaily[AccountId]), AccountDaily[Date], MIN(AccountDaily[Date]) - 1)
RETURN
   if (_yesScore > 0.01 && _yesScore < 2.99, 
       if (_yesBalance < MIN(AccountDaily[Purchases]), _yesBalance, MIN(AccountDaily[Purchases])),
        0)

I think i've got the logic.  Please test with edge cases and other Account IDs

Anonymous
Not applicable

 

 

MeasureEACC =
SUMX(
    'AccountDaily',
    VAR _yesScore =
        LOOKUPVALUE(
            'AccountDaily'[Score],
            'AccountDaily'[AccountID], MIN('AccountDaily'[AccountID]),
            'AccountDaily'[Date], MIN('AccountDaily'[Date]) -1
        )
    VAR _yesBalance =
        LOOKUPVALUE(
            'AccountDaily'[Balance],
            'AccountDaily'[AccountID], MIN('AccountDaily'[AccountID]),
            'AccountDaily'[Date], MIN('AccountDaily'[Date]) -1
        )
    RETURN
        IF(
            _yesSccore >= 0.01 && _yesSccore <= 2.99,
            MIN(_yesBalance, 'AccountDaily'[Purchases]),
         0)
)

 

 

I'm not sure what is going wtong here!

If you stick with the original measure I wrote, you can probably create a new measure

NewEACC = SUMX(AccountDaily, [MeasureEACC])

which might work at month level

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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