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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
hsalasde
Regular Visitor

Log and Delta Calculation

I'm pretty new to BI and have started using the VAR. I'm running a log count and I need to calculate the delta from the previous year for the same month. The results are to be displayed on a table with the rows for years and columns for months. I've managed to create a working count for log #s, but I'm having difficulty with the delta. (The delta is comprised from the difference between the current year's log count and the log count for the same month on the year prior).

 

Here is my DAX: 

 

Delta =
VAR LogIdCountCurrentMonth = CALCULATE(
    DISTINCTCOUNT('Surg Data'[Log #]),
    FILTER(
        ALL('Surg Data'[Date]),
        YEAR('Surg Data'[Date]) = YEAR(TODAY()) &&
        MONTH('Surg Data'[Date]) = MONTH(TODAY())
    )
)

VAR LogIdCountPreviousYearSameMonth =
    CALCULATE(
        DISTINCTCOUNT('Surg Data'[Log #]),
        FILTER(
            ALL('Surg Data'[Date]),
            YEAR('Surg Data'[Date]) = YEAR(TODAY()) - 1 &&
            MONTH('Surg Data'[Date]) = MONTH(TODAY())
        )
    )

RETURN
    IF(
        YEAR(TODAY()) = 2019 || ISBLANK(LogIdCountPreviousYearSameMonth),
        LogIdCountCurrentMonth,
        LogIdCountCurrentMonth - LogIdCountPreviousYearSameMonth
    )
 
Thank you for your help. I'm stuck!
1 ACCEPTED SOLUTION
v-kaiyue-msft
Community Support
Community Support

Hi @hsalasde ,

@amitchandak  makes a good point, I have another method here that I hope will be helpful to you.

Please try to modify the expression:

Delta =
VAR LogIdCountCurrentMonth = CALCULATE(
DISTINCTCOUNT('Surg Data'[Log]),
FILTER(
ALL('Surg Data'),
YEAR(EARLIER('Surg Data'[Date])) = YEAR('Surg Data'[Date]) &&
MONTH(EARLIER('Surg Data'[Date])) = MONTH('Surg Data'[Date])
)
)
VAR LogIdCountPreviousYearSameMonth =
CALCULATE(
DISTINCTCOUNT('Surg Data'[Log]),
FILTER(ALL('Surg Data'),
YEAR('Surg Data'[Date])=YEAR(EARLIER('Surg Data'[Date]))-1&&MONTH('Surg Data'[Date])=MONTH(EARLIER('Surg Data'[Date]))))
RETURN
IF(
YEAR(TODAY()) = 2019 || ISBLANK(LogIdCountPreviousYearSameMonth),
LogIdCountCurrentMonth,
LogIdCountCurrentMonth - LogIdCountPreviousYearSameMonth
)


The final result is shown below.

vkaiyuemsft_0-1710820590814.png

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

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-kaiyue-msft
Community Support
Community Support

Hi @hsalasde ,

@amitchandak  makes a good point, I have another method here that I hope will be helpful to you.

Please try to modify the expression:

Delta =
VAR LogIdCountCurrentMonth = CALCULATE(
DISTINCTCOUNT('Surg Data'[Log]),
FILTER(
ALL('Surg Data'),
YEAR(EARLIER('Surg Data'[Date])) = YEAR('Surg Data'[Date]) &&
MONTH(EARLIER('Surg Data'[Date])) = MONTH('Surg Data'[Date])
)
)
VAR LogIdCountPreviousYearSameMonth =
CALCULATE(
DISTINCTCOUNT('Surg Data'[Log]),
FILTER(ALL('Surg Data'),
YEAR('Surg Data'[Date])=YEAR(EARLIER('Surg Data'[Date]))-1&&MONTH('Surg Data'[Date])=MONTH(EARLIER('Surg Data'[Date]))))
RETURN
IF(
YEAR(TODAY()) = 2019 || ISBLANK(LogIdCountPreviousYearSameMonth),
LogIdCountCurrentMonth,
LogIdCountCurrentMonth - LogIdCountPreviousYearSameMonth
)


The final result is shown below.

vkaiyuemsft_0-1710820590814.png

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

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

Thank you so much for the time and effort to put this together. This is great! I'm getting an error when I try to use the EARLIER function. It keeps saying that it is referring to an earlier row context that does not exist. Do you know why this is the case?

Hi @hsalasde ,

 

EARLIER is primarily used in the context of computed columns. Make sure you are using it in a calculated column, not measure.


EARLIER will succeed if the row context exists before the table scan begins. Otherwise, it returns an error.
It is recommended to use a variable (VAR) to save the value while it is still accessible, and then hide the required row context by the new row context to access the desired value. For detailed information, please refer to the documentation:EARLIER function (DAX) - DAX | Microsoft Learn.

 

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

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

amitchandak
Super User
Super User

@hsalasde , Make sure you create a date table and join it with date of your table and then you can use time intellignece

 

 

example measures

 

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))


last year MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-12,MONTH)))
Previous year Month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth(dateadd('Date'[Date],-11,MONTH)))
last year MTD (complete) Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-12,MONTH))))
Month behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Month))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

 

then you can take a diff

 

Time Intelligence, Part of learn Power BI https://youtu.be/cN8AO3_vmlY?t=27510
Time Intelligence, DATESMTD, DATESQTD, DATESYTD, Week On Week, Week Till Date, Custom Period on Period,
Custom Period till date: https://youtu.be/aU2aKbnHuWs&t=145s

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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