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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
aizhan01
Frequent Visitor

Finding Difference to Previous value in selected date (filter contex)

Here is my table 

 

CategoryFile DateApplicants
Car23/08/20231452
Car30/08/2023739
Car6/09/20231096
Car13/09/20231503
Bike23/08/20235246
Bike30/08/20233839
Bike6/09/20235126
Bike13/09/20236398

 

am trying to calculate Difference to previous date, applying ALLSELECTED() in my DAX to use filter context but ignore row context for previous date. However it doesn't work. 

 

DAX: 
[Previous Date] = VAR _thisdate =  MAX(table[File Date])
              RETURN calculate(  MAX(table[File Date]), ALLSELECTED(table[File Date]) ,table[File Date] < _thisdate ) 

 

First, when i use the measure in a new calculation below  :
     CALCULATE( SUM(table[Applicants]),  table[File Date]  = [Previous Date])

it returns error : a function PLACEHOLDER has been used in True/False expression that is used as a table filter expression.

 

So then i have to redefine everything in the measure :

[Prev.dateApplicant] = VAr _thisdate = MAX(table[File Date])
          Var _daybefore = calculate(  MAX(table[File Date]), ALLSELECTED(table[File Date]) ,table[File Date] < _thisdate ) 

          RETURN CALCULATE( SUM(table[Applicants]), table[File Date] = _daybefore)

 

[Difference to Prev.Date] = sum(table[Applicants])-[Prev.dateApplicant]

 

But the result is not correct when i apply filter of File Date to select only  3 dates: 23/08/2023, 6/09/2023, 13/09/2023

Result:

 ApplicantsPrevious DatePrev.dateApplicantDifference to Prev.Date
Category23/08/20236/09/202313/09/202323/08/20236/09/202313/09/202323/08/20236/09/202313/09/202323/08/20236/09/202313/09/2023
Car14521096150316/09/202330/08/20236/09/202311077391096345357407
Bike52465126639816/09/202330/08/20236/09/202343183839512692812871272

 

I want this: 

 ApplicantsPrevious DatePrev.dateApplicantDifference to Prev.Date
Category23/08/20236/09/202313/09/202323/08/20236/09/202313/09/202323/08/20236/09/202313/09/202323/08/20236/09/202313/09/2023
Car145210961503 23/08/20236/09/2023 14521096 -356407
Bike524651266398 23/08/20236/09/2023 52465126 -1201272

 

What is wrong with my DAX, can someone give me the correct DAX to get what i need please?

 

thanks

 

1 ACCEPTED SOLUTION
v-jianpeng-msft
Community Support
Community Support

Thank you Sahir_Maharaj and Sahir_Maharaj 

Hi, @aizhan01 

According to your description, I use the dataset you provided:

vjianpengmsft_0-1730352676792.png

First, create a calculation table using the following dax expression:

Table 2 = VALUES('Table'[File Date])

In the second step, use to create the relationship as shown in the figure:

vjianpengmsft_1-1730352764995.png

In the third step, create the following two measure:

PreviousDay = 
VAR _seleted_value = VALUES('Table 2'[File Date])
RETURN 
CALCULATE(MAX('Table'[File Date]),FILTER(ALL('Table'[File Date]),'Table'[File Date] < MAX('Table'[File Date])&&'Table'[File Date] IN _seleted_value))
Difference = 
VAR _preday = [PreviousDay]
VAR _current_day_applicant = CALCULATE(SUM('Table'[Applicants]),FILTER(ALL('Table'[File Date]),'Table'[File Date] = MAX('Table'[File Date])))
VAR _pre_applicant = CALCULATE(SUM('Table'[Applicants]),FILTER(ALL('Table'[File Date]), 'Table'[File Date] = _preday))
RETURN _current_day_applicant - _pre_applicant 

Use Table 2 to create a slicer. The Table creates a matrix as follows:

vjianpengmsft_2-1730352891174.png

vjianpengmsft_3-1730352902741.png

Select the corresponding date to get the output you expect:

vjianpengmsft_4-1730352941548.png

 

 

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

View solution in original post

4 REPLIES 4
v-jianpeng-msft
Community Support
Community Support

Thank you Sahir_Maharaj and Sahir_Maharaj 

Hi, @aizhan01 

According to your description, I use the dataset you provided:

vjianpengmsft_0-1730352676792.png

First, create a calculation table using the following dax expression:

Table 2 = VALUES('Table'[File Date])

In the second step, use to create the relationship as shown in the figure:

vjianpengmsft_1-1730352764995.png

In the third step, create the following two measure:

PreviousDay = 
VAR _seleted_value = VALUES('Table 2'[File Date])
RETURN 
CALCULATE(MAX('Table'[File Date]),FILTER(ALL('Table'[File Date]),'Table'[File Date] < MAX('Table'[File Date])&&'Table'[File Date] IN _seleted_value))
Difference = 
VAR _preday = [PreviousDay]
VAR _current_day_applicant = CALCULATE(SUM('Table'[Applicants]),FILTER(ALL('Table'[File Date]),'Table'[File Date] = MAX('Table'[File Date])))
VAR _pre_applicant = CALCULATE(SUM('Table'[Applicants]),FILTER(ALL('Table'[File Date]), 'Table'[File Date] = _preday))
RETURN _current_day_applicant - _pre_applicant 

Use Table 2 to create a slicer. The Table creates a matrix as follows:

vjianpengmsft_2-1730352891174.png

vjianpengmsft_3-1730352902741.png

Select the corresponding date to get the output you expect:

vjianpengmsft_4-1730352941548.png

 

 

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

Sahir_Maharaj
Super User
Super User

Hello @aizhan01,

 

Can you please try this approach:

 

1. Calculate the Previous Date

Previous Date = 
VAR _thisDate = MAX(table[File Date])
RETURN
    CALCULATE(
        MAX(table[File Date]),
        FILTER(
            ALLSELECTED(table),
            table[File Date] < _thisDate && table[Category] = EARLIER(table[Category])
        )
    )

2. Calculate Applicants for the Previous Date

Prev.dateApplicant = 
VAR _prevDate = [Previous Date]
RETURN
    CALCULATE(
        SUM(table[Applicants]),
        table[File Date] = _prevDate,
        VALUES(table[Category])
    )

3. Difference to Previous Date

Difference to Prev.Date = 
SUM(table[Applicants]) - [Prev.dateApplicant]

Hope this helps.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning
rajendraongole1
Super User
Super User

Hi @aizhan01 - can you modify the previous date calculation created as below, using REMOVEFILTERS function. 

 

Previous Date =
VAR _currentDate = MAX(ApplicantsTable[File Date])
RETURN CALCULATE(
    MAX(ApplicantsTable[File Date]),
    REMOVEFILTERS(ApplicantsTable[File Date]),   -- Ignore all filters on the date
    ApplicantsTable[File Date] < _currentDate    -- Get the date before the current date
)

 

create the previous Date Applicants Calculation

Prev.dateApplicant =
VAR _previousDate = [Previous Date]
RETURN CALCULATE(
    SUM(ApplicantsTable[Applicants]),
    REMOVEFILTERS(ApplicantsTable[File Date]),  -- Ignore filters again to get correct applicants for the previous date
    ApplicantsTable[File Date] = _previousDate
)

 

Now calculates the difference between the current applicants and the applicants on the previous date.

Difference to Prev.Date = SUM(ApplicantsTable[Applicants]) - [Prev.dateApplicant]

 

rajendraongole1_0-1729680065535.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





hi @rajendraongole1   

Thank you for your reply. I tried the above calculation and it's not giving want i want as it is removing the filter context.

For reference my calculation gives correct answer for 13Sept2023 but wrong for 6 Sep2023 because my filter excludes 30Aug2023 data, and I want previous date to only consider selected dates, i.e 23Aug2023 here. 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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