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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Antmkjr
Post Patron
Post Patron

Performance issue DAX

 

Is there a way to optimize the performance of below query

 

Sale from Current Inventory Balance NY =
VAR CurrentDate =
TODAY ()
VAR InitialValue = [Opening Bal]
VAR CurrentYear =
YEAR ( CurrentDate )
RETURN
SUMX (
FILTER (
ADDCOLUMNS (
CALENDAR ( TODAY (), DATE ( YEAR ( TODAY () ) + 1, 12, 31 ) ),
"DaysSinceStart", DATEDIFF ( CurrentDate, [Date], DAY ),
"DailyValueSub",
IF (
DATEDIFF ( CurrentDate, [Date], DAY ) < 'Day Range'[Day Range Value],
DIVIDE ( [Opening Bal], 'Day Range'[Day Range Value] )
)
),
[Date] >= CurrentDate
&& YEAR ( [Date] )
= YEAR ( TODAY () ) + 1
),
[DailyValueSub]
)

1 ACCEPTED SOLUTION
shafiz_p
Super User
Super User

Hi @Antmkjr 

Here are some suggestions to optimize your query:

  1. Using TODAY() multiple times can be inefficient. Store it in a variable and reuse it.
  2. Instead of creating a calendar for the entire next year, limit it to the necessary date range.
  3. Calculate DaysSinceStart only once and reuse it.

 

Here’s an optimized version (1) of your query, you could try this:

SaleFromCurrentInventoryBalanceNY =
VAR CurrentDate = TODAY()
VAR InitialValue = [Opening Bal]
VAR CurrentYear = YEAR(CurrentDate)
VAR EndDate = DATE(CurrentYear + 1, 12, 31)
VAR DateRange = CALENDAR(CurrentDate, EndDate)
RETURN
SUMX(
    FILTER(
        ADDCOLUMNS(
            DateRange,
            "DaysSinceStart", DATEDIFF(CurrentDate, [Date], DAY),
            "DailyValueSub",
            IF(
                DATEDIFF(CurrentDate, [Date], DAY) < 'Day Range'[Day Range Value],
                DIVIDE(InitialValue, 'Day Range'[Day Range Value])
            )
        ),
        [Date] >= CurrentDate && YEAR([Date]) = CurrentYear + 1
    ),
    [DailyValueSub]
)

 

 

Version 2:

SaleFromCurrentInventoryBalanceNY =
VAR CurrentDate = TODAY()
VAR InitialValue = [Opening Bal]
VAR CurrentYear = YEAR(CurrentDate)
VAR EndDate = DATE(CurrentYear + 1, 12, 31)
VAR DateRange = CALENDAR(CurrentDate, EndDate)
VAR FilteredDates = 
    FILTER(
        DateRange,
        [Date] >= CurrentDate && YEAR([Date]) = CurrentYear + 1
    )
VAR CalculatedValues = 
    ADDCOLUMNS(
        FilteredDates,
        "DaysSinceStart", DATEDIFF(CurrentDate, [Date], DAY),
        "DailyValueSub", 
        IF(
            DATEDIFF(CurrentDate, [Date], DAY) < 'Day Range'[Day Range Value],
            DIVIDE(InitialValue, 'Day Range'[Day Range Value])
        )
    )
RETURN
SUMX(
    CalculatedValues,
    [DailyValueSub]
)

 

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

 

Best Regards,
Shahariar Hafiz 

View solution in original post

1 REPLY 1
shafiz_p
Super User
Super User

Hi @Antmkjr 

Here are some suggestions to optimize your query:

  1. Using TODAY() multiple times can be inefficient. Store it in a variable and reuse it.
  2. Instead of creating a calendar for the entire next year, limit it to the necessary date range.
  3. Calculate DaysSinceStart only once and reuse it.

 

Here’s an optimized version (1) of your query, you could try this:

SaleFromCurrentInventoryBalanceNY =
VAR CurrentDate = TODAY()
VAR InitialValue = [Opening Bal]
VAR CurrentYear = YEAR(CurrentDate)
VAR EndDate = DATE(CurrentYear + 1, 12, 31)
VAR DateRange = CALENDAR(CurrentDate, EndDate)
RETURN
SUMX(
    FILTER(
        ADDCOLUMNS(
            DateRange,
            "DaysSinceStart", DATEDIFF(CurrentDate, [Date], DAY),
            "DailyValueSub",
            IF(
                DATEDIFF(CurrentDate, [Date], DAY) < 'Day Range'[Day Range Value],
                DIVIDE(InitialValue, 'Day Range'[Day Range Value])
            )
        ),
        [Date] >= CurrentDate && YEAR([Date]) = CurrentYear + 1
    ),
    [DailyValueSub]
)

 

 

Version 2:

SaleFromCurrentInventoryBalanceNY =
VAR CurrentDate = TODAY()
VAR InitialValue = [Opening Bal]
VAR CurrentYear = YEAR(CurrentDate)
VAR EndDate = DATE(CurrentYear + 1, 12, 31)
VAR DateRange = CALENDAR(CurrentDate, EndDate)
VAR FilteredDates = 
    FILTER(
        DateRange,
        [Date] >= CurrentDate && YEAR([Date]) = CurrentYear + 1
    )
VAR CalculatedValues = 
    ADDCOLUMNS(
        FilteredDates,
        "DaysSinceStart", DATEDIFF(CurrentDate, [Date], DAY),
        "DailyValueSub", 
        IF(
            DATEDIFF(CurrentDate, [Date], DAY) < 'Day Range'[Day Range Value],
            DIVIDE(InitialValue, 'Day Range'[Day Range Value])
        )
    )
RETURN
SUMX(
    CalculatedValues,
    [DailyValueSub]
)

 

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

 

Best Regards,
Shahariar Hafiz 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.