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
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
Super User
Super User

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.


@ 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...
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
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.

Top Solution Authors