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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
abloor
Helper IV
Helper IV

Adding an IF statement into Calculate and VAR measure

Hi there,

 

I'm trying to write a measure that gives me the revenue figures for 7 days prior to the latest 'Snap Date'.  Below is what I have done, which works to give me what I need.

 

The trouble is, sometimes if on a public holiday, we won't have any revenue figures for 7 days prior, so if blank,  I want Power BI to show me the revenue figures for 6 days prior.  And if blank, 5 days prior.

 

How would I alter my DAX to incorporate this with an IF statement?

 

Thank you

 

Revenue Same Day Last Week =

VAR LatestSnapDate = LOOKUPVALUE ('Table'[SnapDate], 'Table'[SnapDate], MAX('Table'[SnapDate]) )


RETURN
CALCULATE(SUM('Table'[Revenue]),FILTER(ALL('Table'[SnapDate]), 'Table'[SnapDate] = MAX('Table'[SnapDate])-7))
7 REPLIES 7
Anonymous
Not applicable

Hi @abloor ,

Since there must be two holidays in the last 7 days, I think your requirement could be simply understood as

1.Calculate the sum of the income of the working days in the last 7 days

2.Calculate the total income of the previous 5 days

forSlicer =
CALENDAR ( "2020/8/1", "2020/8/31" )
dateDiff =
DATEDIFF (
    SELECTEDVALUE ( DateRevenue[SnapDate] ),
    SELECTEDVALUE ( forSlicer[Date] ),
    DAY
)
previous5Days =
CALCULATE (
    SUM ( DateRevenue[Revenue] ),
    FILTER ( ALL ( DateRevenue ), [dateDiff] >= 0 && [dateDiff] <= 4 )
)
workingDays =
CALCULATE (
    SUM ( DateRevenue[Revenue] ),
    FILTER (
        ALL ( DateRevenue ),
        [dateDiff] >= 0
            && [dateDiff] <= 6
            && NOT ( ISBLANK ( DateRevenue[Revenue] ) )
    )
)

My visualization looks like this:

9.1.1.1.png

 

Is the result what you want? If you have any questions, please upload some data samples and expected output.

Please do mask sensitive data before uploading.

 

Best Regards,

Eyelyn Qin

Thank you @amitchandak . It keeps giving me the latest data, not the data from 7 days before.  I'll keep playing around with it and see how I go.

 

Thank you also @Anonymous  I will give this a try ASAP in the next week.

Anonymous
Not applicable

Hi @abloor ,

You are welcome. If I asked your question,please mark my reply as solution.If not ,please upload more detailed samples and expected output. Thank you~

 

Best regards,

Eyelyn Qin

Thank you for checking in @Anonymous  and for the solution you put forward.  It's not quite what I'm after, so I will find the time to re-word my post and put up an example as requested.

amitchandak
Super User
Super User

@abloor , Try like

 

measure =
VAR LatestSnapDate = minx(filter( 'Table'[SnapDate] >= MAX('Table'[SnapDate])-7 && not(isblank('Table'[SnapDate]))),'Table'[SnapDate])

RETURN
CALCULATE(SUM('Table'[Revenue]),FILTER(ALL('Table'[SnapDate]), 'Table'[SnapDate] = MAX('Table'[SnapDate])))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandakthanks for your quick response.

I gave this a go, and it's returning the revenue from the most recent snap date. Not the snap date 7 days prior.

 

Any ideas how we could change it so it shows 7, then if blank, 6 days, and if blank, 5 days before the snap date?

 

Many thanks!

@abloor , The idea to use get the min snap date >= snap date -7

minx(filter( 'Table'[SnapDate] >= MAX('Table'[SnapDate])-7 && not(isblank('Table'[SnapDate]))),'Table'[SnapDate])

 

try like

measure =

minx(filter( 'Table',  'Table'[SnapDate] >= today() -7 && not(isblank('Table'[SnapDate]))),'Table'[SnapDate])

RETURN
CALCULATE(SUM('Table'[Revenue]),FILTER(ALL('Table'[SnapDate]), 'Table'[SnapDate] = MAX('Table'[SnapDate])))

or

 

measure =
var _maxsnap = maxx( allselected('Table'),'Table'[SnapDate])
VAR LatestSnapDate = minx(filter('Table', 'Table'[SnapDate] >= _maxsnap-7 && not(isblank('Table'[SnapDate]))),'Table'[SnapDate])

RETURN
CALCULATE(SUM('Table'[Revenue]),FILTER(ALL('Table'[SnapDate]), 'Table'[SnapDate] = MAX('Table'[SnapDate])))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors