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
anubb688
Frequent Visitor

CALCULATE, FILTER and ALL

Hi, below DAX does not display correct result. What this code should be doing is displaying "RPMD" for previous 10 periods. Each period is a week. I suspect the problem lands possibly caused by misusing of CALCULATE, FILTER and ALL. Please advise.

 

RPMD = DIVIDE([Revenue Calculated], [Est Exp], 0)

 

RPMD Last 10 =
VAR _selecteddate =
    MIN ( 'Calendar'[Date] )
RETURN
    CALCULATE (
        [RPMD],
        FILTER (
            ALL ( 'Calendar'[Date] ),
            IF (
                'Calendar'[Date] = _selecteddate - 7,
                'Calendar'[Date],
                IF (
                    'Calendar'[Date] = _selecteddate - 14,
                    'Calendar'[Date],
                    IF (
                        'Calendar'[Date] = _selecteddate - 21,
                        'Calendar'[Date],
                        IF (
                            'Calendar'[Date] = _selecteddate - 28,
                            'Calendar'[Date],
                            IF (
                                'Calendar'[Date] = _selecteddate - 35,
                                'Calendar'[Date],
                                IF (
                                    'Calendar'[Date] = _selecteddate - 42,
                                    'Calendar'[Date],
                                    IF (
                                        'Calendar'[Date] = _selecteddate - 49,
                                        'Calendar'[Date],
                                        IF (
                                            'Calendar'[Date] = _selecteddate - 56,
                                            'Calendar'[Date],
                                            IF (
                                                'Calendar'[Date] = _selecteddate - 63,
                                                'Calendar'[Date],
                                                IF ( 'Calendar'[Date] = _selecteddate, 'Calendar'[Date], BLANK () )
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    )

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@anubb688 Probably, Why not just get the current date and if you want the past 10 periods (each a week) then something like:

RPMD Last 10 = 
  VAR __SelectedDate = MIN('Calendar'[Date])
  VAR __10Prior = __SelectedDate - 70
RETURN
  CALCULATE([RPMD],FILTER('Calendar','Calendar'[Date]>=__10Prior && 'Calendar'[Date]<=__SelectedDate))

@ 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

4 REPLIES 4
Greg_Deckler
Super User
Super User

@anubb688 Probably, Why not just get the current date and if you want the past 10 periods (each a week) then something like:

RPMD Last 10 = 
  VAR __SelectedDate = MIN('Calendar'[Date])
  VAR __10Prior = __SelectedDate - 70
RETURN
  CALCULATE([RPMD],FILTER('Calendar','Calendar'[Date]>=__10Prior && 'Calendar'[Date]<=__SelectedDate))

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

anubb688_0-1627586735890.png

It somehow shows a dot instead of line chart which i have selected

Maybe:

RPMD Last 10 = 
  VAR __SelectedDate = MIN('Calendar'[Date])
  VAR __10Prior = __SelectedDate - 70
RETURN
  CALCULATE([RPMD],FILTER(ALL('Calendar'),'Calendar'[Date]>=__10Prior && 'Calendar'[Date]<=__SelectedDate))

It's really hard to know with such incomplete information.


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

Your code worked when I tweaked my beneath formulas. Thanks. I'll mark yours as solution since I found my direction.

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